Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Tags are special blocks that perform operations on sections of the template and Expressions are literal expressions similar to those in javascript.

On this page:

Table of Contents

You might want to look here for some more built-in tags and expressions in Nunjucks.

Tags

set

The set tag creates or modifies a variable.

...

{% set comments = issue.fields.comment.comments %} sets the comments variable with all the comment objects of the issue.

setContextVar

The setContextVar tag creates or modifies a context variable. Context variables are used to pass information between post-functions in a Sequence of post-functions, a Shared Action or a Scheduled Action. Context variables are available through the transition.context global variable. For example, if you create a context variable myVar in the first post-function of the sequence:

Code Block
{% setContextVar myVar = "a value" %}

This variable will then be available to subsequent post-functions as:

Code Block
{{ transition.context.myVar }}

if

The if tag tests a condition.

For example:

...

returns This is a task if and only if the issue type is "Task".

for

The for tag iterates over an array of values or objects. 

...

iterates over the comments variable and displays each comment body.

include

The include tag pulls in other templates in place. It It's useful when you need to share smaller chunks across several templates. An  An included template does not participate in the block structure of its including template; it has a totally separate inheritance tree and block namespace. In other words, an include is not a an include is not a pre-processor that pulls the included template code into the including template before rendering; instead, it fires off a separate render of the included template, and the results of that render are included.

Syntax:

Code Block
{% include "Test" %}

where Test is the name of the shared Nunjucks template saved in the Admin page.

import

The import tag loads a template and allows you to access its exported values. Macros and top-level assignments are assignments are exported from templates, allowing you to access them in your current template.

...

Code Block
{% import "Mappings" as Mappings %}
{{Mappings.priorityFromImpact(issue.fields.Impact)}}

Whitespace control

The Nunjucks script outputs everything outside of variable and tag blocks verbatim, with all the whitespace (spaces, tabs, newlines,...) as it is in the script. For example, the following script:

Code Block
linenumberstrue
This issue is
{% if issue.fields.priority.name == "Blocker" %}
  urgent
{% else %}
  not urgent
{% endif %}

will result in the following output:

linenumbers
Code Block
true
This issue is
  not urgent

If you don't want the extra whitespace between Nunjucks tags, then you can add the minus the minus sign (-) to the start or end block to strip leading or trailing whitespace. For example:

Code Block
linenumberstrue
This issue is
{%- if issue.fields.priority.name == "Blocker" %}
 urgent
{%- else %}
 not urgent
{%- endif %}

will result in the following output:

true
Code Block
linenumbers
This issue is not urgent

See the Nunjucks documentation for more details.

Info

We already automatically remove spaces and carriage returns surrounding template result values in most post-functions.

Expressions

Mathematical expressions

Nunjucks allows you to do simple calculations on numbers.

...

{{ OriginalEstimate*2 }} doubles the original estimate of the issue.

Comparisons

Nunjucks allows you to compare two values or objects.

For example:

{{ issue.fields.issuetype.name == "Task" }} compares both values.

If

Nunjucks allows you to use if as an inline expression.

...