Allow to retrieve variables from a template
Hello,
We use a template system and send variables from a Java backend. To make it dynamic, we retrieve the template and analyze the detailcontent: Text-part to extract the variables, and then send them. An improvement would be to directly retrieve the variables.
What I would like is to be able to retrieve the variables used inside a template email, for instance when using the java mailjet client, I would like a new enum called Variables and do somethind like that:
public class TemplateDetailcontent {
public static Resource resource = new Resource("template", "detailcontent", ApiVersion.V3, ApiAuthenticationType.Basic);
public static String TEXTPART = "Text-part";
public static String HTMLPART = "Html-part";
public static String MJMLCONTENT = "MJMLContent";
public static String HEADERS = "Headers";
// ADDING new field to retrieve variables
public static String VARIABLES= "Variables";
}
For now I am parsing Text -part and use the folllowing regex to retrieve variables:
private List<String> getTemplateVars(long templateId) throws MailjetException {
List<String> variables = new ArrayList<>();
// Retrieve template
MailjetRequest reqTemplate= new MailjetRequest(TemplateDetailcontent.resource, templateId);
MailjetResponse respTemplate = getMailClient(this.apiKey, this.apiSecretKey).get(reqTemplate);
if (respTemplate.getStatus() == 200) {
JSONArray jArr = respTemplate.getData();
if (jArr.length() > 0) {
JSONObject jObj = jArr.getJSONObject(0);
String textPart = jObj.getString(TemplateDetailcontent.TEXTPART);
Pattern pattern = Pattern.compile("\{\{var:(\w+)(?::\w+)?}}");
Matcher matcher = pattern.matcher(textPart);
while (matcher.find()) {
variables.add(matcher.group(1));
}
}
}
log.info("vars: {}", variables);
return variables;
}