Ejs构建模块网站

这是ejs在githup上的项目,可以通过它进行了解
https://github.com/tj/ejs

使用 npm来安装ejs模块依赖
npm install ejs –save

ejs可以用来传参,下图中用到的module.exports = 代码 是ES6中的语法
<%= 参数名 %> 是ejs的语法

module.exports = `
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset='utf-8'>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Koa Server HTML</title>
            <link href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
            <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script>
            <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js"></script>
        </head>
        <body>
            <div class="container">
                <div class="row">
                    <div class="col-md-8">
                        <h1>Hi <%= you %></h1>
                        <h2>Hello <%= me %>
                    </div>
                    <div class="col-md-4">
                        <p>Koa静态页面</p>
                    </div>
                </div>
            </div>
        </body>
    </html>
`

server/tpl/index.js
pugTpl: 这句代码跟本次笔记无关

module.exports = {
    htmlTpl: require('./html'),
    ejsTpl: require('./ejs'),
    pugTpl: require('./pug')
}

pugTpl 跟本次笔记无关

server/index.js

const Koa = require('koa')
const app = new Koa()
const {htmlTpl, ejsTpl, pugTpl} = require('./tpl')
const ejs = require('ejs')
// const pug = require('pug')
app.use(async (ctx, next) => {
    ctx.type = 'text/html; charset=utf-8'
    ctx.body = ejs.render(ejsTpl, {
        you: 'chenpeilun',
        me: 'shenyuwan'
    })
})
app.listen(5566)

在ctx.body = ejs.render(pugTpl, {
you: ‘chenpeilun’,
me: ‘shenyuwan’
})

这跟的就是ejs中的render方法,将you,me这两个参数赋值并传给ejsTpl模块

然后在网站中输入localhost:5566就可以显示模板页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EJS(Embedded JavaScript)是一种基于JavaScript的模板引擎,可以用来生成HTML、XML等文档。EJS提供了一种简单的方式来将数据嵌入到HTML页面中。其中,include指令用于在EJS模板中引入其他模板文件。 使用include指令,需要在模板中使用以下语法: ``` <% include path/to/other/template.ejs %> ``` 其中,path/to/other/template.ejs是要引入的模板文件的路径。注意,在使用include指令时,被引入的模板文件中不应该包含任何与当前模板文件相同的变量或函数,否则可能会导致冲突。 需要注意的是,EJS并没有内置的include功能,而是通过在模板中定义一个自定义的include函数来实现的。因此,在使用include指令之前,需要在程序中定义这个函数。下面是一个例子: ``` const ejs = require('ejs'); const fs = require('fs'); ejs.filters.include = function (path) { const file = fs.readFileSync(path, 'utf8'); return ejs.render(file, this); }; const template = ` <html> <head> <title>Example</title> </head> <body> <% include path/to/other/template.ejs %> </body> </html>`; const data = { name: 'John' }; const output = ejs.render(template, data); console.log(output); ``` 在上面的例子中,我们首先定义了一个自定义的include函数,然后在模板中使用include指令来引入其他模板文件。当渲染模板时,EJS会自动调用include函数来处理这些指令,并将被引入的模板文件和当前模板文件的数据合并起来生成最终的HTML输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值