SigalZ's avatar
Level 5

Email template markdown conditional table lines

Hello,

I am trying to create a markup email template.

I want to display an address in a table column.

The address has these lines:

addr_line_1, addr_line_2, suburb, city, province, post_code

If the addr_line_2 is empty, I don't want the display it, the same with the suburb field.

I can't figure a way to do this in a table. I have styles for table headings and that's why I want to use a table.

Not sure it is possible.

The blade:

<x-mail::table>
| Address |
| ------------|
|{{  $data['address'] }} |
</x-mail::table>

As an example, let's say the address is:

addr_line_1: my street

addr_line_2: NULL

suburb: NULL

city: Johannesburg

province: Gauteng

post_code: 1111

In the array sent to the blade, I build the address like this:

$data['address'] = $addr->addr_line_1;

if(!is_null($addr->addr_line_2))
      $data['address']  .= "<br>{$addr->addr_line_2}";
            
if(!is_null($addr->suburb))
       $data['address'] .= "<br>{$addr->suburb}";

$data['address'] .= "<br>{$addr->city}<br>{$addr->province}  {$addr->post_code}";

The result I get:

my streetJohannesburgFree State 9707

I tried adding spaces instead of :

$data['address'] .= "      {$addr->city}      {$addr->province}  {$addr->post_code}";

That just displayed the spaces as well: my street Johannesburg Free State 9707.

I tried:

$data['address'] .= "\<br> {$addr->city}\<br>{$addr->province}  {$addr->post_code}";

The result: my street <br>Johannesburg<br>Free State 9707.

Anyway to solve this?

0 likes
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@sigalz Just a wild guess!

Isn't that simply work for you if you try to check in the email template?

For example:

<x-mail::table>
| Address |
| ------------|
| 
@if($data['address']['addr_line_1'])
{{ $data['address']['addr_line_1'] }}
@endif
|
</x-mail::table>
3 likes

Please or to participate in this conversation.