我可以在 StringTemplate 中使用字符串分隔符吗?如果没有,什么是好的模板引擎?
我需要一个简单的模板引擎,它只进行变量名称替换(我不需要其他功能),并且可以配置为使用字符串而不是字符的分隔符。例如
new Template("Hello {{topic}}")
.add("topic", "world")
.render()
应该返回“Hello world”(java代码可以改变,我更关心模板语法)
我想要多字符分隔符的原因是我有几个可能包含各种字符的模板,并且想确保模板化文档的本地语言(html、css、js)不会与模板引擎发生冲突。
在阅读这篇文章之后,我尝试使用StringTemplate,但我不确定它是否支持超过一个字符长的分隔符(ST< 上的构造函数/代码> 似乎接受字符分隔符而不是字符串)。
StringTemplates 支持多字符分隔符吗?如果没有,是否有其他与我上面描述的模板语法一起使用的简单模板引擎的建议?
I need a simple template engine, that only does variable names replacement (I don't need other features), and can be configured to use delimiters that are strings, not characters. E.g.
new Template("Hello {{topic}}")
.add("topic", "world")
.render()
Should return "Hello world" (the java code can change, I care more about the template syntax)
The reason I want multi-character delimiters is that I have several templates that may contain all kinds of characters, and would like to make sure the native languages of the templated documents (html, css, js) don't collide with the template engine.
After reading this post, I tried to using StringTemplate, but I'm not sure if it supports delimiters that are more than one character long (The constructor on ST
seems to accept character delimiters instead of strings).
Does StringTemplates support multi-character delimiters? If not, any recommendations of another simple template engine that works with the template syntax I described above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用模板中未使用的 unicode 字符。
new ST("Hello «prop»", '«', '»').add("prop", "xxxxxx").render()
You could use a unicode character that isn't used in your templates.
new ST("Hello «prop»", '«', '»').add("prop", "xxxxxx").render()
我认为 Apache Velocity 是最好的模板引擎。我们用它来做很多事情:生成动态 HTML(而不是 JSP)、生成 XML 而不是 XSLT 等。
I think Velocity by Apache is the best templating engine out there. We use it for lots of things: generating dynamic HTML (instead of JSP), XML generation instead of XSLT, etc.