Express Scaffolding build project using node. JS The default is the Jade template engine, Jade engine is too difficult to use, so hard to set as the default template engine, too much ah! Using the handlebars template engine to write about the past, but I prefer to use Ejs, choose it because the template engine with the ASP is a bit similar to it.
Let's take a look at these template engines:
Jade Templates
HTML Head title #{title} Meta (charset= "UTF-8") body div.description #{description} ul -Each data in Datas li.item (id= ' item_ ' +data.index) span= data.time a.art (href=data.url) = Data.title
It looks clean, introduces indentation, loses the flexibility of HTML
Handlebars template
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "UTF-8"> <title>{{title}}-Page Test</title></Head><Body> <Divclass= "description">{{Description}}</Div> <ul>{{#datas}}<Liclass= "Item"ID= "Item_{{index}}"><span>{{Time}}</span><ahref= "{{URL}}"class= "Art">{{title}}</a></Li>{{/datas}}</ul></Body></HTML>
It's a little uncomfortable to use.
Ejs Template
<!DOCTYPE HTML><HTML><Head> <MetaCharSet= "UTF-8"> <title><%=title%>-Page Test</title></Head><Body> <Divclass= "description"><%=Description%></Div> <ul><% functiondata (data) {%> <Liclass= "Item"ID= "Item_<%=data.index%>"><span><%=Data.time%></span><ahref= "<%=data.url%>"class= "Art"><%=Data.title%></a></Li><% } %><%datas.map (data)%> </ul></Body></HTML>
Although it seems messy, but easy to write their own logic, suitable for personal projects, if you really have code cleanliness, you can consider the server-side book sentence to the front-end JS, the browser to render the data
<script> var name = ' <%=name%> '; // name is a string var str= ' <%-json.stringify (userinfo)%> '; // UserInfo is an object var userino=</script>
This way can also do a bit of deformation, when the server-side transfer of the object to the first turn into a string
<script> var userinfo=<%-userinfo%>; // back end of the time json.stringify (), because JS is a dynamic type, so here can directly get UserInfo object </script>
Both of these methods are also suitable for the handlebars template engine
- <% code%>: JavaScript codes.
- <%= code%>: Displays content that has been replaced with HTML special characters. Equivalent to {{{code}}} in handlebars, do not want the HTML to be encoded
- <%-code%>: Displays the original HTML content. Equivalent to {{code}} in handlebars, encoded HTML
<%= the difference between code%> and <%-code%>, when the variable code is a normal string, there is no difference between the two. When code such as
The node. JS Ejs template engine assigns back-end data to the front-end JS