当前位置:网站首页>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-template
Command 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
边栏推荐
- LeetCode 20、有效的括号
- [graph theory brush question-4] force deduction 778 Swimming in a rising pool
- How many hacking methods do you know?
- Notes of Tang Shu's grammar class in postgraduate entrance examination English
- Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
- SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
- Three. Based on ply format point cloud voxel model JS upload interface writing
- Numpy mathematical function & logical function
- Click an EL checkbox to select all questions
- Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
猜你喜欢
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
LeetCode 994、腐烂的橘子
[PTA] l1-002 printing hourglass
Mysql database backup scheme
Livego + ffmpeg + RTMP + flvjs to realize live video
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
Handwritten Google's first generation distributed computing framework MapReduce
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
随机推荐
Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
SQL gets the latest record of the data table
LeetCode 1351、统计有序矩阵中的负数
LeetCode 1337、矩阵中战斗力最弱的 K 行
LeetCode 116. 填充每个节点的下一个右侧节点指针
LeetCode 20、有效的括号
【PTA】L2-011 玩转二叉树
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
内网渗透之DOS命令
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
JS arrow function user and processing method of converting arrow function into ordinary function
Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
Commande dos pour la pénétration de l'Intranet
Confusion about thread blocking after calling the read () method of wrapper flow
Leetcode 20. Valid parentheses
The flinkcdc reports an error: but this is no longer available on the server
Numpy - creation of data type and array
6-5 string - 2 String copy (assignment) (10 points) the C language standard function library includes the strcpy function for string copy (assignment). As an exercise, we write a function with the sam
Common form verification
Investigate why close is required after sqlsession is used in mybatties