Template engine

Discussion in 'Java' started by Artur Siekielski, Aug 14, 2007.

  1. 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
     
    Artur Siekielski, Aug 14, 2007
    #1
    1. Advertising

  2. Artur Siekielski

    Daniel Pitts Guest

    On Aug 14, 5:18 am, Artur Siekielski <>
    wrote:
    > 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.
     
    Daniel Pitts, Aug 14, 2007
    #2
    1. Advertising

  3. Artur Siekielski

    Joe Attardi Guest

    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.


    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)


    --
    Joe Attardi
     
    Joe Attardi, Aug 14, 2007
    #3
  4. Artur Siekielski

    Roedy Green Guest

    Roedy Green, Aug 14, 2007
    #4
  5. > 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).
     
    Artur Siekielski, Aug 14, 2007
    #5
  6. On Aug 14, 11:01 pm, Roedy Green <>
    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, Aug 14, 2007
    #6
  7. Artur Siekielski, Aug 14, 2007
    #7
  8. Artur Siekielski

    Lew Guest

    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.


    Artur Siekielski wrote:
    > 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?

    --
    Lew
     
    Lew, Aug 14, 2007
    #8
  9. Artur Siekielski

    Daniel Pitts Guest

    On Aug 14, 2:25 pm, Artur Siekielski <>
    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).


    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.
     
    Daniel Pitts, Aug 15, 2007
    #9
  10. > 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...
     
    Artur Siekielski, Aug 15, 2007
    #10
  11. Artur Siekielski

    Daniel Pitts Guest

    On Aug 15, 11:07 am, Artur Siekielski <>
    wrote:
    > > 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);
    }
     
    Daniel Pitts, Aug 16, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    715
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Chris Theis
    Replies:
    2
    Views:
    480
    Chris Theis
    Jul 24, 2003
  3. tom_usenet
    Replies:
    0
    Views:
    539
    tom_usenet
    Jul 24, 2003
  4. Replies:
    1
    Views:
    379
    Sybren Stuvel
    Apr 10, 2006
  5. Sasha
    Replies:
    3
    Views:
    595
    Sasha
    May 22, 2007
Loading...

Share This Page