I am working on simple templating enging for my work in javascript.. it needs to be very simple so i am not using Handlebars, moustache or any other robust templating engine available.
I keep reading the word 'PRECOMPILE' or 'COMPILE' template to boost performance. But i am not sure what exactly then mean by that. In my work i cache the template html in my object to avoid hitting the template html everytime.
It's a very simple function which mainly does the following
_template = _template.replace(/\{(.+?)\}/g, function(token, match, number, txt) {
return item[match];
});
item is the object which holds the value to be replaced.
Can someone please tell me what exactly it means when they(handlebars etc) say compile the template. ?
Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats. Handlebars templates look like regular text with embedded Handlebars expressions.
EJS Is Way Faster Than Jade And Handlebars. EJS Has A Really Smart Error Handling Mechanism Built Right Into It. It Points Out To You, The Line Numbers On Which An Error Has Occurred So That You Don't End Up Looking Through The Whole Template File Wasting Your Time In Searching For Bugs.
These templates may be executed in the same manner as templates. If using the simple mode the precompiler will generate a single javascript method. To execute this method it must be passed to the Handlebars. template method and the resulting object may be used as normal.
The most important use of Handlebars, and any templating engine, is to keep your HTML pages simple and clean and decoupled from the logic-based JavaScript files, and Handlebars serves this purpose well. Indeed, Dust. js is also a logic-less templating engine and a worthy alternative to Handlebars. js.
Underscore, Handlebars, and most other JS templates create a function out of the template string. You can then call the function multiple times with different arguments and get the appropriate string of HTML. (I believe both Underscore and Handlebars do this by parsing the template string into JS code and then calling the Function
constructor to make the compiled template function.)
When these libraries talk about "precompiling", they mean the initial step of turning the template string into a reusable function. This can be a somewhat heavy step, but it makes rendering the template much faster (faster than simple regex substitution in most cases) so it's more efficient to create the function from the template code and then use it multiple times, rather than recompiling and rendering every time the template is called.
Usually templates are parsed using regex
and then converted to function
using eval
.
This called precompile template.
Invoking such functions with some data passed as arguments called render template. So templates doesn't get parsed every time you call it - it already exists in form of function that concatenates and return string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With