1- 安装
npm install --save art-templatenpm install --save koa-art-template
2- 配置
const render = require("koa-art-template");const path = require("path");render(app,{/* __dirname当前文件所在文件夹 */root:path.join(__dirname,"pages"),extname:".html"})router.get("/",async ctx=>{await ctx.render("index.html")})
2-1 实例
const koa = require("koa");const app = new koa();const router = require("koa-router")();const render = require("koa-art-template");const path = require("path")const axios = require("axios")render(app,{/* __dirname 当前文件所在的文件夹 */root:path.join(__dirname,'pages'),extname:".html"})router.get("/",async ctx=>{var html = await axios.get("http://192.168.4.18:8000/")await ctx.render("index",{arr:html.data.result})})router.get("/friend",async ctx=>{await ctx.render("friend.html")})app.use(router.routes())app.listen(3000)
3- 基本语法
3-1 遍历 each
{{each arr}}<p>{{$index}}-{{$value}}</p>{{/each}}
3-2 if && if-else
// if{{if isShow}}<p>加载更多</p>{{/if}// if-else{{if isShow}}<p>加载更多</p>{{else}}<p>不要加载</p>{{/if}}
5- 模板继承
可以使用一个通用的模块
5-1 extend
//template.html 定义一个公用的导航模块//index.html 可以在index中使用extend关键字将其获取{{extend 'template.html'}}
5-2 include
可以获取子模板
{{include './nav.html'}}
5-3 block 插槽
//骨架模板<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{{block "title"}} {{/block}}</title></head><body>{{include './nav.html'}}<!-- 插槽 -->{{block "content"}}{{/block}}</body></html>
// index.html{{extend './common/template.html'}}{{block "title"}}音乐{{/block}}{{block "content"}}<p>music</p>{{/block}}
5-4 标准语法
{{extend "./common/template.html"}}{{block 'content'}}{{/block}}
