How can we help?

Advanced reusable HTML content usage subject

Overview

Similar to this advanced example, there are times where you might want to use similar logic in both the subject line and the body. When doing complex code, we like to move the logic to an HTML include and then use the output variables (not the HTML) in our content.

Example use case

In the following example, we're going to look up the weather. Then we're going to use the weather results in both the subject and the body. One thing to understand here is that the code in the include will execute twice, once for subject and once for body.  

1. To accomplish this using the advanced method, you start with the HTML content. Here's an example:

{$data.beach_temp = 68}
{$data.weather_url = 'https://api.worldweatheronline.com/premium/v1/weather.ashx?key=bc145e6ca1cf46c9955225423181201&q=__zip_code__&format=json&num_of_days=1'}
{$data.weather_cache_minutes = 60}

{if $contact.zip_code}
{$data.zip_code = $contact.zip_code}
{else}
{$data.zip_code = '92101'}
{/if}

{$data.weather_url = $data.weather_url|replace:'__zip_code__':$data.zip_code}
{$weather=$utils->getJson($data.weather_url, $data.weather_cache_minutes)} 

{if isset($weather) && $weather.data.current_condition[0].temp_F}
  {$data.temp = $weather.data.current_condition[0].temp_F}
{/if}

{* when subject and body are both not equal to 1 then we must be debugging the template *}
{if $isSubject != 1 && $isBody != 1}
  {$isSubject = 1}
  {$isBody = 1}
  {$debug = 1}
{/if}

{if $isSubject == 1}
  {$data.isSubject = 1}
  {$data.subject_text = ''}
  {if $data.temp > $data.beach_temp}
    {$data.subject_text = 'You ready for the beach?'}
  {else}
    {$data.subject_text = 'A good day for the gym?'}
  {/if}
{/if}

{if $isBody == 1}
  {$data.isBody = 1}
  {$data.body_text = ''}
  {if $data.temp > $data.beach_temp}
    {$data.body_text = "It is {$data.temp} outside, make sure you put on some sunscreen before going to the beach."}
  {else}
    {$data.body_text = "It is {$data.temp} outside, maybe you should hit the indoor gym instead."}
  {/if}
{/if}

{if $debug == 1}
<h1>Content Data</h1>
<pre>
{$JSON_PRETTY_PRINT = 128}
{json_encode($data, $JSON_PRETTY_PRINT)}
</pre>
<br><hr><br>

<h1>Contact Data</h1>
<pre>
{$JSON_PRETTY_PRINT = 128}
{json_encode($contact, $JSON_PRETTY_PRINT)}
</pre>
<br><hr><br>

<h1>Weather JSON Info</h1>
<pre>
{$JSON_PRETTY_PRINT = 128}
{json_encode($weather, $JSON_PRETTY_PRINT)}
</pre>
<br><hr><br>
{/if}

2. Now we need to use this this code in the subject. You'll notice below that we start by setting isSubject, this gets used inside the include to know which code is only to be executed for the subject line.

{$isSubject=1}{include 'content:body_subject' assign='blackhole' scope='parent'}{$data.subject_text}

3. In the body, we set isBody similar to isSubject.

{$isBody=1}{include 'content:body_subject' assign='blackhole' scope='parent'}
<div>{$data.body_text}</div>

4. When you preview the HTML content, you'll get some debugging output.

5. And when you preview the message you can see the output of the subject and body.

Comments

0 comments

Please sign in to leave a comment.