Skip to content

Commit 1b70a62

Browse files
author
Igor Polevoy
committed
#541 Create a simple mechanism for merging small templates in code
1 parent 11502f7 commit 1b70a62

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2009-2016 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package org.javalite.common;
18+
19+
20+
import java.util.Map;
21+
22+
import static org.javalite.common.Util.readResource;
23+
24+
/**
25+
* Simple implementation of small quick templates filled with dynamic data.<br/>
26+
* An example of a template:
27+
*
28+
*
29+
* <pre>
30+
* Hello, {{name}}!
31+
* </pre>
32+
*
33+
*
34+
* Usage:
35+
*
36+
* <pre>
37+
* Templator templator = new Templator("/hello_template.txt");
38+
* System.out.println(templator.merge(map("name", "Kyla"))); // prints: Hello, Kyla!
39+
* </pre>
40+
*
41+
* <p></p>
42+
* Note: Yes, I'm aware of existence of Mustache, Freemarker, Velocity, etc.
43+
* This implementation is for quickly loading small snippets of text merged with data.
44+
* The goal is to eliminate code pollution. If you need more power, use bigger frameworks.
45+
*
46+
* @author Igor Polevoy on 9/30/16.
47+
*/
48+
public class Templator {
49+
50+
private String template;
51+
52+
/**
53+
* @param templatePath path to a template on classpath
54+
*/
55+
public Templator(String templatePath){
56+
template = readResource(templatePath);
57+
}
58+
59+
/**
60+
* This method is used in repeated operations, since it will load a resource once.
61+
*
62+
* @param values values to merge into a template
63+
* @return result of merging
64+
*/
65+
public String merge(Map<String, ?> values){
66+
return mergeFromTemplate(template, values);
67+
}
68+
69+
/**
70+
* This method is used in one-off operations, where it is OK to load a template every time.
71+
*
72+
* Example:
73+
* <code>
74+
* String result = Templator.mergeFromPath(readResource("/message_template.txt", valuesMap));
75+
* </code>
76+
*
77+
* @param templatePath template to merge
78+
* @param values values to merge into a template
79+
* @return result of merging
80+
*/
81+
public static String mergeFromPath(String templatePath, Map<String, ?> values) {
82+
return mergeFromTemplate(readResource(templatePath), values);
83+
}
84+
85+
/**
86+
* Merges from string as template.
87+
*
88+
* @param template template content, with placeholders like: {{name}}
89+
* @param values map with values to merge
90+
*/
91+
public static String mergeFromTemplate(String template, Map<String, ?> values) {
92+
for (String param : values.keySet()) {
93+
template = template.replace("{{" + param + "}}", values.get(param) == null ? "" : values.get(param).toString());
94+
}
95+
return template.replaceAll("\n|\r| ", "");
96+
}
97+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javalite.common;
2+
3+
import org.junit.Test;
4+
5+
import static org.javalite.common.Collections.map;
6+
import static org.javalite.test.jspec.JSpec.a;
7+
8+
/**
9+
* @author igor on 9/30/16.
10+
*/
11+
public class TemplatorSpec {
12+
13+
@Test
14+
public void shouldMergeTemplateWithTemplatorInstance(){
15+
Templator t = new Templator("/templator/hello.txt");
16+
a(t.merge(map("name", "Igor", "greeting", "Hi"))).shouldBeEqual("Hi, Igor");
17+
}
18+
19+
@Test
20+
public void shouldMergeTemplateWithStaticTemplator(){
21+
a(Templator.mergeFromPath("/templator/hello.txt", map("name", "Igor", "greeting", "Hi"))).shouldBeEqual("Hi, Igor");
22+
}
23+
}
24+
25+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{greeting}}, {{name}}

0 commit comments

Comments
 (0)