Avoiding boilerplate code in Email Templates

Use Case:

In email templates, we will write HTML and CSS code to define the look and feel of emails to end users. In most cases, it would be the same code, except the body information, such as dynamic values. The rest will be the remains same across all email templates.

What if the same code is present in multiple files? It would be a little hard and time-consuming to update all emails.

Solution:

The approach I followed is to move all HTML code from the email template object and place it in HTML files (.html) then refer the HTML files in the email template object.

Steps to achieve lets assume email templates has header, body and footer.

  1. Most of the time Header & Footer are the same in all email templates. So, I have Created a HTML files for header and footer.
    image
  2. Next, Place your HTML files inside "identityiq\WEB-INF\classes"
  3. Now call your HTML files in you email templates using #include like below
  4. Simple Two lines of code which makes it easier to reuse in multiple email templates.

Example:

I have implemented a sample email template which includes, a brand logo, image and dynamic year calculation.

  • Header.html consists of Brand logo, Brand Image & Email classification
  • Footer.html consists of Resources/ Help points, Display current year.
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE EmailTemplate PUBLIC "sailpoint.dtd" "sailpoint.dtd">
    <EmailTemplate name="testEmail">
      <Body>
        #include("html-header.html")
    
        &lt;p> place your logic here in email template object &lt;/p>
    
        #parse ("vtl-common.vtl")
        #parse("html-footer.html")
      </Body>
      <Description>
        Email Template for Testing
      </Description>
      <Subject>Test Email </Subject>
    </EmailTemplate>
    

Output:

Note: We can parse the HTML file as well. Here In footer.html I have included © $myarray.get(1) To fetch the current year value, I have written a logic in the rule and calling it from .vtl file like below

	#set( $sailpointContext=$spTools.class.forName("sailpoint.api.SailPointFactory").getMethod("getFactory", null).invoke(null, null).getCurrentContext() ) 
    
	#set( $ruleYear = $sailpointContext.getObjectByName($spTools.class.forName("sailpoint.object.Rule"), "Current-Year") )  
    #set( $myarray = $sailpointContext.runRule($ruleYear, $null) )

Pros:

  1. Code Reusability
  2. Maintainability
  3. Avoid Code Duplication
  4. Writing Shorter Codes.
  5. Hiding Business Logic

Cons:

  1. Increased complexity, especially when it not working properly
  2. Difficult to understand If it is a large code.

Reference:

To know more about #include
To know more about #parse

4 Likes