如何将Markdown文本传递到Laravel中的Markdown刀片?

问题描述

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

$body = '''
# Introduction
hi {{ $username }} 
The body of your {{ $family }}.

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

在刀片的相关视图中:

@component('mail::message')

    {{ $body }}

Thanks,<br>
{{ config('app.name') }}
@endcomponent

这是输出

laravel-output

有人知道为什么会这样吗?

解决方法

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

  @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');
    }

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

,

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

{!! $body !!}