How to use Template Engine to Render Complex Templates - Part 1

How to use Template Engine to Render Complex Templates - Part 1

Rob BeckerRob Becker Member
edited August 2020 in Siemplify Best Practices

I have recently created Template Engine, an integration available to the Community which utilizes Jinja2 to render templates for usage in Siemplify.  Jinja2 is an extremely powerful templating language and can be used to make aesthetically pleasing HTML pages for use in Siemplify Insights, Emails, and anything benefiting from template usage.  

The Template Engine integration includes two actions: Render Template and Entity Insight.  The Render Template action will render a single template using the input JSON string.  The resulting template can be used in Emails, ticketing systems, or any action accepting an input string.  The Entity Insight action will create individual entity insights based upon the entities in the action scope and the input JSON string.  

In this blog post, we are going to show you how to use the Render Template action along with the Get Case Data action from SiemplifyTools, to produce an HTML email that contains details from the case as well as all of the favorited comments from the case wall.

The Template Engine integration uses Jinja2 to render templates.  Creating a basic Jinja template is easy, but Jinja2 is extremely powerful and advanced templates are beyond the scope of this walkthrough.  For a full explanation of Jinja templating can be found at the following URL:

https://jinja.palletsprojects.com/en/2.11.x/

The first step will be to make sure you have SiemplifyTools and TemplateEngine integrations installed and updated.  Configure both of these in your Shared Environment.  

The second step will be to create the template that will be rendered.  The template used by Render Template can contain Jinja2 style templating and Siemplify placeholder style templating.  The Jinja2 style utilizes curly braces { } and Siemplify uses square brackets [ ].  By using the combination of templating formats, you are able to create very rich documents.  

The Siemplify placeholders gives you access to all of the data values available through the “Placeholder” dialog in Playbook builder.  Examples of these values are: [ Environment.Name ], [ Case.Id ], [ Entity.identifier ], etc.  

These values can also be used WITHIN Jinja2 templates.  If you want to iterate over the entities, you can use [ Entity.identifier ] as an input for a FOR loop in Jinja2. There will be an example of this further down.

The Jinja2 template receives it’s data through the dictionary object ‘input_json’.  This variable, ‘input_json’ can be used within the template to output the desired information.

In the following example, we’ll create a template that uses both Jinja2 and Siemplify formats.  This template will summarize the case and can be used as a closing email to a customer or user.

First we use Siemplify placeholders to extract the name of the Environment.  

Now we create a table that is going to contain the information about the case.

Here is some of the Jinja2 magic.  We want the title <td> to include the list of alert rule generator values in the case.  To do that, we are going to loop through all of the alerts in the input_json, case, and append the description of that alert to a list.  After we accumulate the alert_descs list, we are going to use a Jinja filter to only return the unique values chained to another filter to separate the list with a comma. 

Now that we have the title, we want to output some more details about the case, the ID and the time it was created.

Conditionals within Jinja can be used to alter the output of templates.  Below, we are using a combination of both Jinja and Siemplify Placeholders to output a string based on the case priority.  The [Case.Priority] will be rendered first and then the Template Engine will render conditional. 

-------------------------------------

The final section will collect all the user created comments and insights and add them to a list.  Using a conditional, we can restrict the output to only render if there are items in the list.

If we were to view this template as HTML, without any Jinja or Siemplify template rendering, it would look like this:

Continue reading the second post (part 2) for more information on How to use Template Engine to Render Complex Templates.

Enjoy!

Sign In or Register to comment.