[Helma-user] New Additions to Template.js

Juerg Lehni juerg at scratchdisk.com
Fri Apr 13 18:29:04 CEST 2007


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


More information about the Helma-user mailing list