Templating is a technology that help us to represent data in different forms.
In the old days, choosing a appropriate template engine for client templating is not easy, beacuse you were left with little other choice. Nowadays, choosing templating engine is still a big probelm, beacuse there are so many template engines and most of them seems do nothing different(Template-Engine-Chooser! comes).
In this article, from a principle perspective ,we will have A comprehensive comparison of front-end templating solutions. there are some distinct types of solution that will be mentioned.
The article won’t take the fourth type into detail, except for React. But you will find that react is very similiar with Living templating. beacuse all them is compeletely independent with innerHTML
.
Before diving into detail, let’s talking about innerHTML
first.
innerHTML
is the key through this post,so we need to have a brief review on it. But i don’t think there is any necessity to bring innerHTML
into detail, since we are all familiar with it. so let’s talk about the pros & cons directly.
Before innerHTML
becomes web standard, it has been a conventional “practical standard” for serveral years beacuse its irreplaceable advantages. for example.
1 . easy to code and intuitive to view
imagine that you need to append a html like that.
use innerHTML
compare with the way using Dom API
innerHTML
obviously win the game.
Although some frameworks like mootools:Element provide some more efficient way to constructing HTML with Dom API
, innerHTML
is still the most intuitive way.
2 . it is faster,especially in old IE
> the test maybe out of date in modern browser, the difference between innerHTML
and Dom Level 1
is become smaller and smaller.
But we also learned: The recommended way to modify the DOM is to use the DOM Level 1 API.
A great reference to this is Chapter 15 of “Javascript: The Definitive Guide”. Why?
1. security issuesinnerHTML
has more security issues , especially if you don’t sanitize what you’re putting into it. for example
I know you won’t code like this, but if the HTML is not compeltely controlled by yourself(for example : from remote server), it will be a big issue.
2. it is slow
Indeed, I mentioned innerHTML
is fast. if you just need change a attribute but replacing all DOM nodes with innerHTML
completely, it would be obviously inefficient.
Context is everything
3. not smart enough
it disconnect all existing DOM nodes and rerendering again, all events and state in previous DOM nodes is gone.
4. potential for generating invalid markup with invalid markup.
html parser is so “friendly”, it even accpet invalid html, but developer wont get any ‘parse error ‘during the parsing.
Maybe it’s not innerHTML that is the problem, but constructing HTML with string operations is.
we have thought through innerHTML
already, it is time to talk about “templating solutions” now.
It is essentially a way to address the need to populate an HTML view with data in a better way than having to write a big, ugly string concatenation expression.
—- cited from http://www.dehats.com/drupal/?q=node/107
String-based templating is the most common solution we ever used. beacuse frontend templating is derivatived from backend, in server side the output must be a string , so the browser can render it.
Example
The basic process
As shown above, string-based templating is tightly coupled with innerHTML
(for Rendering).
pros
cons
innerHTML
sectioninnerHTML
sectionIn recent years, dom-based begun to pop up, the prime example is Angular that earned almost 28000 stars in github.
Example
The basic process
dom-based template doesn’t have their own parser, so if you need creating view from a template string, you have to use innerHTML
to convert the string to dom(parsing), then walk the dom tree using the Dom API(attributes
, getAttribute
, firstChild
… etc). All information like directives is hold by the dom node and its attributes.
In fact, the whole process is more like reshaping than rendering.
pros
cons
innerHTML
.ng-show
, etc) in every nodes.string-based and dom-based template are all tightly coupled with innerHTML
, the differrence is: String-based template use innerHTML
for Rendering and Dom-based use it for Parsing.
Why not combining String-based parser and Dom-based compiler to abate the dependence on innerHTML
?
In fact, there have been servaral templates that realized in this way .
Example
The basic process
As shown in the picture above, parsing and compiling are similar with String-based template and dom-based template respectively
First. it use a builtin parser to parse the template string then output a AST.
for example, template string(syntax base on regularjs)
will be parsed to:
with spcified model (in regularjs, it is a plain object), template engine walks the AST and generating the dom recursively, meanwhile, according to the directive and other binder(event, inteplation… etc), it also create the binding between model and dom to make the dom living.
for example, just like the inteplation {isLogin? 'Login': 'Wellcome'}
showed above. once the compiler seen it, the expression
walker will be called.
as shown above, once the expression changed, node.textContent(or innerText) will changes synchronous.
Compare to string-based template, instead of innerHTML
, it use DOM
api(createElement, setAttribute, createTextNode…etc ) to generate the dom. so it is safe.
In fact, in compiling phase, the most difference between Living template and dom-based template is: __dom-based template act a reshaping on dom nodes, But living template is building that according to the resuable AST.
React can be considered as a templating solution,it avoid be coupled with innerHTML
by using virtual dom which is created by nested function call(you can also use jsx syntax)
Example
which in regularjs
Every one thinks in his way, And I prefer using template to describe my structure, do you?
Warning:
Contrast /Solutions | String-based templating | Dom-based templating | Living templating |
---|---|---|---|
Example | Mustache,Dustjs | Angularjs, Vuejs | Regularjs 、Ractivejs、htmlbars |
Syntax | ♦♦♦ | ♦ | ♦♦♦ |
Living Dom | X | ♦♦♦ | ♦♦♦ |
Security | ♦ | ♦♦ | ♦♦♦ |
SVG support(*1) | X | ♦♦ | ♦♦♦ |
Dom independent | ♦♦♦ | X | ♦♦ |
Server Rendering | ♦♦♦ | ♦ | ♦ |