Admin Guide

Twig syntax basics


The templating system Deskpro uses is called Twig .

Below are the features you’ll need to know to do the most common forms of customization.

Variables

You insert a variable into a template with double curly braces:

{{variable}}

There are dozens of variables available for Deskpro.

Here are some examples:

{{ticket.subject}}
  • the subject line of the current ticket
{{ticket.agent.display_name}}
  • the name of the agent assigned to the current ticket (or their override name if one is set)

Note that in snippets and trigger actions, you have to put spaces before and after the variable name, like this:

{{ variable }}

but that does not work in templates.

Tags

Twig also includes tags which control the logic of the template.

For example, the {% if %} tag lets you have a section of your template that only displays if a certain condition is met.

This code on a portal page:

{% if app.user.id %}
  <p>You are logged in.</p>
{% endif %}

would display the text “You are logged in” if the template could retrieve a user ID - i.e. if the user is logged in.

Other useful tags you will see in templates:

  • {% else %} - used in conjunction with {% if %} to display content if its condition is not met
  • {% for %} - creates a loop while a certain condition is met; you’ll see this used in portal templates for displaying a list of items
  • {% include %} - includes the content of one template within another

See the official Twig documentation at http://twig.sensiolabs.org/doc/templates.html for more information about what tags can do.

Comments

You can leave comments to explain what your custom templates do. Comments will not affect how the template works; they are just for human reference.

{# this is a comment #}

When you’re troubleshooting a template, or trying to understand how the built-in templates work, it’s useful to comment out a section of code to quickly disable it, and see how the template behaves without it. For example, here’s

{# this comment wraps the code below
  {% for item in navigation %}
      <li><a href="{{ item.href }}">
      {{ item.caption }}</a></li>
  {% endfor %}
#}

Warning

Don’t use HTML comments within templates. If there are comments in a piece of inserted HTML, the two sets of comments can interact and mangle the intended HTML.

Comments (0)

Add a comment

Add a comment

You need to log in before you can submit a comment.

Need a password reminder?