Match Replace – vRO Template handling

Using templates basically has the following advantages: – standardization – centralization – atomic template handling In this post an example with an eMail template is used. Of course, the method also works with scripts or similar tempaltes which take placeholders as variables and should be replaced at runtime. In relation to eMail templates it is of course possible to nested, i.e. first a general template that defines the framework, then an inner part which is specifically adapted to the notification.

Sample template

First of all we need a template. All placeholders / variables are placed in the {{variable_name}} style.

<!doctype html>
<html>
    <head></head>
    <body>
        <h1>{{title}}</h1><br>
        <p>{{notification}}</p>
    </body>
</html>

Build better eMail templates with e.g. https://topol.io

Call matchReplace

In this snippet a variable of the type Properties is created, this contains the variable name as key and the content as value.

resourceName = "email-notification";
resourcePath = "vbounty/";
properties = new Properties();

properties.put("title","New User Interaction");
properties.put("notification","Dear User <br>You've got a new user interaction which needs your attention. To answer please click followed link: " + link);

content = System.getModule("ch.vbounty.misc").matchReplace(resourcePath,resourceName,properties);

Action matchReplace

the match replace method will first load the content of the supplied resource element. All placeholders are loaded using Regex and replaced with the supplied property values.

/**
 * Returns content of a resource element. Provided resource element will be search and replaced for variables like {{foo}}. Values to be replaced needs to be delivered as key/value in properties. Key is exactly the name of the variable. Value holds the string to be inserted.
 * @param {String} resourcePath //Path to resource element category
 * @param {String} reosurceName //Resource element name
 * @param {Properties} properties //Key/Value Store: key needs to be exactly like the placeholder/variable from the resource element. Value holds the string be inserted.
 * @return {String}
 */
var content = undefined;

//Fetch resource element
var category = Server.getResourceElementCategoryWithPath(resourcePath);
for each (var resourceElement in category.resourceElements) {
	if (resourceElement.name == resourceName) {
		content = resourceElement.getContentAsMimeAttachment().content;
		break;
	}
}
if(!content) throw "No resoucre element found for path: "+resourcePath+", and name: "+resourceName;

//gathering variables e.g. {{foo}}
var pattern = new RegExp(/\{\{[a-zA-Z]*\}\}/g);
var matches = content.match(pattern);

//replace variables
for each (var match in matches){
	var key = match.substring(2,match.length-2);
	System.debug("replacing match: " + match + ", with value: "+properties[key]);
	content = content.replace(match,properties[key]);
}

System.debug("new refined content: \n" + content);
return content;

Leave a comment

Your email address will not be published. Required fields are marked *