4

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?

Community
  • 1
  • 1
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • Not sure about multi character for StringTemplate but using '<>' pairs in non HTML/XML, and '$' delimiters in HTML/XML works well in my experience. – I82Much Nov 27 '11 at 16:42
  • @I82Much What about `a < b` in javascript? – ripper234 Nov 27 '11 at 16:43
  • I actually just about always prefer the '$' notation as I find it collides very rarely with syntax in the languages I'm using. – I82Much Nov 27 '11 at 16:45
  • For StringTemplate questions, I find the mailing list a much better source of definitive info than StackOverflow, unfortunately - see http://www.antlr.org/pipermail/stringtemplate-interest/ – I82Much Nov 27 '11 at 16:49
  • @I82Much - thanks. For now, I think I'll just use $, but be careful to only apply the template on a small subset of my files, so it should be good enough. – ripper234 Nov 27 '11 at 16:57
  • Alright. I have been very happy with ST in the past. It looks pretty confusing now because a lot of the documentation is on older versions of the code. Good luck – I82Much Nov 27 '11 at 16:58
  • If all you're going to do is a few replacements, do you really want a complete templating engine? You could easily write a single `Template` class to do this. – Bart Kiers Nov 28 '11 at 13:47
  • @BartKiers - Yes I could. And I would need to test and debug it. – ripper234 Nov 28 '11 at 13:52
  • My point is that if it would only need to do such simple replacements, testing and debugging would be kept to a minimum. Perhaps it'd take less time than to ask here for suggestions and learn how to use an existing templating engine :) – Bart Kiers Nov 28 '11 at 14:09
  • @BartKiers - but learning to use something new is useful in by itself. Anyway... – ripper234 Nov 28 '11 at 14:33

2 Answers2

2

You could use a unicode character that isn't used in your templates.

new ST("Hello «prop»", '«', '»').add("prop", "xxxxxx").render()

Sriram Srinivasan
  • 1,255
  • 17
  • 16
2

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 2
    Can you by any chance give a code snippet that implements the template above, with custom delimiters? – ripper234 Nov 27 '11 at 16:25