为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕尼黑8549860 2023-10-01 16:05:25
我需要在 Laravel 中发送 Markdown 电子邮件,但是该电子邮件的文本必须是可编辑的。当我传递$body到相关视图时,它显示如下:$body = '''# Introductionhi {{ $username }} The body of your {{ $family }}.@component('mail::button', ['url' => ''])Button Text@endcomponent'''在blade的相关视图中:@component('mail::message')    {{ $body }}Thanks,<br>{{ config('app.name') }}@endcomponent这是输出:有谁知道为什么会发生这种情况?
查看完整描述

3 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

您无需将其作为字符串传递,而是将整个主体放入已有的视图刀片中,并包含上述所有变量,如下所示:


  @component('mail::message')

    

       hi {{ $username }} 

       The body of your {{ $family }}.


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

          Button Text

       @endcomponent

    

       Thanks,<br>

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

    @endcomponent

然后,在发送邮件时,只需将所需的所有变量传递给该视图即可。由于我不确定您如何发送电子邮件,以下是使用 Mailable 类的示例:


Mail::to('email_address')->send(new MailableClass($username, $family));

然后,您的 Mailable 类将如下所示:


public function __construct($username, $family)

    {

        $this->username = $username;

        $this->family = $family;

    }


    

    public function build()

    {

        $data['username'] = $this->username;

        $data['family'] = $this->family;


        return $this

            ->view('your_blade', $data)

            ->subject('Subject');

    }

然后,您的变量将显示在给定的视图中。


查看完整回答
反对 回复 2023-10-01
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

只需将其更改为查看刀片:

{!! $body !!}


查看完整回答
反对 回复 2023-10-01
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

将 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 批量发送给我的用户。


查看完整回答
反对 回复 2023-10-01
  • 3 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信