当前位置:网站首页>小试牛刀之NunJucks模板引擎
小试牛刀之NunJucks模板引擎
2022-07-07 17:25:00 【抗争的小青年】
NunJucks模板引擎
模板引擎有很多,我之前就曾用过ejs,都是对页面进行渲染.接下来带大家简单体验一下Nunjucks。真别说,插值语法有点vue的那股味道了。
初试NunJucks模板引擎
- 先创建项目文件夹
nunjucksExprice - 初始化项目文件
npm init -y
- 引入NumJucks
提示
终端输入
npm install nunjucks
等待安装完成后,我们在安装express.js,来搭建一个服务器。
- 引入express.js
npm install express
- 创建视图文件夹
view - 在视图文件夹中创建
index.html文件 - 创建项目入口文件
index.js - 使用express创建服务器
const express = require("express")
const app = express()
app.listen('80', () => {
console.log('express is running at http://127.0.0.1/');
})
一个简单的服务器就创建好了。
- 在入口文件中引入
nunjucks,并使用node.js自带的path模块设置模板路径。
const express = require("express")
const nunjucks = require("nunjucks")
const path = require("path")
const app = express()
//设置模板存储位置
app.set("view", path.join(__dirname, "view"))
//设置模板引擎
nunjucks.configure("view", {
autoescape: true, //是否自动匹配
express: app
})
//设置视图引擎
app.set("view engine", "html")
//设置路由,进行页面渲染
app.get("/", (req, res) => {
res.render("index")
})
app.listen('80', () => {
console.log('express is running at http://127.0.0.1/');
})
上面的代码也可以这样写,这两种写法是等价的,唯一不同的区别在于一个设置了视图引擎,所以在渲染页面的时候
不用加.html后缀,一个没有设置,就需要加上.html后缀,否则模板引擎找不到页面。const express = require("express") const nunjucks = require("nunjucks") const path = require("path") const app = express() //设置模板存储位置 app.set("view", path.join(__dirname, "view")) //设置模板引擎 nunjucks.configure("view", { autoescape: true, //是否自动匹配 express: app }) app.get("/", (req, res) => { res.render("index.html") }) app.listen('80', () => { console.log('express is running at http://127.0.0.1/'); })
功能小试
接下来和我一起了解一下NumJucks的使用方法吧。
和ejs一样,NumJucks在渲染页面的时候也可以传值(key:value)的形式。
传递变量
我们创建一个变量str
str = 'Word'
app.get("/", (req, res) => {
res.render("index.html",{
str})
})
str:ES6语法,键值一样写一个就行。
在页面中这样使用
{
{
str}}
循环遍历
我们创建一个数组
let list = ['橘子', '桃子', '西瓜']
app.get("/", (req, res) => {
res.render("index.html",{
list})
})
在页面中这样使用
<ul>
{% for item in list%}
<li>{
{item}}</li>
{% endfor %}
</ul>
可以看到它的使用方式不再和
ejs一样,ejs的模板语法为<%%>,这里要稍加区分。同时在循环结尾处,必须要有 {% endfor %}
我们创建一个对象数组
var items = [{
name: '张三', age: 20 }, {
name: '王五', age: 19 }]
在页面中这样使用
<ul>
{
% for item in items%}
<ol>{
{
item.name}} - {
{
item['age']}}</ol>
{
% endfor%}
</ul>
判断
我们创建一个布尔类型变量
var isDelete = true
app.get("/", (req, res) => {
res.render("index.html",{
isDelete})
})
在页面中这样使用
{% if isDelete %}
<h3>欢迎登陆</h3>
{% else %}
<h3>请登陆</h3>
{% endif %}
同样,{%endif%} 也是不可缺少的。
因为isDelete的值是true,那么页面会显示"欢迎登陆"
输出效果

项目目录

这里就带大家体验了平时开发中比较常用的功能了,更多用法请到官方文档查阅。我已经放到本文首了,方便大家查阅。
边栏推荐
- [tpm2.0 principle and Application guide] Chapter 16, 17 and 18
- 炒股如何开户?请问一下手机开户股票开户安全吗?
- First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
- POJ 1182: food chain (parallel search) [easy to understand]
- 基于图像和激光的多模态点云融合与视觉定位
- Seize Jay Chou
- AI writes a poem
- 谷歌seo外链Backlinks研究工具推荐
- AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
- Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
猜你喜欢
![[tpm2.0 principle and Application guide] Chapter 9, 10 and 11](/img/7f/0d4d91142bc3d79ea445a8f64afba7.png)
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11

Creative changes brought about by the yuan universe

99% of people don't know that privatized deployment is also a permanently free instant messaging software!

2022.07.02

Mathematical analysis_ Notes_ Chapter 11: Fourier series

Zhong Xuegao wants to remain innocent in the world

Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products

多个kubernetes集群如何实现共享同一个存储

杰理之测试盒配置声道【篇】

杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】
随机推荐
杰理之快速配对,不支持取消配对【篇】
Version 2.0 of tapdata, the open source live data platform, has been released
炒股如何开户?请问一下手机开户股票开户安全吗?
Key points of anti reptile: identifying reptiles
Solve the error reporting problem of rosdep
LeetCode 648(C#)
国内首次!这家中国企业的语言AI实力被公认全球No.2!仅次于谷歌
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
Borui data was selected in the 2022 love analysis - Panoramic report of it operation and maintenance manufacturers
testing and SQA_ Dynamic white box test [easy to understand]
[mime notes]
Hutool - lightweight DB operation solution
从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks
指定opencv非标准安装的版本
How to open an account for stock speculation? Excuse me, is it safe to open a stock account by mobile phone?
Golang client server login
648. 单词替换
多个kubernetes集群如何实现共享同一个存储
Longest common prefix (leetcode question 14)
企业MES制造执行系统的分类与应用