首页 > 其他分享 >nunjucks模板语法

nunjucks模板语法

时间:2022-11-17 22:48:32浏览次数:46  
标签:nunjucks const views 语法 studentList html router 模板

循环语句

server.js

const Koa = require("koa");//引入koa构造函数
const app = new Koa();//创建应用
const views = require("koa-views");//引入koa-views
const nunjucks = require("nunjucks");//引入nunjucks引擎
const router = require("koa-router")();

app.use(views(__dirname + "/views",{
    map:{html:"nunjucks"}
}))

router.get("/",async ctx => {
    let studentList = ["ben","mike","lucy"]
    await ctx.render("index",{
        title:"后台数据",
        studentList//studentList:studentList的简写
    });
})

app.use(router.routes());

app.listen(3000,() => {
    console.log("server is running");
});

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>模板语法</h1>
    <p>{{title}}</p>
    <ul>
        {% for student in studentList %}<!--student是列表的元素-->
        <li>{{student}}</li>
        {% endfor %}
    </ul>
</body>
</html>

条件语句

server.js 只显示if相关语句

router.get("/",async ctx => {
    let studentList = ["ben","mike","lucy"]
    await ctx.render("index",{
        title:"后台数据",
        studentList,
        isLogin:false,
        username:"admin"
    });
})

index.html 只显示if相关语句

<body>
    {% if isLogin %}
    <p>欢迎{{username}}</p>
    {% else %}
    <p>请登录...</p>
    {% endif %}
</body>


模板继承



每个页面都有晓舟报告那一行的头部栏,有两种方法实现
要么单独给每个页面都写一遍头部栏,要么用模板继承
如果有几十个页面,第一种显然不合理,也不利于后续修改头部栏
模板继承就是先写一个公共模板,再让其他页面继承它
server.js

const Koa = require("koa");//引入koa构造函数
const app = new Koa();//创建应用
const views = require("koa-views");//引入koa-views
const nunjucks = require("nunjucks");//引入nunjucks引擎
const router = require("koa-router")();

app.use(views(__dirname + "/views",{
    map:{html:"nunjucks"}
}))

router.get("/",async ctx => {
    let studentList = ["ben","mike","lucy"]
    await ctx.render("index",{
        title:"后台数据",
        studentList,
        isLogin:false,
        username:"admin"
    });
})

router.get("/images",async ctx => {
    await ctx.render("images");
})

app.use(router.routes());

app.listen(3000,() => {
    console.log("server is running");
});

layout.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <a href="/">首页</a>
        <a href="/images">图片</a>
    </div>
    {% block content %} {% endblock %}
    <!--block说明是模板,content是模板的名字-->
    <!--把每个页面中公共区域所有的内容都放到layout.html这一模板文件中-->
</body>
</html>

index.html

{% extends "./views/layout.html" %}<!--继承自layout.html-->

{% block content %}<!--继承的内容-->
    <h1>模板语法</h1>
    <p>{{title}}</p>
    <ul>
        {% for student in studentList %}<!--student是列表的元素-->
        <li>{{student}}</li>
        {% endfor %}
    </ul>
    {% if isLogin %}
    <p>欢迎{{username}}</p>
    {% else %}
    <p>请登录...</p>
    {% endif %}

{% endblock %}<!--结束标志-->

images.html

{% extends "./views/layout.html" %}

{% block content %}
    <h1>图片页面</h1>

{% endblock %}

include

include与block的区别在于block模板是每个页面都有的部分,而include只是有些页面需要用到。
index.html

{% extends "./views/layout.html" %}<!--继承自layout.html-->

{% block content %}<!--继承的内容-->
    <h1>模板语法</h1>
    <p>{{title}}</p>
    <ul>
        {% for student in studentList %}<!--student是列表的元素-->
        <li>{{student}}</li>
        {% endfor %}
    </ul>
    {% if isLogin %}
    <p>欢迎{{username}}</p>
    {% else %}
    <p>请登录...</p>
    {% endif %}

    {% include "./views/footer.html" %}<!--include的内容-->

{% endblock %}<!--结束标志-->

footer.html

<div>
    powerby Benson
</div>

标签:nunjucks,const,views,语法,studentList,html,router,模板
From: https://www.cnblogs.com/ben10044/p/16901277.html

相关文章

  • 最长不下降子序列nlogn模板
     #include<bits/stdc++.h>usingnamespacestd;intd[100011],n,len,a[100011];intmain(){//freopen(".in","r",stdin);//freopen(".out","w",stdou......
  • Nunjucks模板入门
    概述安装nunjucks代码实现server.jsconstKoa=require("koa");//引入koa构造函数constapp=newKoa();//创建应用constviews=require("koa-views");//引入k......
  • Verilog语法入门
    VerilogHDL是一种硬件描述语言(HDL:HardwareDescriptionLanguage),以文本形式来描述数字系统硬件的结构和行为的语言,用它可以表示逻辑电路图、逻辑表达式,还可以表示数字逻辑......
  • Vue基础语法
    基本结构helloworldel来匹配元素,#依据id匹配,.依据class匹配data设置数据<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>HelloWorld......
  • 模板方法
    模板方法例子如下,还是计算器为例。publicenumOperator{Add,Sub,Mul,Div}publicabstractclassCalTemplate{publicabstractOperatorOperat......
  • Jenkins之流水线语法
    目录1流水线1.1简介1.2声明式流水线简介1.3脚本化流水线简介2声明式流水线2.1规范2.2Sections2.2.1Agent2.2.1.1any2.2.1.2none2.2.1.3label2.2.1.4node2.2.1.......
  • 11.5 基础语法
    基础语法数据类型强类型语言:所有变量都必须先定义后才能使用基本类型:​ 整数类型:byte,short,int,long​ 浮点数:float,double​ 字符:char​ boolean:true,false引......
  • [模板]kmp求Next数组
    模板#include<iostream>#include<string>usingnamespacestd;voidgetNext(conststring&p,intnext[]){intlen=(int)p.size();next[0]=-1;......
  • JavaScript语法-特殊语法、流程控制语句
    JavaScript语法-特殊语法<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>特殊语法</title><script>//1、语句以;结尾.......
  • 正则表达式基本语法的详解
    正则表达式基本语法的详解本文给给大家介绍正则表达式的基本语法,需要的朋友可以参考下 正则表达式是一种文本模式,包括普通字符(例如,a到z之间的字母)和特殊字符(称为“......