我正在使用StringTemplate V4在我的项目中生成一些HTML代码。 我需要在我的模板中使用HTML格式,因此使用默认分隔符<
和>
会非常尴尬。如何获取StringTemplate V4忽略<作为分隔符?
所以,我创建了一个组作为参数传递分隔符(由this question推荐),但它根本不起作用。
这里是我的测试代码:
public void testTemplate() {
char sep = '$';
STGroup stGroup = new STGroupString("temp",
"<html>hello, $name$!</html>", sep, sep);
System.out.println("Group created");
ST st = stGroup.getInstanceOf("temp");
if (st == null) {
System.out.println("Failed to get template!");
} else {
st.add("name", "Guest");
System.out.println("Template initialized correctly");
}
}
这是我得到的输出:
temp 1:1: invalid character '<'
temp 1:5: invalid character '>'
temp 1:1: garbled template definition starting at 'html'
temp 1:6: garbled template definition starting at 'hello'
temp 1:13: invalid character '$'
temp 1:18: invalid character '$'
temp 1:19: invalid character '!'
temp 1:21: invalid character '<'
temp 1:22: invalid character '/'
temp 1:14: garbled template definition starting at 'name'
temp 1:26: invalid character '>'
temp 1:22: garbled template definition starting at 'html'
Failed to get template!
缺少什么我在这里?
它只适用于模板,但我真的需要使用该组,因为在真正的代码中我需要注册渲染器。 – elias