java StringTemplate 和 FreeMarker 之间的主要区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3741618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the main differences between StringTemplate and FreeMarker?
提问by Nakedible
The net seems to be full of comparisons between Velocity and FreeMarker, and they seem to be reasonably equivalent. But there seems to be almost no comparisons between StringTemplate and FreeMarker!
网上似乎充满了 Velocity 和 FreeMarker 之间的比较,而且它们似乎相当等效。但是在 StringTemplate 和 FreeMarker 之间似乎几乎没有可比性!
So, what are the main differences between StringTemplate and FreeMarker?
那么,StringTemplate 和 FreeMarker 之间的主要区别是什么?
My usage for them would be to generate HTML pages only. Out of the two, I would've expected FreeMarker to be the more suitable and more capable, as that seems to the more common one - but through a quick glance it seems that StringTemplate actually has more suitable features!
我对它们的用法是仅生成 HTML 页面。在这两者中,我原本希望 FreeMarker 更合适、功能更强大,因为这似乎更常见 - 但通过快速浏览,StringTemplate 似乎实际上具有更合适的功能!
It would be great if someone who has used both would have time to comment - for the actual specifics, I can just read the documentation side by side, but I'd like something to get started with.
如果同时使用两者的人有时间发表评论,那就太好了 - 对于实际细节,我可以并排阅读文档,但我想要一些开始。
回答by Terence Parr
I designed ST to build jGuru after getting sick of the "code in template" model of JSP. Velocity and friends (i.e., every other engine i think) give you more power than you need. I used basically four features to build jGuru.com (as described in paper). More features are unnecessary and lead you to entangle your model into the template. If you're building a one-off prototype, that's ok and any engine is fine. Code in templates is ok in that case since you don't care about maintenance.
在厌倦了 JSP 的“模板代码”模型后,我设计了 ST 来构建 jGuru。速度和朋友(即我认为的所有其他引擎)为您提供比您需要的更多的动力。我基本上使用了四个功能来构建 jGuru.com(如论文中所述)。更多的功能是不必要的,并导致您将模型纠缠到模板中。如果您正在构建一次性原型,那没关系,任何引擎都可以。在这种情况下,模板中的代码是可以的,因为您不关心维护。
Another thing people seem to ignore: how the hell can a graphics designer read code in templates? They can't. So, how can they work on the templates then? Even changing the order of some elements can break your model. You really need the separation to work in commercial environment, unless you want a site that looks like a coder built the html ;)
人们似乎忽略的另一件事是:图形设计师怎么能阅读模板中的代码?他们不能。那么,他们如何处理模板呢?即使更改某些元素的顺序也会破坏您的模型。您确实需要分离才能在商业环境中工作,除非您想要一个看起来像编码人员构建 html 的站点;)
回答by lujop
The main difference is that StringTemplate strictly enforces model-view separation and you can't put logic in the templates and FreeMarker lets you put full of logic in the templates.. This at least at the beginning make it more difficult to use but it's more scalable. For example if you are generating code and you have no logic in the templates generating another port for another language is less tedious because you haven't to replicate logic in each template.
主要区别在于 StringTemplate 严格执行模型视图分离,您不能在模板中放置逻辑,而 FreeMarker 允许您在模板中放置完整的逻辑.. 这至少在开始时使其更难使用,但它更多可扩展。例如,如果您正在生成代码并且模板中没有逻辑,则为另一种语言生成另一个端口就不那么乏味了,因为您不必在每个模板中复制逻辑。
There's a paperfrom Terence Parr that explains the benefits from model-view separation
Terence Parr的一篇论文解释了模型视图分离的好处