Our new Appfire Documentation Space is now live!
Take a look here! If you have any questions please email support@appfire.com
Shared Nunjucks Templates
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 tester.
On this page:
Creating a shared Nunjucks template
- Log in to your Jira Cloud instance as an administrator.
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Shared Nunjucks templates.
- Provide a name for the Nunjucks template in
Name
. - You can optionally provide a description for the Nunjucks template in
Description
. - Click on
Add
. The Nunjucks template editor is displayed. - Write a template in the editor.
- You can optionally test it against any issue using the Nunjucks script tester.
- Click on
Save
.
Editing a shared Nunjucks template
- Log in to your Jira Cloud instance as an administrator.
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Shared Nunjucks templates.
- Click on
Edit
for the template you wish to modify. - Modify it and click on
Save
to save your changes.
Deleting a shared Nunjucks template
- Log in to your Jira Cloud instance as an administrator.
- Go to the Administration icon and click on it.
- Locate Add-ons from the menu and click on it.
- Locate JIRA MISC WORKFLOW EXTENSIONS on the left panel.
- Click on Shared Nunjucks templates.
- 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.
Limitations of macros
Nunjucks macros have two limitations that you need to be aware of:
- 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. - 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.
- Go to Shared Nunjucks templates in the JMWE administration pages.
- Name the template as
Mappings
. 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 -%}
- To access the template in a post-function, go to the configuration of the post-function.
Write the following template in the editor.
{% import "Mappings" as Mappings %} {{Mappings.priorityFromImpact(issue.fields.Impact)}}
- 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:
- Go to Shared Nunjucks templates in JMWE administration pages.
- Name the template as
addDateExcludingWeekends
. 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() }}
- To access the template in a post-function, go to the configuration of the post-function.
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:
- Go to Shared Nunjucks templates in JMWE administration pages.
- Create a new shared template to hold the HTML body of an email and name it
emailHtmlBody
In the HTML Body field of the Email Issue post-function, write the following template:
{% include "emailHtmlBody" %}