我正在使用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