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.

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 Groovy 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.

    {% macro addDays(from, nod) %}
        {% set weeks = nod//5 %}
        {% set remDays = nod%5 %}
         
        {#- Adding the whole weeks #}
        {%- set from = from | date('add', weeks,'weeks') %}
         
        {#- Run a loop and check each day to skip a weekend #}
        {%- for i in range(0,remDays) %}
        {% set from = from | date('add',1,'d') | date %}
        {% if from | date('day') == "6" %}
              {% set from = from | date('add',1,'d') | date %}
          {% endif %}
          {% if from | date('day') == "0" %}
              {% set from = from | date('add',1,'d') | date %}
          {% endif %}
        {% endfor %}
         
        {#- Skipping the ending weekend #}
        {%- if from | date('day') == "6" %}
            {% set from = from | date('add',1,'d') %}
        {% endif %}
        {% if from | date('day') == "0" %}
            {% set from = from | date('add',1,'d') -%}
        {% endif -%}
         
        {#-Return the Momentjs#}
        {{-from | date}} 
    {% endmacro -%}
    {% macro daysFromPriority(priority) %}
        {% if priority == "Highest" %}
        	2
        {% elseif priority == "High"%}
        	3
        {% elseif priority == "Medium"%} 
        	4
        {% elseif priority == "Low"%} 
        	5
        {% else %}
        	5
        {% 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.


    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 :
    {%- import "addDateExcludingWeekends" as AD %}
    {%- set days = AD.daysFromPriority(issue.fields.priority.name) %}
    {{-AD.addDays(issue.fields.created,days)}}
    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" %}