Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

javascript - What is a template engine?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JavaScript template engines are often used when writing thick JS-clients or as "normal" template engines when using server-side js like Node.js.

Instead of cluttering the code by generating HTML using string concatenating etc., you instead use a template and interpolate the variable in them.

For instance in the example bellow you pass in an array foo to the template engine which then interpolates (replaces the placeholder ${data}) with item i from the array foo:

//pseudo code
<table>
 <tr>
   for each variable data in foo
    <td>${data}</td>
   end
 <tr>
</table>

In frameworks like backbone.js you typically use a template to implement the view part of the MVC pattern.

There are many different types of template engines in JS. Some are direct ports of server-side frameworks like ERB, HAML etc. Others are developed specially to work with JavaScript i.e Pure

There is a debate of how much expressiveness the template engine should give you because putting logic in your templates is usually a bad thing. Because of this some template engines are specifically designed to not let you put logic in them i.e Mustache


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...