当前位置:网站首页>Nunjuks template engine
Nunjuks template engine
2022-07-07 19:41:00 【Struggling young man】
NunJucks template engine
There are many template engines , I've used it before ejs
, It's all about rendering pages . Next, I'll give you a simple experience Nunjucks
. Don't really say , Interpolation syntax is a little vue
The smell of .
First try NunJucks template engine
- Create project folder first
nunjucksExprice
- Initialize project file
npm init -y
- introduce NumJucks
Tips
The input terminal
npm install nunjucks
Wait for the installation to complete , We're installing express.js, To build a server .
- introduce express.js
npm install express
- Create a view folder
view
- Create... In the view folder
index.html
file - Create project entry file
index.js
- Use express Create a server
const express = require("express")
const app = express()
app.listen('80', () => {
console.log('express is running at http://127.0.0.1/');
})
A simple server is created .
- Introduce... In the entry file
nunjucks
, And use node.js Self contained path Module setting template path .
const express = require("express")
const nunjucks = require("nunjucks")
const path = require("path")
const app = express()
// Set the template storage location
app.set("view", path.join(__dirname, "view"))
// Set up the template engine
nunjucks.configure("view", {
autoescape: true, // Whether to automatically match
express: app
})
// Set up the view engine
app.set("view engine", "html")
// Set the routing , Render the page
app.get("/", (req, res) => {
res.render("index")
})
app.listen('80', () => {
console.log('express is running at http://127.0.0.1/');
})
The above code can also be written like this , These two writing methods are equivalent , The only difference is that a view engine is set , So when rendering pages
Don't have to add
.html suffix , One has no settings , JustNeed to add
On .html suffix , Otherwise, the template engine cannot find the page .const express = require("express") const nunjucks = require("nunjucks") const path = require("path") const app = express() // Set the template storage location app.set("view", path.join(__dirname, "view")) // Set up the template engine nunjucks.configure("view", { autoescape: true, // Whether to automatically match express: app }) app.get("/", (req, res) => { res.render("index.html") }) app.listen('80', () => { console.log('express is running at http://127.0.0.1/'); })
Functional test
Next, join me to learn about NumJucks How to use it .
and ejs
equally ,NumJucks You can also transfer values when rendering pages (key:value) In the form of .
Passing variables
We create a variable str
str = 'Word'
app.get("/", (req, res) => {
res.render("index.html",{
str})
})
str
:ES6 grammar , Just write one as the key value .
Use this in the page
{
{
str}}
Loop traversal
Let's create an array
let list = [' a mandarin orange ', ' Peach ', ' watermelon ']
app.get("/", (req, res) => {
res.render("index.html",{
list})
})
Use this in the page
<ul>
{% for item in list%}
<li>{
{item}}</li>
{% endfor %}
</ul>
You can see that its usage is no longer the same as
ejs
equally ,ejs The template syntax of is <%%>, A little distinction should be made here .At the end of the loop , There must be {% endfor %}
We create an array of objects
var items = [{
name: ' Zhang San ', age: 20 }, {
name: ' Wang Wu ', age: 19 }]
Use this in the page
<ul>
{
% for item in items%}
<ol>{
{
item.name}} - {
{
item['age']}}</ol>
{
% endfor%}
</ul>
Judge
We create a boolean variable
var isDelete = true
app.get("/", (req, res) => {
res.render("index.html",{
isDelete})
})
Use this in the page
{% if isDelete %}
<h3> Welcome landing </h3>
{% else %}
<h3> Please login. </h3>
{% endif %}
Again ,{%endif%} It is also indispensable .
because isDelete The value of is true, Then the page will show " Welcome landing "
Output effect
Project directory
Here we will experience the functions commonly used in daily development , Please refer to the official documents for more usage . I've put it at the beginning of this article , It's convenient for you to consult .
边栏推荐
- PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
- R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist
- Ucloud is a basic cloud computing service provider
- R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
- ASP.NET幼儿园连锁管理系统源码
- AD域组策略管理
- 超分辨率技术在实时音视频领域的研究与实践
- How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
- IP 工具类
- RESTAPI 版本控制策略【eolink 翻译】
猜你喜欢
2022.07.04
杰理之相同声道的耳机不允许配对【篇】
Kirin Xin'an joins Ningxia commercial cipher Association
[RT thread env tool installation]
9 原子操作类之18罗汉增强
杰理之测试盒配置声道【篇】
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
PV静态创建和动态创建
PV static creation and dynamic creation
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
随机推荐
一张图深入的理解FP/FN/Precision/Recall
关于ssh登录时卡顿30s左右的问题调试处理
Throughput
POJ 2392 Space Elevator
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)
现在股票开户可以直接在网上开吗?安全吗。
Introduction to bit operation
How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
Kirin Xin'an cloud platform is newly upgraded!
8 CAS
Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
Unable to link the remote redis server (solution 100%
Tp6 realize Commission ranking
杰理之相同声道的耳机不允许配对【篇】
索引总结(突击版本)
how to prove compiler‘s correctness
AD域组策略管理
项目经理『面试八问』,看了等于会了