Can I use thymeleaf for creating javascript files

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I use thymeleaf for creating javascript files

sudhirkd
I am looking for alternative to Apache Velocity for creating plain javascript code (Ext-js/sencha touch code to be specific). The files would have not script tag, so its Non-xml file. I read that though thymeleaf is focused on XML it can be extended to non-xml files.

Any comment would be appreciated.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Can I use thymeleaf for creating javascript files

Zemi
Administrator
Thymeleaf can perform expression evaluation inside Javascript code, so you can use ${...}, @{...}, #{...} or ${#list.whatever(...)}, but you cannot use standard attribute processors like th:each or th:if in a clean way.

Check chapter 11.2 of the "Using Thymeleaf" tutorial for details.
Reply | Threaded
Open this post in threaded view
|

Re: Can I use thymeleaf for creating javascript files

danielfernandez
Administrator
In reply to this post by sudhirkd
In order to extend thymeleaf for use with non-XML files, you'd need to write your own "template mode".

A Template Mode is managed by a "handler" object, which has to be an implementation of the ITemplateModeHandler interface. A "TemplateEngine" object can register a set of different "template mode handlers", and these template modes are selected for application on the resolved templates by the "Template Resolvers", which as a result of their execution return the template mode to be applied along with the InputStream to the resolved template itself.

And what is a template mode? Basically, a combination of parser + writer. A "parser" is an implementation of the ITemplateParser interface, and is in charge of converting templates into a DOM tree. A "writer" (implementation of ITemplateWriter) is the opposite: turns a DOM tree into a text result. The parser is obviously applied to the template at the beginning of the template execution, and the writer at the end.

So you can use thymeleaf for processing non-XML templates *as long as they can be represented as a DOM tree*. And a "DOM tree" is a tree of objects of the classes living at the "org.thymeleaf.dom" package.

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Can I use thymeleaf for creating javascript files

Zemi
Administrator
Yeah, I know that Javascript code processing has been repeatedly requested in these forums, but I don't share the point.

I think that expression evaluation should be enough.

We could figure out a syntax for iterating inside Javascript. For example, in order to set several markers into a Google Map, it could be:

       [[each location : ${locations}]]
           new google.maps.Marker({
               map: map,
               position: [[${location}]]
           });
       [[end-each]]

But, in my opinion, it's not a good practice to generate Javascript code in that way, better move the logic to Javascript code itself, like:

         var locations = /*[[${locations}]]*/ {{42.1, 1.5}, {41.3, 1.1}};
         for (var index in locations) {
           new google.maps.Marker({
               map: map,
               position: locations[index]
           });
        }

Furthermore, the second example would work statically.
Reply | Threaded
Open this post in threaded view
|

Re: Can I use thymeleaf for creating javascript files

sudhirkd
Thanks guys. Really appreciate your quick responses. Would reread and decide if I can use for my purpose. 

I had concern regarding velocity because it has not been updated for last 3 years. 

Sent from my iPhone

On Mar 26, 2013, at 2:15 AM, "Zemi [via Thymeleaf - User Forum]" <[hidden email]> wrote:

Yeah, I know that Javascript code processing has been repeatedly requested in these forums, but I don't share the point.

I think that expression evaluation should be enough.

We could figure out a syntax for iterating inside Javascript. For example, in order to set several markers into a Google Map, it could be:

       [[each location : ${locations}]]
           new google.maps.Marker({
               map: map,
               position: [[${location}]]
           });
       [[end-each]]

But, in my opinion, it's not a good practice to generate Javascript code in that way, better move the logic to Javascript code itself, like:

         var locations = /*[[${locations}]]*/ {{42.1, 1.5}, {41.3, 1.1}};
         for (var index in locations) {
           new google.maps.Marker({
               map: map,
               position: locations[index]
           });
        }

Furthermore, the second example would work statically.



If you reply to this email, your message will be added to the discussion below:
http://forum.thymeleaf.org/Can-I-use-thymeleaf-for-creating-javascript-files-tp4025817p4025820.html
To unsubscribe from Can I use thymeleaf for creating javascript files, click here.
NAML