当前位置:网站首页>Use of node template engine
Use of node template engine
2022-04-23 20:28:00 【Different 213】
List of articles
template engine
1. The basic concept of template engine
1.2 art-template template engine
- Use... In command line tools
npm install art-templateCommand to download - Use
const template = require('art-template')Introduce template engine - Tell the template engine where the data and template to be spliced are :
const html = template(' Template path ', data );
The basic example code is as follows :
// Import template engine
const template = require('art-template');
const {
Agent } = require('http');
const path = require('path');
// Splicing template path
const views = path.join(__dirname,'views','index.art')
// template Method is used to splice strings
// 1. Template path Using absolute paths
// 2. Data to be displayed in the template object type
// Returns the concatenated string
const html = template(views, {
name: ' Zhang San ',
age:20
})
console.log(html);
2. Template engine Syntax
2.1 Template syntax
- art-template Supports both template syntax : Standard grammar and original grammar .
- Standard syntax makes templates easier to read and write , Primitive grammar has powerful logical processing ability .
2.2 Output
Output a piece of data in a template , The standard syntax and the original syntax are as follows :
-
Standard grammar :{ { data }}
-
Original grammar :<%= data %>
2.3 Original output
If the data carries HTML label , The default template engine does not parse tags , It will be escaped and output .
- Standard grammar :{ {@ data }}
- Original grammar :<%- data %>
2.4 conditional
In the template, you can decide which block to display according to the conditions HTML Code .
<!-- Standard syntax for conditional judgment -->
{
{if age > 18}}
Older than 18
{
{else if age < 15}}
Age is less than 15
{
{else}}
Age does not meet the requirements
{
{/if}}
<!-- The original grammar makes conditional judgment -->
<% if (age > 18) { %>
Older than 18
<% } else if (age < 15) { %>
Age is less than 15
<% } else { %>
Age does not meet the requirements
<% } %>
2.5 loop
-
Standard grammar :{ {each data }} { {/each}}
-
Original grammar :<% for() { %> <% } %>
<ul> { {each users}} <li> { {$value.name}} { {$value.age}} { {$value.sex}} </li> { {/each}} </ul> <ul> <% for(var i = 0;i<users.length; i++) { %> <li> <%= users[i].name%> <%= users[i].age%> <%= users[i].sex%> </li> <% }%> </ul>
2.6 Sub template
Use the subtitle version to make the website public ( Head 、 Bottom ) Pull out into a separate file .
- Standard grammar :{ {include ‘ Templates ’}}
- Original grammar :<%include(’ Templates ‘) %>
{
{include './common/header.art'}}
<div> {
{ msg }} </div>
{
{include './common/footer.art'}}
<%include('./common/header.art') %>
<div> {
{ msg }} </div>
<%include('./common/footer.art')%>
2.7 Template inheritance
Using template inheritance, the website can be HTML Extract the skeleton into a separate file , Other page templates can inherit skeleton files .
The sample code is as follows :
This part is HTML Skeleton formwork part :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Skeleton template </title>
{
{block 'link'}} {
{/block}}
</head>
<body>
{
{block 'content'}} {
{/block}}
</body>
</html>
This part is the pit filling part :
{
{extend './common/layout.art'}}
{
{block 'content'}}
<p>{
{ msg }}</p>
{
{/block}}
{
{block 'link'}}
<link rel="stylesheet" href="./main.css">
{
{/block}}
2.8 Template configuration
- Import variables into the template
template.defaults.imports. Variable name = A variable's value ; - Set the template root directory
template.defaults.root = The template directory; - Configure the default suffix of the template
template.defaults.extname = Suffix name
The sample code is as follows :
// Import template engine
const template = require('art-template');
const path = require('path');
const dateformat = require('dateformat')
// Set the root directory of the template
template.defaults.root = path.join(__dirname, 'views');
// Import template variables
template.defaults.imports.dateformat = dateformat;
// Configure the default suffix of the template
template.defaults.extname = '.html';
// After setting the root directory of the template , Just fill in the file name to be rendered here : for example 06
const html = template('06.art', {
time : new Date()
});
// Rendering 07.html file , write in 07 The automatic query file name is 07.html The file of
console.log(template('07', {
}))
console.log(html);
版权声明
本文为[Different 213]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210550451054.html
边栏推荐
- Use the rolling division method to find the maximum common divisor of two numbers
- Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
- Mysql database backup scheme
- Leetcode 1351. Negative numbers in statistical ordered matrices
- JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
- LeetCode 1346、检查整数及其两倍数是否存在
- 堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
- JS arrow function user and processing method of converting arrow function into ordinary function
- What is the difference between a host and a server?
- ABAQUS script email auto notification
猜你喜欢
![[PTA] l1-002 printing hourglass](/img/9e/dc715f7debf7edb7a40e9ecfa69cef.png)
[PTA] l1-002 printing hourglass

上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案

2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real

【PTA】L1-002 打印沙漏

DNS cloud school rising posture! Three advanced uses of authoritative DNS

Monte Carlo py solves the area problem! (save pupils Series)

Plato Farm元宇宙IEO上线四大,链上交易颇高

LeetCode 74、搜索二维矩阵

go-zero框架数据库方面避坑指南

Latest investigation and progress of building intelligence based on sati
随机推荐
Some basic knowledge of devexpress report development
【PTA】L2-011 玩转二叉树
Leetcode 74. Search two-dimensional matrix
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
[PTA] l1-006 continuity factor
[talkative cloud native] load balancing - the passenger flow of small restaurants has increased
Notes of Tang Shu's grammar class in postgraduate entrance examination English
After route link navigation, the sub page does not display the navigation style problem
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
PostgreSQL basic functions
Markdown < a > tag new page open link
ABAQUS script email auto notification
Es error: request contains unrecognized parameter [ignore_throttled]
Numpy Index & slice & iteration
三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
What is the difference between a host and a server?
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
BMP JPEG 图片转换为矢量图像 ContourTrace
Livego + ffmpeg + RTMP + flvjs to realize live video
内网渗透之DOS命令