JMWE for Jira Cloud provides support for shared Nunjucks templates. This is very useful to share macros across post-functions, as well as to split lengthy templates when you run into this issue: MWEC-159.

This page explains how to create shared Nunjucks templates and reuse them from other Nunjucks templates written in JMWE post-functions or the Nunjucks scripts tester


On this page:


Creating a shared Nunjucks template

  1. Log in to your Jira Cloud instance as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Locate Add-ons from the menu and click on it.
  4. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  5. Click on Shared Nunjucks templates.
  6. Provide a name for the Nunjucks template in Name.
  7. You can optionally provide a description for the Nunjucks template in Description.
  8. Click on Add. The Nunjucks template editor is displayed.
  9. Write a template in the editor.
  10. You can optionally test it against any issue using the Nunjucks script tester.
  11. Click on Save.

Editing a shared Nunjucks template

  1. Log in to your Jira Cloud instance as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Locate Add-ons from the menu and click on it.
  4. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  5. Click on Shared Nunjucks templates.
  6. Click on Edit for the template you wish to modify.
  7. Modify it and click on Save to save your changes.

Deleting a shared Nunjucks template

  1. Log in to your Jira Cloud instance as an administrator.
  2. Go to the Administration icon  and click on it.
  3. Locate Add-ons from the menu and click on it.
  4. Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
  5. Click on Shared Nunjucks templates.
  6. Click on Delete for the template you wish to delete.

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.

Nunjucks macros have two limitations that you need to be aware of:

  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.


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. Name the template as Mappings.
  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.

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


  6. You can test the written template against any issue using the Nunjucks script 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. Name the template as addDateExcludingWeekends.
  3. Provide the following template in the Nunjucks editor.

    {% set priority = issue.fields.priortiy.name %}
    {% if priority == "Highest" %}
    	{% set nod = 2%}
    {% elseif priority == "High"%}
    	{% set nod = 4%}
    {% elseif priority == "Medium"%} 
    	{% set nod = 6%}
    {% elseif priority == "Low"%} 
    	{% set nod = 8%}
    {% else %}
    	{% set nod = 6%}
    {% endif %}
    {{ from | date('businessAdd', nod ) | date() }}


  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.

    Hi {{issue.fields.reporter.displayName }},
    
    This is in response to the ticket : {{issue.key}} that you created with us on {{issue.fields.created | date("DD/MM/YYYY") }}. Your issue will be resolved on or before :{% set from = issue.fields.created %} {% include "addDateExcludingWeekends" %}
    
    
    Regards,
    {{ issue | projectInfo | field("lead.displayName")}}


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:

    {% include "emailHtmlBody" %}