Larevel markdown render output to blade template

Time:2020-10-26

Original address:https://www.wjcms.net/archives/laravelmarkdown Render output to blade template

preface

Yesterday, we released an article supporting markdown editor by laravel, and uploaded the configuration image. However, some netizens asked how to render the output in the blade template. Here is an article to record.

Install expansion pack

Larravel markdown requires PHP 7.2-8.0. This particular version supports laravel 6-8.

According to the table above, select the appropriate version. My version is 8, so install version 13.1.

composer require graham-campbell/markdown:^13.1

When I installed, I found an error:

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.#

So here we use the following command to install:

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

/ usr / bin / composer in the above command is the installation address of composer
Available
composer -h
Command.

Configure providers

//cconfig/app.php
'providers' => [
    //Add the following line
    GrahamCampbell\Markdown\MarkdownServiceProvider::class,
]

Configure alias

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

Copy the relevant files to the project folder

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

Used in controller

  1. Easy to use
use GrahamCampbell\Markdown\Facades\Markdown;

Markdown::convertToHtml('foo'); // foo
  1. How to write dependency injection
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();

Used in blade template

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

For more information, please refer to the official documents.
https://github.com/GrahamCampbell/Laravel-Markdown

Recommended Today

MVC and Vue

MVC and Vue This article was written on July 27, 2020 The first question is: is Vue an MVC or an MVVM framework? Wikipedia tells us: MVVM is a variant of PM, and PM is a variant of MVC. So to a certain extent, whether Vue is MVC or MVVM or not, its ideological direction […]