October 12, 2018

Dynamic email template

This article, with examples, explains how to use Twig in HTML templates on Wooxy.

Variable replacement

Simple variable replacement is achieved by using the following syntax.

Template HTML

HTML
                <p><b>Hello, {{ firstName }}</b></p>

<img src="{{ image }}">

<a href="{{ link }}">Go to Wooxy</a>
            

Rendered HTML

HTML
                Hello, John
*image*
            

Conditional Content

Conditional syntax allows for the inclusion or exclusion of email content based on data from API calls or contact list attributes.

Template HTML

HTML
                {% if paidCustomer %}
  Thanks for being a customer!
{% else %}
  Sign up today and get 30% off!
{% endif %}

{% if signupType == 'referral' %}
  <a href="https://app.wooxy.com">Refer your friends!</a>
{% endif %}
            

Rendered HTML

HTML
                Sign up today and get 30% off!
            

HTML in Variables

Most HTML requirements are supported directly within the template editor. When using user-generated HTML as a variable, use Twig raw filters or autoescape tags to ensure the content is rendered correctly.

Template HTML

HTML
                {{ message|raw }}
 <a href="https://app.wooxy.com">
</a>
            

Rendered HTML

HTML
                Hello World
            

Formatting numbers

While the template editor covers most design needs, variables containing raw HTML must be handled with raw filters or autoescape tags to prevent unintended escaping.

Template HTML

HTML
                <p><b>Numbers:</b><p>
<ul>
  <li>{{ price|round }}</li>
  <li>{{ price|round(1, 'floor') }}</li>
  <li>{{ price|number_format(2, '.', ',') }}</li>
  <li>{{ price|number_format(0, '.') }}</li>
  <li>{{ "$%.2f"|format(price) }}</li>
</ul>
            

Rendered HTML

HTML
                9800
9800.3
9,800.33
9 800
$9800.33
            

Formatting Dates

The following example demonstrates the use of Twig filters for date formatting, applicable to both timestamps and ISO 8601 date strings.

Template HTML

HTML
                <p><b>Flexible date formatting</b></p>
<p>Current date: {{ "now"|date("m/d/Y") }}</p>
<p>isoDate: {{ isoDate|date("D, F y") }}</p>
<p>timeStamp to custom: {{ timestamp|date('g:ia \o\n l jS F Y')}}</p>
            

Rendered HTML

HTML
                Flexible date formatting
Current date: 10/12/2018
isoDate: Fri, April 16
timeStamp to custom: 8:48 pm 2018 Friday 12th October 2018