koa-ejs 使用

1 , 安装

2,

a.ejs

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <% include header.ejs -%>
    <h2>hello</h2>
    <p><%=name%></p>
    <ul>
        <% courses.forEach(item=>{ %>
        <li><%=item%></li>
        <% })%>

    </ul>
</body>

</html>

header.ejs

<nav>
    <p>hello</p>
</nav>

generate.js

const fs = require('fs')
const ejs = require('koa-ejs')
const path = require('path')
const Koa = require('koa')
const server = new Koa();

server.listen(3000)
//配置ejs
ejs(server, {
    root: path.resolve(__dirname, 'template'),
    layout: false,
    viewExt: 'ejs',
    cache: false,
    debug: false

})

server.use(async ctx => {
    await ctx.render('a', {
        "name": "blue",
        "courses": ["java", "php", "c", "node"]

    })
})

显示结果:

猜你喜欢

转载自blog.csdn.net/qq_15009739/article/details/108154754