Laravel- use @foreach in markdown mail

2019-06-16 21:28发布

问题:

I am trying to use @foreach loop inside markdown template for sending mails.

While using HTML tags inside @foreach, it is not rendered properly

@component('mail::message')

These are the latest contents in our website

@foreach($results as $type => $result)
   <h4>{{ $result['name'] }}</h4>
@endforeach

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

In the received mail, the <h4> tag will be displayed as,

<h4>Article</h4>

Markdown is not getting processed when it's placed inside @foreach loop. But it's processed when placed outside @foreach loop.

Any help will be much appreciated. Thanks.

回答1:

As stated in laravel documentation:

Do not use excess indentation when writing Markdown emails. Markdown parsers will render indented content as code blocks.

The problem is not the HTML tags put inside the @foreach, but the indentation makes parsers render as follows (I am using markdown now, 4 spaces before <h4>):

<h4>Article</h4>


回答2:

Good question, the same applies with an example using markup instead of html. For example:

@component('mail::message')
# Data received:

@foreach($fields as $name => $value)
    **{{ $name }}: ** {{ $value }}

@endforeach

@endcomponent

Can result in:

<h1>Data received:</h1>
<pre><code>**voornaam: ** piet
**familienaam: ** hein</code></pre>

While with 2 spaces indentation instead of 4 like so

@component('mail::message')
# Data received:

@foreach($fields as $name => $value)
  **{{ $name }}: ** {{ $value }}

@endforeach

@endcomponent

The same input results in:

<h1>Data received:</h1>
<p><strong>voornaam: </strong> piet</p>
<p><strong>familienaam: </strong> hein</p>

as intended and explained by @Ben pointing to https://laravel.com/docs/5.8/mail#writing-markdown-messages

Do not use excess indentation when writing Markdown emails. Markdown parsers will render indented content as code blocks.