Support Pug@3.
npm install koa-pug --save
const Koa = require('koa')
const path = require('path')
const Pug = require('koa-pug')
const app = new Koa()
const pug = new Pug({
viewPath: path.resolve(__dirname, './views'),
locals: { /* variables and helpers */ },
basedir: 'path/for/pug/extends',
helperPath: [
'path/to/pug/helpers',
{ random: 'path/to/lib/random.js' },
{ _: require('lodash') }
],
app: app // Binding `ctx.render()`, equals to pug.use(app)
})
pug.locals.someKey = 'some value'
app.use(async ctx => {
await ctx.render('index', locals, true)
})
For koa@1
:
const koa = require('koa')
const Pug = require('koa-pug')
const app = koa()
const pug = new Pug({ app: app })
app.use(function * () {
yield this.render('index', locals, true)
})
Use koa-pug
as a standalone Pug renderer:
const pug = new Pug({
viewPath: path.resolve(__dirname, './views'),
locals: { /* variables and helpers */ },
basedir: 'path/for/pug/extends',
helperPath: [
'path/to/pug/helpers',
{ random: 'path/to/lib/random.js' },
{ _: require('lodash') }
],
// Can work with / without Koa
// app: app
})
async function sendEmail(recipients, tplFile, locals) {
const body = await pug.render(tplFile, locals)
await send(recipients, body)
}
options
are extended from Pug's options, all options will be passed to Pug compiler except the following:
viewPath: string
: the location of Pug templates. Default is process.cwd()
.
locals: { [key: string]: any }
: variables and helpers that will be passed to Pug compiler.
helperPath: string | string[] | { [key string]: string }
: location(s) of helper(s).
koa-pug
will set content-type
to text/html
for you, you can change it:
await ctx.render('index')
ctx.type = 'text/plain'
By setting helperPath
, koa-pug will load all the modules that under sepecified folxder, and make them available on all templates.
helperPath
also could be an array including folders, files path, even moduleName: 'path/to/lib.js
mapping object. Also support node module as a helper, just like: '_': require('lodash')
// format-date.js, `koa-pug` will convert filename to camel case and use it as module name
module.exports = function (input) {
return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()
}
Equals to:
// Becasue of there is a `moduleName`, `koa-pug` will use it as module name instead of filename
module.exports = {
moduleName: 'formatDate',
moduleBody (input) {
return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()
}
}
Use help in Pug:
p= formatDate(new Date())
koa-pug
resolves Pug template filesLet's say the project views structure like:
views
├── user.pug
├── user
│ └── index.pug
└── file
└── index.pug
koa-pug
will search file in the following order:
<tpl_name>.pug
<tpl_name>/index.pug
When pug.render('user')
is called, views/user.pug
will be rendered. If you want to render views/user/index.pug
, you have to pass it to renderer explicitly: pug.render('user/index)
.
When pug.render('file')
is called, views/file/index.pug
will be rendered.
Via GitHub
模板引擎 模板引擎:模板引擎是web应用中动态生成html的工具,负责将数据和模板结合。 常见模板引擎有:ejs、jade(现更名为pug)、Handlebars、Nunjucks、Swig等; 使用模板引擎可以是项目结构更加清晰,结构更加合理,让项目维护变得更简单; pug模板引擎使用 安装pug npm i pug -g pug常用语法 pug语法:通过缩进关系,代替以往html的层级包含关系
koa2 的模板渲染有很多,比如pug(原 jade)、ejs 等等,这里以 pug 为例。同时使用 koa-views 中间件,将动态模板渲染集成到 koa2 中。 安装:npm install koa-views pug -s 新建一个简单的 server/index.js,代码如下: const Koa = require('koa'); const app = new Koa(); con
koa2-pug路由添加备忘方便自查 框架:koa2 模板引擎:pug 用koa2-generator 创建项目后,用pug做页面展现,配置路由,将页面简单串起来,可以互相跳转访问。 路由配置步骤: 1./views/文件夹下新建一个pug,比如test.pug 2./routes/ 文件夹下新建js文件,如:test.js const router = require ( 'koa-router
const Koa = require("koa"); const Router = require("koa-router"); const static = require("koa-static") const views = require("koa-views"); let data = require("./data/data.json"); let app = new Koa();
使用场景 在web应用中,我们常常使用session来管理会话,用一个sessionId来表明用户已登录。session结合cookie管理会话在web应用中是很常见的。 安装 在koa项目中 npm install koa-session2 简单使用 //app.js const Koa = require('koa') const app = new Koa() const views = r
Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren
koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 历史 1. Express Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: var express = require('express'); var app = express(); app.get('/', function (req, res) {
Koa 是下一代的 Node.js 的 Web 框架。由 Express 团队设计。旨在提供一个更小型、更富有表现力、更可靠的 Web 应用和 API 的开发基础。 Koa可以通过生成器摆脱回调,极大地改进错误处理。Koa核心不绑定任何中间件,但提供了优雅的一组可以快速和愉悦地编写服务器应用的方法。 示例代码: var koa = require('koa');var app = koa();//
Koa - HelloWorld 以上便是全部了,我们重点来看示例,我们只注册一个中间件, Hello Worler Server: <?php $app = new Application(); // ... $app->υse(function(Context $ctx) { $ctx->status = 200; $ctx->body = "<h1>Hello Worl
koa-log4js A wrapper for log4js-node which support Koa logger middleware.Log message is forked from Express (Connect) logger file. Note This branch is use to Koa v2.x.To use Koa v0.x & v1.x, please ch
koa-rudy 环境 node -v >=6.9.0pm2 启动 npm install npm run dev 开发环境 npm run dev || test || prod 接口测试 npm run mocha 推荐开发工具 vscode 实现 支持 async/await MVC架构(middleware-view-controller) RESTful a
学习 koa 源码的整体架构,浅析koa洋葱模型原理和co原理 1. 前言 你好,我是若川,微信搜索「若川视野」关注我,专注前端技术分享。欢迎加我微信ruochuan12,加群交流学习。 这是学习源码整体架构系列第七篇。整体架构这词语好像有点大,姑且就算是源码整体结构吧,主要就是学习是代码整体结构,不深究其他不是主线的具体函数的实现。本篇文章学习的是实际仓库的代码。 本文仓库地址:git clon
koa-seo SEO middleware for koa base on chrome-render, a substitute for prerender. Modern web app use technique like react.js vue.js which render html in browser, this lead to search engine can't crawl