Skip to content

Instantly share code, notes, and snippets.

@volyx
Last active January 16, 2018 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volyx/86fadc74ecc51b947863e9afd67567d2 to your computer and use it in GitHub Desktop.
Save volyx/86fadc74ecc51b947863e9afd67567d2 to your computer and use it in GitHub Desktop.
Fully extendable java html template engine
// regex for uri template pattern
public static Pattern TEMPLATE_PATTERN = Pattern.compile("\\{(.*?)\\}");
public static String templateReplace(String template,
Map<String, Object> properties) {
String result = template;
try {
if (template != null && template.contains("{")
&& template.contains("}")) {
Matcher m = TEMPLATE_PATTERN.matcher(template);
while (m.find()) {
String param = m.group(1);
if (properties.containsKey(param)) {
// replace template tokens
result = template.replaceAll(
"\\{" + Pattern.quote(param) + "\\}",
properties.get(param).toString());
}
}
}
} catch (Exception e) {
logger.error("An error occurred while replacing tokens in ["
+ template + "]", e);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment