Search through more than a hundred articles on every aspect of User.com

Dawid Tyburek
Written by Dawid Tyburek

Advanced data rendering

Render the content of your messages using and other statements


Introduction

In our app, you can render different data based on the user's attributes. For example you might have a company with different level Employees and you want to send one type of communication but covering different topics. You can do that using if statements.

Use cases

  • Let's say you have companies with Employees on a different level. You might want to differentiate parts of your communication to be relevant to the employee you send your messages to. Simply use if statements to check if the receiver meets the specified criteria and if yes the email will show him desired message. If not, the email will render something different, of your choice.

  • Sometimes you want to prepare one template to users who have an attribute filled in certain way, for example if they are Sellers or Buyers. You can prepare one email to address both groups, especially if you want only some parts of the email to be different then you don't have to prepare multiple versions of your message.
  • If you run a Car Sharing Company you might want to send a different message based of the favourite rental car or the time somebody spent using your fleet. Just prepare different versions of the message and use desired attribute in within the statement.

if statements

Let's assume that you want to render different message blocks depending whether a receiver has provided their first name in your database. 

Start with the statement: 

{% if receiver.first_name %}
Hello {{ receiver.first_name }}, how are you?
This is what you can do with the Bonus Points accumulated on your account.
{% else %}
Hello, my friend.
Here is a link to enroll to our Bonus Points Program, make sure to take part today!
{% endif %}

Let's translate the snippet above line by line.

{% if receiver.first_name %} - Checks if the receiver of the message has a first name attribute. If yes, it uses the first name in the welcome sentence and renders the prepared message.

{% else %} - When the previous condition is false, it renders the message below.

{% endif %} - It end the statement.


2. "elif" statements and logic operators

There is also a more advanced way to use "If" statements. When your statement block contains "elif" statements then the value under the first "true" statement will be executed. 

Start with the statement: 

{% if receiver.first_name and receiver.last_name %}
Hello {{ receiver.first_name }} {{ receiver.last_name }}

{% elif receiver.first_name and not receiver.last_name %}
Hello {{ receiver.first_name }}

{% elif not receiver.first_name and receiver.last_name %}
Hello Mr. {{ receiver.last_name }}

{% else %}
Hello friend

{% endif %}

Let's translate the snippet above line by line:

{% if receiver.first_name and receiver.last_name %} - If receiver has any value for first_name and last_name, render content below:

{% elif receiver.first_name and not receiver.last_name %} - If receiver has any value for first_name but not for last_name, render content below:

{% elif not receiver.first_name and receiver.last_name %} - If receiver has no value for first_name but has value for last_name, render content below

{% else %} - If all statements above are "false", render content below:

{% endif %} - this is the tag that ends the statement.

3. Filters

Note: remember when rendering receiver attribute, use double curly brackets:

{{ }}

but when introducing logic, use curly bracket with a percentage sign:  

{% %}

If the receiver.first_name in your database starts with a lowercase letter, you may want to use one of the available filters:

{{ receiver.first_name|capfirst }} - If the first_name is john it will be capitalized to John

More useful filters

{{ receiver.value|cut:" " }} - removes all values of an argument in a given string

{{ receiver.first_name|default:"Friend" }} - If receiver.first_name is empty, use default value Friend.

{{ receiver.attribute|divisibleby:"3" }} - If value under attributes is divisible by 3, it returns as "True".

{{ receiver.attribute|lower }} - converts text into lowercase


Related articles


Categories: