Coding Custom Blocks

The ability to code your own blocks is really where everything in Sendlio comes together. When you click the "Edit Block" button in the campaign design view, a modal will appear containing the Sendlio Code Editor. This is where your work will be done.

It's important to note that, in order to get the most from Sendlio, you don't want to put all of your email's code into a single block. Instead, create individual sections. This way, you can save them as custom blocks and re-use them in the future. Remember, each time you use a block, it can be connected to a different data source.

Code Editor

The code editor allows you to edit the code for your individual blocks. Sendlio uses a templating language called Twig.  If you're faimilar with the Symfony PHP framework then using Sendlio's templates will be second nature. Twig is very versatile and similar in many ways to the Liquid syntax.

Variables

The application passes variables to the templates for manipulation in the template. Variables may have attributes or elements you can access, too. 

Use a dot ( .) to access attributes of a variable (methods or properties of a object, or items of an array):

{{ foo.bar }}

Setting Variables

You can assign values to variables inside code blocks. Assignments use the set tag:

{% set foo = 'foo' %}{% set foo = [1, 2] %}{% set foo = {'foo': 'bar'} %}
			

Filters

Variables can be modified by filters. Filters are separated from the variable by a pipe symbol ( |). Multiple filters can be chained. The output of one filter is applied to the next.

The following example removes all HTML tags from the  name and title-cases it:

{{ name|striptags|title }}
			

Filters that accept arguments have parentheses around the arguments. This example joins the elements of a list by commas:

{{ list|join(', ') }}
			

To apply a filter on a section of code, wrap it with the apply tag:

{% apply upper %} This text becomes uppercase {% endapply %}
		

Functions

Functions can be called to generate content. Functions are called by their name followed by parentheses ( ()) and may have arguments.

For instance, the  range function returns a list containing an arithmetic progression of integers:

{% for i in range(0, 3) %}{{ i }}, {% endfor %}
					

Named Arguments

{% for i in range(low=1, high=10, step=2) %}{{ i }}, {% endfor %}
					

Using named arguments makes your templates more explicit about the meaning of the values you pass as arguments:

{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}

{# versus #}

{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
					

Named arguments also allow you to skip some arguments for which you don't want to change the default value:

{# the first argument is the date format, which defaults to the global date format if null is passed #}
{{ "now"|date(null, "Europe/Paris") }}

{# or skip the format value by using a named argument for the time zone #}
{{ "now"|date(timezone="Europe/Paris") }}
					

You can also use both positional and named arguments in one call, in which case positional arguments must always come before named arguments:

{{ "now"|date('d/m/Y H:i', timezone="Europe/Paris") }}
					

Each function and filter documentation page has a section where the names of all arguments are listed when supported.

Control Structure

A control structure refers to all those things that control the flow of a program - conditionals (i.e.  if/elseif/else), for-loops, as well as things like blocks. Control structures appear inside {% ... %} blocks.

For example, to display a list of users provided in a variable called  users, use the for tag:

<h1>Members</h1> 
<ul>     
{% for user in users %}        
 <li>{{ user.username|e }}</li>     
{% endfor %} 
</ul>
					

The if tag can be used to test an expression:

{% if users|length > 0 %}     
<ul>         
{% for user in users %}             
<li>{{ user.username|e }}</li>         
{% endfor %}     
</ul>
 {% endif %}
					

Go to the tags page to learn more about the built-in tags.

Comments

To comment-out part of a line in a template, use the comment syntax  {# ... #}. This is useful for debugging or to add information for other template designers or yourself:

{# note: disabled template because we no longer use this     
{% for user in users %}         ...     {% endfor %} #}
					

Expressions

Twig allows expressions everywhere.

The operator precedence is as follows, with the lowest-precedence operators listed first:  ?: (ternary operator), b-andb-xorb-ororand==!=<=><>>=<=inmatchesstarts withends with..+-~*///%is (tests), **??| (filters), [], and .:

{% set greeting = 'Hello ' %}
{% set name = 'Fabien' %}

{{ greeting ~ name|lower }} {# Hello fabien #}

{# use parenthesis to change precedence #}
{{ (greeting ~ name)|lower }}{# hello fabien #}
								

Literals

The simplest form of expressions are literals. Literals are representations for PHP types such as strings, numbers, and arrays. The following literals exist:

  • "Hello World": Everything between two double or single quotes is a string. They are useful whenever you need a string in the template (for example as arguments to function calls, filters or just to extend or include a template). A string can contain a delimiter if it is preceded by a backslash (\) -- like in 'It\'s good'. If the string contains a backslash (e.g. 'c:\Program Files') escape it by doubling it (e.g. 'c:\\Program Files').
  • 42 / 42.23: Integers and floating point numbers are created by writing the number down. If a dot is present the number is a float, otherwise an integer.
  • ["foo", "bar"]: Arrays are defined by a sequence of expressions separated by a comma (,) and wrapped with squared brackets ([]).
  • {"foo": "bar"}: Hashes are defined by a list of keys and values separated by a comma (,) and wrapped with curly braces ({}):
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
    									
    {# keys as string #}
     { 'foo': 'foo', 'bar': 'bar' }  
    
    {# keys as names (equivalent to the previous hash) #} 
    { foo: 'foo', bar: 'bar' }  
    
    {# keys as integer #} 
    { 2: 'foo', 4: 'bar' }  
    
    {# keys can be omitted if it is the same as the variable name #}
     { foo }
    
     {# is equivalent to the following #} 
    { 'foo': foo } 
    
     {# keys as expressions (the expression must be enclosed into parentheses) #}
    {% set foo = 'foo' %}
     { (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }
    									
  • true / falsetrue represents the true value, false represents the false value.
  • nullnull represents no specific value. This is the value returned when a variable does not exist. none is an alias for null.

Arrays and hashes can be nested:

{% set foo = [1, {"foo": "bar"}] %}
								

Math

Twig allows you to do math in templates; the following operators are supported:

  • +: Adds two numbers together (the operands are casted to numbers). {{ 1 + 1 }} is 2.
  • -: Subtracts the second number from the first one. {{ 3 - 2 }} is 1.
  • /: Divides two numbers. The returned value will be a floating point number. {{ 1 / 2 }} is {{ 0.5 }}.
  • %: Calculates the remainder of an integer division. {{ 11 % 7 }} is 4.
  • //: Divides two numbers and returns the floored integer result. {{ 20 // 7 }} is 2{{ -20 // 7 }} is -3 (this is just syntactic sugar for the round filter).
  • *: Multiplies the left operand with the right one. {{ 2 * 2 }} would return 4.
  • **: Raises the left operand to the power of the right operand. {{ 2 ** 3 }} would return 8.

Logic

You can combine multiple expressions with the following operators:

  • and: Returns true if the left and the right operands are both true.
  • or: Returns true if the left or the right operand is true.
  • not: Negates a statement.
  • (expr): Groups an expression.

Operators are case sensitive.

Comparisons

The following comparison operators are supported in any expression:  ==!=<>>=, and <=.

You can also check if a string  starts with or ends with another string:

{% if 'Fabien' starts with 'F' %}{% endif %}{% if 'Fabien' ends with 'n' %}{% endif %}
								

For complex string comparisons, the  matches operator allows you to use regular expressions:

{% if phone matches '/^[\\d\\.]+$/' %}{% endif %}
								

Containment Operator

The  in operator performs containment test. It returns true if the left operand is contained in the right:

{# returns true #}{{ 1 in [1, 2, 3] }}{{ 'cd' in 'abcde' }}
								

You can use this filter to perform a containment test on strings, arrays, or objects implementing the  Traversable interface.

To perform a negative test, use the  not in operator:

{% if 1 not in [1, 2, 3] %}{# is equivalent to #}{% if not (1 in [1, 2, 3]) %}
							

Test Operator

The  is operator performs tests. Tests can be used to test a variable against a common expression. The right operand is name of the test:

{# find out if a variable is odd #}{{ name is odd }}
								

Tests can accept arguments too:

{% if post.status is constant('Post::PUBLISHED') %}
								

Tests can be negated by using the  is not operator:

{% if post.status is not constant('Post::PUBLISHED') %}{# is equivalent to #}{% if not (post.status is constant('Post::PUBLISHED')) %}
								

Go to the tests page to learn more about the built-in tests.

Other Operators

The following operators don't fit into any of the other categories:

  • |: Applies a filter.
  • ..: Creates a sequence based on the operand before and after the operator (this is syntactic sugar for the range function):
    {{ 1..5 }}{# equivalent to #}{{ range(1, 5) }}
    									

    Note that you must use parentheses when combining it with the filter operator due to the operator precedence rules:

    (1..5)|join(', ')
    									
  • ~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is 'John'Hello John!.
  • .[]: Gets an attribute of a variable.
  • ?:: The ternary operator:
    {{ foo ? 'yes' : 'no' }}{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
    									
  • ??: The null-coalescing operator:
    {# returns the value of foo if it is defined and not null, 'no' otherwise #}{{ foo ?? 'no' }}
    									

String Interpolation

String interpolation ( #{expression}) allows any valid expression to appear within a double-quoted string. The result of evaluating that expression is inserted into the string:

{{ "foo #{bar} baz" }}{{ "foo #{1 + 2} baz" }}
								
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us