如何在视图的 Blade 模板中使用 Laravel Markdown

huangapple go评论59阅读模式
英文:

How to use Laravel Markdown in View blade template

问题

我的主要目标是获取视图文件的原始HTML输出,然后将其发送到外部API(Zoho)以处理电子邮件。

我有一个位于/resources/views/emails/welcome.blade.php的文件(该文件没有Mailable类),并且我有以下代码,它提取文件的原始HTML(使用view()->render()):

$to = [
    'user_name' => 'Test',
    'email' => 'xxx@email.com'
];

$mail = [
    'to' => $to,
    'lines' => ['Test 1', 'Test 2']
];

$data = [
    'to' => [$to],
    'mail_format' => 'html',
    'content' => view('emails.welcome', compact('mail'))->render()
];

$payload = $this->payload([$data]);

但是,我希望在welcome.blade.php上使用Markdown,我尝试使用其他电子邮件模板上看到的<x-mail:message>,如下面的代码,但它抛出错误**未为[mail]定义提示路径。**:

<x-mail::message>
# Hi {{ $mail['to']['user_name'] }}

{{-- Lines --}}
@foreach ($mail['lines'] as $line)
{!! $line !!}

@endforeach

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>

如果我直接将内容添加到文件中,Markdown标记不起作用。

感谢任何帮助 如何在视图的 Blade 模板中使用 Laravel Markdown

英文:

My primary goal was to grab the raw html output of the view file to send to an external API (Zoho) to process the email.

I have a file in /resources/views/emails/welcome.blade.php (there is no Mailable class for that file)

and I have this code below which pulls the raw html of the file ( using view()-&gt;render() )

$to = [
    &#39;user_name&#39; =&gt; &#39;Test&#39;,
    &#39;email&#39; =&gt; &#39;xxx@email.com&#39;
];

$mail = [
    &#39;to&#39; =&gt; $to
    &#39;lines&#39; =&gt; [&#39;Test 1&#39;, &#39;Test 2&#39;]
];

$data = [
    &#39;to&#39; =&gt; [$to],
    &#39;mail_format&#39; =&gt; &#39;html&#39;,
    &#39;content&#39; =&gt; view(&#39;emails.welcome&#39;, compact(&#39;mail&#39;))-&gt;render()
];

$payload = $this-&gt;payload( [$data] );

However, I want to use markdown on the welcome.blade.php, I tried using the &lt;x-mail:message&gt; I've seen on other email template like the code below, but its throwing an error No hint path defined for [mail].

&lt;x-mail::message&gt;
# Hi {{ $mail[to][&#39;user_name&#39;] }}

{{-- Lines --}}
@foreach ($mail[&#39;lines&#39;] as $line)
{!! $line !!}

@endforeach

Thanks,&lt;br&gt;
{{ config(&#39;app.name&#39;) }}
&lt;/x-mail::message&gt;

and if I add the content directly in the file, the markdown tagging does not work

appreciate any help 如何在视图的 Blade 模板中使用 Laravel Markdown

答案1

得分: 1

这是未经测试的,我尝试在本地使Markdown工作,它有效,你可以测试一下(不要忘记在你的类顶部添加 use Illuminate\Mail\Markdown):

use Illuminate\Mail\Markdown;

...

$to = [
    'user_name' => 'Test',
    'email' => 'xxx@email.com'
];

$mail = [
    'to' => $to,
    'lines' => ['Test 1', 'Test 2']
];

$markdown = app(Markdown::class);

$data = [
    'to' => [$to],
    'mail_format' => 'html',
    'content' => $markdown->render('emails.welcome', compact('mail'))->toHtml()
];

$payload = $this->payload([$data]);

希望这有帮助!

英文:

This is untested, I tried to make the Markdown work on my local and it works, can you test this out (don't forget to put use Illuminate\Mail\Markdown at the top of your class):

use Illuminate\Mail\Markdown;

...

$to = [
    &#39;user_name&#39; =&gt; &#39;Test&#39;,
    &#39;email&#39; =&gt; &#39;xxx@email.com&#39;
];

$mail = [
    &#39;to&#39; =&gt; $to
    &#39;lines&#39; =&gt; [&#39;Test 1&#39;, &#39;Test 2&#39;]
];

$markdown = app(Markdown::class);

$data = [
    &#39;to&#39; =&gt; [$to],
    &#39;mail_format&#39; =&gt; &#39;html&#39;,
    &#39;content&#39; =&gt; $markdown-&gt;render(&#39;emails.welcome&#39;, compact(&#39;mail&#39;))-&gt;toHtml()
];

$payload = $this-&gt;payload( [$data] );

huangapple
  • 本文由 发表于 2023年2月27日 08:35:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定