Shared Nunjucks Templates

JMWE for Jira Cloud provides support for shared Nunjucks templates. A shared template can be used across many post-functions, and updating a shared template through the administration page will update the template everywhere it is used, increasing maintainability and efficiency.

A shared Nunjucks template can also be used to implement lengthy templates - longer than those normally possible inside post-functions.

The sections below explain how to create shared Nunjucks templates and reuse them from JMWE post-functions or the Nunjucks tester

Creating a shared Nunjucks template

 

  1. Log in to your Jira Cloud instance as an administrator.

  2. Click the Settings icon  in the upper right corner and select Apps.

  3. Under JIRA MISC WORKFLOW EXTENSIONS in the left-hand panel, click Shared Nunjucks templates.

  4. At the top of the list of shared templates, click Create new template.

  5. The Shared Nunjucks Template Editor (Figure 2, right) will open.

  6. Configure and build the template:

    1. Template name - Give the template a name.

    2. Description - Give the template a description, if desired.

    3. Nunjucks template - Enter the Nunjucks template.

  7. Optionally, you can test the template against any issue using the Test Nunjucks template… button in the upper right corner of the editor.

  8. Click Save.

Working with Templates

Within the list of Shared Nunjucks templates (Figure 1, right), there are several tools for sorting, filtering and working with your templates:

  • Use the Filter button ( filter.png ) to filter by any column.

  • Use the Sort button ( sort.png ) to sort a column.

  • Use the Action button ( click Edit to the right of the Shared Template name.

  • To delete a template, click Delete to the right of the Shared Template name.

Import/Include a Nunjucks template

You can import or include a shared Nunjucks template into your post-functions or Script tester using the respective tags available in Nunjucks.

Limitations of macros

Nunjucks macros have two limitations about which you need to be aware:

  1. Macros do not have access to the global variables of the calling Nunjucks template (such as issue, currentUser, etc.) If you need to access these variables from the body of the macro, you need to pass them as parameters to the macro.

  2. Macros cannot execute asynchronous code. This means that any filter that calls the Jira API will fail when used inside a macro, and the failure is silent (no error message). This is a major limitation of Nunjucks macros. The workaround is not to use macros and instead include Shared Templates.

  3. Macros cannot be used inside an {% if %} block. When a macro needs to be used in an if block, use ifAsync instead. For example, instead of:

    {% if true %} {% import "getReporter" as Reporter %} {{ Reporter.getReporter("Test",1) }} {% endif %}

    Use:

    {% ifAsync true %} {% import "getReporter" as Reporter %} {{ Reporter.getReporter("Test",1) }} {% endif %}

Examples

Example 1: To set the Priority of the issue based on its impact.

  1. Go to Shared Nunjucks templates in the JMWE administration pages.

  2. Enter Mappings into the Name field and click Add.

  3. Provide the following template in the Nunjucks editor.

    {% macro priorityFromImpact(impact) %} {%- if impact == "Company wide" %} Highest {% elseif impact == "More than one project" %} High {% elseif impact == "Single project" or impact == "Individual" %} Medium {% else %} Low {% endif %} {% endmacro -%}

     

  4. To access the template in a post-function, go to the configuration of the post-function.

  5. Write the following template in the editor.

     

  6. You can test the written template against any issue using the Nunjucks tester and verify the result.

Example 2: To provide a resolution date excluding the weekends and based on the priority of the issue:

  1. Go to Shared Nunjucks templates in JMWE administration pages.

  2. Enter addDateExcludingWeekends in the Name field and click Add.

  3. Provide the following template in the Nunjucks editor.

     

  4. To access the template in a post-function, go to the configuration of the post-function.

  5. Write the following template in the editor.

     

Example 3: To include a shared email body template in an Email Issue post-function:

  1. Go to Shared Nunjucks templates in JMWE administration pages.

  2. Create a new shared template to hold the HTML body of an email and name it emailHtmlBody

  3. In the HTML Body field of the Email Issue post-function, write the following template:

     

You are viewing the documentation for Jira Cloud.

JMWE Administration page listing Shared Nunjucks Templates
Figure 1 - Shared Nunjucks Templates
Figure 2 - Shared Nunjucks Template Editor