我可以在 StringTemplate 中使用字符串分隔符吗?如果没有,什么是好的模板引擎?

发布于 2024-12-18 06:39:45 字数 644 浏览 9 评论 0原文

我需要一个简单的模板引擎,它只进行变量名称替换(我不需要其他功能),并且可以配置为使用字符串而不是字符的分隔符。例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝戈者 2024-12-25 06:39:45

您可以使用模板中未使用的 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()

怪我入戏太深 2024-12-25 06:39:45

我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文