最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is javascript template precompiling? - Stack Overflow

programmeradmin5浏览0评论

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 pile the template. ?

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 pile the template. ?

Share Improve this question asked Nov 23, 2012 at 22:08 KamalKamal 1,12211 silver badges18 bronze badges 1
  • i found this post ejohn/blog/javascript-micro-templating after reading reply from @nrabinowitz.. – Kamal Commented Nov 23, 2012 at 22:25
Add a ment  | 

3 Answers 3

Reset to default 11

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 piled template function.)

When these libraries talk about "prepiling", 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 repiling and rendering every time the template is called.

Usually templates are parsed using regex and then converted to function using eval. This called prepile 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.

Update: I'm incorrect here, see ment below.

Well let's say my template is a sentence with fill in the blanks:

One day, ____ decided he was going to bring _____ with him to the _____.

Compiling the template would be filling in the blanks.

On day, JOHN decided he was going to bring SALLY with him to the BATMAN PREMIER.

Hope metaphor is helpful in this case : )

发布评论

评论列表(0)

  1. 暂无评论