The node. JS Ejs template engine assigns back-end data to the front-end JS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.