26-Node.js study notes -art-template template engine use

1.1 template engine

Third-party template engine module

Let developers in a more friendly way to concatenate strings, the project code clearer and easier to maintain.

1.2 art-template template engine

1.命令行工具中使用 npm install art-template 命令进行下载
2.使用const template = require('art-template') 引入模板引擎
3.告诉模板引擎要拼接的数据和模板在哪 const html = template('模板路径',数据);

1.3 art-template sample code

//导入模板引起模块
const template = require(' art-template');
//将特定模板与特定数据进行拼接
const html = template('./views/index.art',{
    data:{
        name:'柠檬不酸',
        age:18
    }
})


//
<div>
    <span>{{data.name}}</span>
    <span>{{data.age}}</span>
</div>

demo

//导入模板引擎
const template  = require('art-template');
const path = require('path');
//template方法是用来拼接字符串的
//1.模板路径 绝对路径
const views = path.join(__dirname,'views','index.art')

//2.要在模板中显示的数据,对象类型
//返回拼接好的 字符串
const html = template(views,{
    name:'柠檬不酸',
    age:18

})
console.log(html)
//template/views/index.art
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  {{name}}
  {{20}}
</body>
</html>

2.1 template syntax

art-template supports both template syntax: standard syntax and original syntax

Standard syntax lets the template easier to read and write, the original amount of logic syntax with powerful processing capabilities

标准语法:{{数据}}
原始语法:<%=数据%>

2.2 Output

An output of the data in the template, the standard syntax of the original syntax is as follows:

//标准语法:{{数据}}
{{value}}
{{a+b}}
//原始语法:<%=数据%>
<%=value%>
<%=a+b%>

2.3 Original output

If the data carries the html tag, the default template engine does not resolve the label, it will be after the escape output

//标准语法:{{数据}}
{{@数据}}

//原始语法:<%=数据%>
<%-数据%>

demo2


const template  = require('art-template');
const path = require('path');

const views = path.join(__dirname,'views','01.art')
const html = template(views,{
    name:'柠檬不酸',
    age:18,
    content:'<h2>我是标题</h2>'

})
console.log(html)
//template/views/01.art
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  //标准语法
  <p>{{name}}<p>
  <p>{{age}}<p>
  <p>{{1+1==2?'相等':'不相等'}}<p>
  <p>{{content}}<p>
  <p>{{@content}}<p>
  //原始语法
  <p><%=name%></p>
  <p><%=age%></p>
  <p><%=1+1==2?'相等':'不相等'%></p>
  <p><%=content%><p>
  <p><%-content%><p>
</body>
</html>

2.4 conditional

In the template may be determined according to the conditions displayed piece of html code

Guess you like

Origin www.cnblogs.com/foreverLuckyStar/p/12094691.html