为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel markdown渲染输出到blade模版

标签:
laravel

前言

> 昨天,发布了laravel支持markdown编辑器的文章,还附上了配置图片上传,但是有网友问怎么在blade模版中渲染输出,这里写个文章记录一下。

安装扩展包

Laravel Markdown需要PHP 7.2-8.0 。此特定版本支持Laravel 6-8。

对照上边的表,选择对应合适的版本,这里我的版本是8,所以安装13.1版本。

composer require graham-campbell/markdown:^13.1

在我安装的时候发现报错:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///www/server/php/74/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223
 
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) in phar:///www/server/php/74/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223
 
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.#   

所以这里我们使用如下命令进行安装:

php -d memory_limit=-1 /usr/bin/composer require graham-campbell/markdown:^13.1

上述命令中的/usr/bin/composer,为composer安装地址
可使用
composer -h
命令进行获取。

配置providers

//cconfig/app.php
'providers' => [
    //添加如下一行
    GrahamCampbell\Markdown\MarkdownServiceProvider::class,
]

配置alias

 'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class,

拷贝相关文件到项目文件夹中

php artisan vendor:publish --provider="GrahamCampbell\Markdown\MarkdownServiceProvider"

控制器中使用

  1. 简单使用
use GrahamCampbell\Markdown\Facades\Markdown;

Markdown::convertToHtml('foo'); // <p>foo</p>
  1. 依赖注入的写法
use Illuminate\Support\Facades\App;
use League\CommonMark\MarkdownConverterInterface;

class Foo
{
    protected $converter;

    public function __construct(MarkdownConverterInterface $converter)
    {
        $this-&gt;converter = $converter;
    }

    public function bar()
    {
        return $this-&gt;converter-&gt;convertToHtml('foo');
    }
}

App::make('Foo')-&gt;bar();

blade模版中使用

@markdown
{{$data-&gt;content}}
@endmarkdown

更多内容参考官方文档。
https://github.com/GrahamCampbell/Laravel-Markdown

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消