• Laravel markdown渲染输出到blade模版


    原文地址:https://www.wjcms.net/archives/laravelmarkdown渲染输出到blade模版

    前言

    昨天,发布了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' => [
        //添加如下一行
        GrahamCampbellMarkdownMarkdownServiceProvider::class,
    ]
    

    配置alias

     'Markdown' => GrahamCampbellMarkdownFacadesMarkdown::class,
    

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

    php artisan vendor:publish --provider="GrahamCampbellMarkdownMarkdownServiceProvider"
    

    控制器中使用

    1. 简单使用
    use GrahamCampbellMarkdownFacadesMarkdown;
    
    Markdown::convertToHtml('foo'); // <p>foo</p>
    
    1. 依赖注入的写法
    use IlluminateSupportFacadesApp;
    use LeagueCommonMarkMarkdownConverterInterface;
    
    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

  • 相关阅读:
    Erlang性能的八个误区
    Unity预览
    一步步实现cnblogs博客采集工具>实现辅助对话框
    Asp.Net MVC 必备插件MVC Route Visualizer(Visual Studio 2012 版)
    IBM SOA[ESB,BPM,Portal等]基础架构图解
    PowerShell收发TCP消息包
    Sonar安装使用篇
    在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke
    排序算法
    ASP.NET MVC Web API 学习增删改查
  • 原文地址:https://www.cnblogs.com/wjcms/p/13783929.html
Copyright © 2020-2023  润新知