如何在 Laravel 中将 Markdown 文本传递给 Markdown Blade?

我需要在 Laravel 中发送 Markdown 电子邮件,但是该电子邮件的文本必须是可编辑的。当我传递$body到相关视图时,它显示如下:


$body = '''

# Introduction

hi {{ $username }} 

The body of your {{ $family }}.


@component('mail::button', ['url' => ''])

Button Text

@endcomponent

'''

在blade的相关视图中:


@component('mail::message')


    {{ $body }}


Thanks,<br>

{{ config('app.name') }}

@endcomponent

这是输出:

https://img.mukewang.com/6519288c00017b6706450409.jpg

有谁知道为什么会发生这种情况?



慕尼黑8549860
浏览 73回答 3
3回答

千万里不及你

您无需将其作为字符串传递,而是将整个主体放入已有的视图刀片中,并包含上述所有变量,如下所示:&nbsp; @component('mail::message')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;hi {{ $username }}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;The body of your {{ $family }}.&nbsp; &nbsp; &nbsp; &nbsp;@component('mail::button', ['url' => ''])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Button Text&nbsp; &nbsp; &nbsp; &nbsp;@endcomponent&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Thanks,<br>&nbsp; &nbsp; &nbsp; &nbsp;{{ config('app.name') }}&nbsp; &nbsp; @endcomponent然后,在发送邮件时,只需将所需的所有变量传递给该视图即可。由于我不确定您如何发送电子邮件,以下是使用 Mailable 类的示例:Mail::to('email_address')->send(new MailableClass($username, $family));然后,您的 Mailable 类将如下所示:public function __construct($username, $family)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->username = $username;&nbsp; &nbsp; &nbsp; &nbsp; $this->family = $family;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public function build()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data['username'] = $this->username;&nbsp; &nbsp; &nbsp; &nbsp; $data['family'] = $this->family;&nbsp; &nbsp; &nbsp; &nbsp; return $this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->view('your_blade', $data)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->subject('Subject');&nbsp; &nbsp; }然后,您的变量将显示在给定的视图中。

慕妹3242003

只需将其更改为查看刀片:{!!&nbsp;$body&nbsp;!!}

红糖糍粑

将 Markdown 内容作为变量传递到空 Blade 文件,但它不起作用。我的 Mailable 类未处理刀片指令(例如@component)。为什么?看起来,在替换变量之前,render中的方法Illuminate\Mail\Markdown.php将替换 Blade 视图文件中的邮件组件(按原样)。解决方法并不理想,但现在我最终修改了一个实际文件(仅用作占位符)。注意:您可能希望 git 忽略此文件,或更新回空。class CampaignFormat extends Mailable{    protected $markdownContent;    public function __construct($markdownContent)    {        $this->markdownContent = $markdownContent;    }    /**     * Required, since it won't let me pass the content to the file as a string variable.     * It expects to have the component directives directly on the actual blade file.     */    const DYNAMIC_MARKDOWN_TEMPLATE_PATH = 'views/emails/dynamic_markdown_template.blade.php';    /**     * The render method invokes this one internally.     */    public function build()    {                $this->markdown('emails.dynamic_markdown_template')            ->with('markdownContent', $this->markdownContent);    }        private function updateTemplateFileTemporarily()    {        // Update the file with the desired content        File::put(resource_path(self::DYNAMIC_MARKDOWN_TEMPLATE_PATH), $this->markdownContent);       }        private function clearTemplateFile()    {        // Clear the file so it doesn't keep changes        File::put(resource_path(self::DYNAMIC_MARKDOWN_TEMPLATE_PATH), '');    }    /**     * It does the magic.     */    public function dynamicRender(): string    {        $this->updateTemplateFileTemporarily();        $html = $this->render();        $this->clearTemplateFile();        return $html;    }}就我而言,我只是使用它来获取渲染的 HTML,并将其存储到我的数据库中。我使用的电子邮件服务将负责将 HTML 批量发送给我的用户。
打开App,查看更多内容
随时随地看视频慕课网APP