30

Laravel markdown渲染输出到blade模版

 3 years ago
source link: https://www.wjcms.net/archives/laravelmarkdown渲染输出到blade模版
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

前言

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

安装扩展包

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

EFZVzeJ.png!mobile

对照上边的表,选择对应合适的版本,这里我的版本是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.#

7fqIz2e.png!mobile

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

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->converter = $converter;
    }

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

App::make('Foo')->bar();

blade模版中使用

@markdown
{{$data->content}}
@endmarkdown

更多内容参考官方文档。

https://github.com/GrahamCampbell/Laravel-Markdown

bqayQzj.jpg!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK