当前位置:网站首页>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.htmlfile - 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 addOn .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
ejsequally ,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 .
边栏推荐
- 2022年投资哪个理财产品收益高?
- ASP. Net kindergarten chain management system source code
- 超分辨率技术在实时音视频领域的研究与实践
- 2022.07.04
- 解决远程rviz报错问题
- what‘s the meaning of inference
- [HDU] 5248 sequence transformation (greedy + dichotomy) [recommended collection]
- Pasqal首席技术官:模拟量子计算率先为工业带来量子优势
- J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
- LeetCode 648(C#)
猜你喜欢

2022.07.05

Numpy——2. Shape of array

2022.07.04

Responsibility chain model - unity

648. 单词替换

Business experience in virtual digital human

J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar

Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
![Jerry's headphones with the same channel are not allowed to pair [article]](/img/7d/3dcd9c7df583944e1d765b67543eb1.png)
Jerry's headphones with the same channel are not allowed to pair [article]

2022.07.02
随机推荐
实训九 网络服务的基本配置
杰理之开机自动配对【篇】
杰理之快速配对,不支持取消配对【篇】
AI writes a poem
9 原子操作类之18罗汉增强
R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
Numpy——2. Shape of array
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
what‘s the meaning of inference
杰理之测试盒配置声道【篇】
Unable to link the remote redis server (solution 100%
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
Ucloud is a basic cloud computing service provider
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
L1-027 rental (Lua)
How to buy bank financial products? Do you need a bank card?
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
Hongmeng smart home [1.0]
Command mode - unity