It all started with reading this
post here .
I really liked the idea, and without thinking twice, I began to search the Internet for ready-made solutions of “template engines” for jQuery.
Here is what I managed to find:
Short about found
jQuery templates is a simple and intuitive plugin that replaces expressions of the form
${myvar:func1:func2}
with the values ​​passed by the input parameters.
Nano - does almost the same thing, but the plug-in code takes five lines.
Pure - “DOM-based” template engine; in fact, all substitutions are due to the imposition of object data on the dom-tree.
jQote - as they themselves state - is rewritten under the jQuery “Micro Template Engine”.
jTemplates - a rather interesting engine with great functionality (thanks to
spanasik )
JQuery templates
On the jQuery forums, there was a rather active discussion on the topic of incorporating the template engine into the jQuery core (
link ).
In particular, a demo version was offered, written by John Resig (
link )
')
My template engine
In my opinion, the most successful of template engines is jquery-tmpl (although it is a demonstration one). But I didn’t really like its implementation, plus I lacked some methods.
Therefore, I decided to rewrite it and add a small functionality (plus write a demo that would show the possibility of using templates).
Result
The result of the work can be downloaded here:
jQuery.tpl , all the sources are:
Google code .
And here's the
demo page .
Main features
- PHP-like structure (code between "{%" and "%}", the rest is text)
- Using Named Templates
- Ability to add custom modifiers (via the
$.template.modificators
object) - The catch modifier, each
- Fast creation of functions ( and scope ) using the modifiers "@" and "~"
- Pattern caching
- Lines outside the code do not pass through eval (replaced by variables of the form $ n)
- The template can store data (the $ scope variable available from the template)
- Ability to specify optional parameters (when accessing them, errors will not be displayed if they are not specified)
- The code between "{% =" and "%}" can return not only a string, but also a jQuery object
I will be glad to criticism and comments.
PS
Works under IE6