defrex
defrex

Reputation: 16435

JavaScript template language that pre-compiles to pure JavaScript

I'm using the Google Closure Compiler in advanced mode for a project I'm working on. This is an extremely aggressive JavaScript compiler that can optimize your code quite a bit. However, in order to do this it needs to see the whole picture. That is to say, if I assign something to window.foo, and then only access window.foo from JS that the compiler can't see, it won't exist, since it will have been changed to window.f or even removed all together.

This complicates things when it comes time for JS temples. Since they are loaded at runtime out of a string, they tend to refer to variables that no longer exist. This can be mitigated slightly by using quoted assignment for the object used to provide values to the template. eg. templateContext['user'] rather then templateContext.user. However, that doesn't allow me to do templateContext.user.username and so on, so it's somewhat limiting.

tl;dr: I need a JavaScript templating solution that can be pre-processed into pure JavaScript that I can include in a .js file.

Does anything like this exist?

Upvotes: 2

Views: 629

Answers (2)

Stephen Chung
Stephen Chung

Reputation: 14605

You can try Dojo 1.6. There is a way to make it work quite seamlessly with Closure in Advanced mode, even with Dijit templates.

http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

Upvotes: 0

defrex
defrex

Reputation: 16435

Honestly, I'm not sure how I missed this: Google Closure Templates.

Upvotes: 1

Related Questions