首页 > 解决方案 > 如何在 Laravel Markdown Mailable 中生成一个换行符

问题描述

这是可邮寄的模板:

@component('mail::message')
Address line 1
Address line 2
Address line 3
@endcomponent

这是生成的 HTML:

<p style="font-family: Avenir, Helvetica, sans-serif; box-sizing: border-box; color: #74787E; font-size: 16px; line-height: 1.5em; margin-top: 0; text-align: left;">Address line 1
Address line 2
Address line 3</p>

当您在浏览器中显示此内容时...

地址行 1 地址行 2 地址行 3

因此,我已经看到了其他几个关于此的 SO 问题,但它们并没有解决我的问题。我试过在每行后面加上两个空格,也用反斜杠。两者都没有任何效果。

标签: laravelmarkdown

解决方案


首先,总是可以将 markdown 和 HTML 结合起来:

@component('mail::message')
Address line 1<br>
Address line 2<br>
Address line 3<br>
@endcomponent

另一种方法是,使用换行符\n并将其解析为nl2br()

@component('mail::message')
{!! nl2br("Address line 1\n Address line 2\n Address line 3") !!}
@endcomponent

注意:nl2br()仅当字符串在双引号中时才有效!

我也喜欢表格组件:

@component('mail::table')
| Addresses      |
| -------------- |
| Address line 1 |
| Address line 2 |
| Address line 3 |
@endcomponent

还有一件事:不要考虑将模板文件中的所有内容重构为通知。通知还支持作为电子邮件发送,并且可以将邮件模板重用于其他电子邮件。(但这不会帮助您了解新线路。)


推荐阅读