Template engine

A

Artur Siekielski

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
 
D

Daniel Pitts

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

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.
 
J

Joe Attardi

Daniel said:
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)
 
A

Artur Siekielski

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).
 
A

Artur Siekielski

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.
 
L

Lew

Artur said:
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?
 
D

Daniel Pitts

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).

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.
 
A

Artur Siekielski

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.

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...
 
D

Daniel Pitts

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.

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...


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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top