smorken/string2blade

Laravel的字符串到Blade编译器

v10.2.0 2024-08-08 16:55 UTC

This package is auto-updated.

Last update: 2024-09-08 17:05:12 UTC


README

提供Laravel处理原始字符串作为Blade模板的能力。

根据规范,您需要对模板字符串中的内容负责!用户提供的输入可能是一个糟糕的主意。

使用值的sha1来缓存'string',因此修改字符串值应导致Laravel创建新的缓存文件。

许可证

本软件是开源软件,许可协议为MIT许可证

Laravel框架是开源软件,许可协议为MIT许可证

安装

它应该会自动注册。如果没有,请将其添加到您的config/app.php

...
'providers' => [
        Smorken\String2Blade\ServiceProvider::class,
        Smorken\String2Blade\MailerServiceProvider::class,
...

String2Blade使用

  • 契约
// create view factory from DI
$view = App::make(\Smorken\String2Blade\Contracts\View\Factory::class);
$view->make('hello {{ $world }}!', ['world' => 'Earth']);
  • 外观
use Smorken\String2Blade\Facades\String2Blade;

String2Blade::make('hello {{ $world }}')->with('world', 'Earth');

可邮寄使用

  • Blade模板字符串
use Smorken\String2Blade\Mail\Mailable;

class TestMailable extends Mailable
{

    public function build(): void
    {
        $this->to('fiz@example.com')
             ->from('buz@example.com')
             ->subject('Test Subject')
             ->view('hello {{ $world }}')
             ->with(
                 [
                     'world' => 'earth',
                 ]
             );
    }
}
  • Markdown模板字符串
use Smorken\String2Blade\Mail\Mailable;

class TestMarkdownMailable extends Mailable
{

    public function build(): void
    {
        $this->to('fiz@example.com')
             ->from('buz@example.com')
             ->subject('Test Subject')
             ->markdown($this->getMarkdown())
             ->with(
                 [
                     'world' => 'earth',
                 ]
             );
    }

    protected function getMarkdown(): string
    {
        return <<<STR
@component('mail::message')
# Hello

Your world is {{ \$world }}
@endcomponent
STR;
    }
}