当前位置:网站首页>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 .
边栏推荐
- 杰理之关于 TWS 配对方式配置【篇】
- L1-023 output gplt (Lua)
- 648. 单词替换
- R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
- 2022.07.04
- Key points of anti reptile: identifying reptiles
- Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
- 爬虫实战(七):爬王者英雄图片
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- 指定opencv非标准安装的版本
猜你喜欢
Is PMP beneficial to work? How to choose a reliable platform to make it easier to prepare for the exam!!!
杰理之测试盒配置声道【篇】
How to estimate the value of "not selling pens" Chenguang?
一张图深入的理解FP/FN/Precision/Recall
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
小试牛刀之NunJucks模板引擎
Former richest man, addicted to farming
杰理之关于 TWS 配对方式配置【篇】
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
8 CAS
随机推荐
Time tools
AI writes a poem
tp6 实现佣金排行榜
Numpy——axis
IP 工具类
让这个 CRMEB 单商户微信商城系统火起来,太好用了!
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
A pot of stew, a collection of common commands of NPM and yarn cnpm
Hongmeng smart home [1.0]
银行理财产品怎么买?需要办银行卡吗?
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
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
2022.07.05
转置卷积理论解释(输入输出大小分析)
What does "true" mean
杰理之按键发起配对【篇】
Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
杰理之测试盒配置声道【篇】
Throughput