Java Forum / General / August 2007
Template engine
Artur Siekielski - 14 Aug 2007 13:18 GMT Hello, I'm looking for a template engine with several characteristics: 1. Variables used in a template should be declared in template. Or the engine should be able to parse a template and tell me what variables are used. 2. Variables should have types, ie. an author of a template specifies that some variable is of type 'integer' and some is of type 'enum(one, two, three)'. 3. Templates must not be precompiled, they should be parsed dynamically. By "variables" I mean holes that are filled with data taken from model.
I haven't found an engine fitting my needs. http://www.jamon.org/ is close but it requires template precompiling. One option is to use one of popular engine, like Velocity, and gain 1. by parsing template by hand and 2. by using variable names convention. Another option is to use XSLT, but achieving 1. is not simple.
If you know any better solution, please tell me,
Best regards, Artur
Daniel Pitts - 14 Aug 2007 20:53 GMT On Aug 14, 5:18 am, Artur Siekielski <artur.siekiel...@gmail.com> wrote:
> Hello, > I'm looking for a template engine with several characteristics: [quoted text clipped - 19 lines] > Best regards, > Artur My advice: Give up on strict typing of template variables, it doesn't buy you much at all. After that, you are free to use Velocity or Freemarker, or build your own.
Joe Attardi - 14 Aug 2007 21:26 GMT > My advice: Give up on strict typing of template variables, it doesn't > buy you much at all. After that, you are free to use Velocity or > Freemarker, or build your own. I have to agree with Daniel. There is little value IMHO of strict typing of the template variables. I have worked with Velocity on several projects, and it is a joy to work with (except for the damn whitespace... anyone who has worked with Velocity will know what I mean here)
 Signature Joe Attardi jattardi@gmail.com
Artur Siekielski - 14 Aug 2007 22:25 GMT > My advice: Give up on strict typing of template variables, it doesn't > buy you much at all. After that, you are free to use Velocity or > Freemarker, or build your own. Strict typing is a crucial thing for me. I have to dynamically render GUI which allows setting template variables values. Variables values can be numbers, strings, dates, enums. Defining type of a variable is crucial for rendering proper input box and do validation. Or, variables must be self-describing in other way (like using variables name convention).
Lew - 14 Aug 2007 22:54 GMT Daniel Pitts wrote:
>> My advice: Give up on strict typing of template variables, it doesn't >> buy you much at all. After that, you are free to use Velocity or >> Freemarker, or build your own.
> Strict typing is a crucial thing for me. I have to dynamically render > GUI which allows setting template variables values. Variables values > can be numbers, strings, dates, enums. Defining type of a variable is > crucial for rendering proper input box and do validation. Or, > variables must be self-describing in other way (like using variables > name convention). I'm curious - what will templating provide that JSP (say, with JSF) doesn't offer?
 Signature Lew
Daniel Pitts - 15 Aug 2007 16:26 GMT On Aug 14, 2:25 pm, Artur Siekielski <artur.siekiel...@gmail.com> wrote:
> > My advice: Give up on strict typing of template variables, it doesn't > > buy you much at all. After that, you are free to use Velocity or [quoted text clipped - 6 lines] > variables must be self-describing in other way (like using variables > name convention). Or perhaps they can all be wrappers that contain that information dynamically.
class Variable<T> { T value; Validator<? super T> validator; RenderType renderType; };
I would think that this is the most useful and flexible approach, but maybe thats just me.
Artur Siekielski - 15 Aug 2007 19:07 GMT > Or perhaps they can all be wrappers that contain that information > dynamically. [quoted text clipped - 8 lines] > I would think that this is the most useful and flexible approach, but > maybe thats just me. This approach facilitates simulation of declaration of types: <template-code> set $i.type = 'integer' set $e.type = 'enum(one, two, three)'
You set i to $i.value and e to the number $e.value </template-code>
It's quite good, but using '$i.value' instead of '$i' is error prone and a it's a kind of hack (template code must be as simple as possible, because it will be created by nonprogrammers). But it's a very nice idea, thanks. I'm also considering XSLT, it would look like that: <template-code> You set i to <xsl:value-of select="//var[@name='i' and @type='integer']"/> and e to the number <xsl:value-of select="// var[@name='e' and @type='enum(one, two, three)']"/>. </template-code> But verboseness of XML is very discouraging...
I also have to parse template and get names and types of variables. When using XSLT it's a bit easier, because I can parse it with XML and XPath parsers. I don't know if Velocity or Freemarker have parsers accessible from user code...
Daniel Pitts - 16 Aug 2007 16:17 GMT On Aug 15, 11:07 am, Artur Siekielski <artur.siekiel...@gmail.com> wrote:
> > Or perhaps they can all be wrappers that contain that information > > dynamically. [quoted text clipped - 33 lines] > XPath parsers. I don't know if Velocity or Freemarker have parsers > accessible from user code... or, if the type is really determined at the template level (instead of at the model level passed too the template), then perhaps what you really want is to be able to update the model from the template, so that the template can set the "type" of the variable...
Also, if you care about the difference between e.value and e...
public String toString() { return renderType.render(value); }
Roedy Green - 14 Aug 2007 22:01 GMT >If you know any better solution, please tell me, another template engine is FreeMarker. I don't think it is compiled. what's the problem with compiling? see http://mindprod.com/jgloss/templateengine.html There are not many of them.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Artur Siekielski - 14 Aug 2007 22:45 GMT On Aug 14, 11:01 pm, Roedy Green <see_webs...@mindprod.com.invalid> wrote:
> what's the problem with compiling? Templates will be created by non-programmers after application deployment, so my application must read templates dynamically.
> There are not many of them. Yes, but all, I think, are center towards usage in web apps, where template's arguments and their meaning are known at the time the application is developed. In this scenario a template is a slave of the model encoded in Java. I would like to achieve the opposite.
Artur Siekielski - 14 Aug 2007 22:49 GMT On Aug 14, 11:01 pm, Roedy Green <see_webs...@mindprod.com.invalid> wrote:
> There are not many of them. Sorry, I have read it as "There are many of them". And I have found plenty of template engines :). See http://www.java-source.net/open-source/template-engines
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|