[Helma-user] New Additions to Template.js

Juerg Lehni juerg at scratchdisk.com
Sat Apr 14 21:15:28 CEST 2007


Yet another update:

I added prefix / suffix / default to conditional macros and loops,  
and a new feature called separator to loop.

This now allows things like:

<% foreach $value in [1, 2, 3] prefix="[ " suffix=" ]" separator=", " %>
   <%= $value %>
<% end %>

Result: [1, 2, 3]

<% if param.greetings prefix="Hello" %>
   World
<% end %>

Result: Hello World

Also note that parantheses are not needed any longer in if / elseif /  
foreach, but can still be used.

I think these additions are very useful.

The next feature will be to somehow allow $value in the loop above to  
know where it is... I still have not decided how this can be done  
nicely.

The changes lead to another clean up of the code, which seems to get  
less of a hack with each addition.

Download at the usual place:

http://dev.helma.org/wiki/JavaScript+Template+Engine/

Jürg

On 13 Apr 2007, at 12:29, Juerg Lehni wrote:

> Hello,
>
> I just upploaded a largely refactored and overhauled Template.js
>
> http://dev.helma.org/wiki/JavaScript+Template+Engine/
>
> This incorperates most of the skin additions described here:
>
> http://dev.helma.org/wiki/New+Skin+Features+in+Helma+1.6/
>
> Generally, I think althought the code became longer, it should be
> much more readable. There are still many regexps in there, but that
> is just the way to do it and still get a half-way decent speed for
> parsing in JS. All these constructs could be easily replaced with
> something more understandable in Java. I am very pleased with the
> efficiency of the new macr parser in Template.prototype.parseMacro
> and its private functions, for those who want to have a closer look.
>
> Some of the features / changes:
>
> - Local variables now need to have a $ in front of them, so you will
> probably have to change some of your templates. Right now, the new
> way to do things is:
>
>    <% foreach ($value in list) %> instead of <% foreach (value in
> list) %>
>    <% $value = 10 %> instead of <% set value = 10 %>
>
> This is not finalized though, i am simply playing around with
> notation and am very curious to hear the thoughts of the Template.js
> users.
>
> - Filter chains work, and hopefully the same way as in Helma. I am
> not so sure about all the details, e.g. where and when to append
> prefix / suffix, but will look at this soon to make sure it does the
> same.
>
> - Neste macros are supported. You can still write something like <%
> macro value=param.otherValue %> and for simple things, that is the
> recommended way, but you can also go and render a sub template into
> the parameter of another macro. neat!
>
> - Unnamed parameters are there and work in the same way as in Helma.
>
> - Sub templates are implemented in a new way, closly mimicing sub  
> skins:
>
> <% #name %>
> This is a sub-template. It can call <% macros %> and write <%=
> param.value %> too!
> Empty lines before and after it are automatically removed.
>
> <% #greedy +%>
> This is another sub-template. As apposed to the one abovce, this one
> keeps the
> empty lines due to the + at the end.
>
> Template.js also adds a macro for rendering templates and
> subtemplates. All this should work:
>
> <% this.template "#sub" value="variables" %>
> <% this.template "main#sub" value="variables" %>
> <% this.template "main" value="variables" %>
>
> Despite my first somewhat negative reactions to these I now started
> to see the advantages too, and have actually come to the conclusion
> that it is not a bad thing to drop nested content in tags.
>
> And while I was at it, I thought it would be neat to offer a
> possibility to render templates directly into a scope variable:
>
> <% $text %>
> Render this text into '$text'. Again, <% macros %> can be called.
>
> This also creates a sub template of the same name, without the $. And
> the variable $text then just contains the rendered result in the
> example above. This is actually what gave me the idea to only use $
> for all variables, but I am not so sure about that decision yet...
>
> I am curious to here what the users of Template.js think about all
> these changes.
>
> Will anybody miss the possibility to have nested content?
>
> I hope you enjoy this.
>
> I am looking forward to recieving some feedback.
>
> Jürg
> _______________________________________________
> Helma-user mailing list
> Helma-user at helma.org
> http://helma.org/mailman/listinfo/helma-user



More information about the Helma-user mailing list