All Projects → queckezz → Koa Views

queckezz / Koa Views

Licence: mit
Template rendering middleware for koa (hbs, swig, pug, anything! ✨)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Koa Views

Art Template
High performance JavaScript templating engine
Stars: ✭ 9,554 (+1300.88%)
Mutual labels:  koa, template-engine
Kov Blog
A blog platform built with koa,vue and mongoose. 使用 koa ,vue 和 mongo 搭建的博客页面和支持markdown语法的博客编写平台,自动保存草稿。博客地址:https://chuckliu.me
Stars: ✭ 635 (-6.89%)
Mutual labels:  koa
Graphql Pokemon
Get information of a Pokémon with GraphQL!
Stars: ✭ 441 (-35.34%)
Mutual labels:  koa
Koa Helmet
Important security headers for koa
Stars: ✭ 595 (-12.76%)
Mutual labels:  koa
Carbone
Fast and simple report generator, from JSON to pdf, xslx, docx, odt...
Stars: ✭ 487 (-28.59%)
Mutual labels:  template-engine
Latte
☕ Latte: the intuitive and fast template engine for those who want the most secure PHP sites.
Stars: ✭ 616 (-9.68%)
Mutual labels:  template-engine
Fenom
Template Engine for PHP
Stars: ✭ 426 (-37.54%)
Mutual labels:  template-engine
Blade
🚀 Lightning fast and elegant mvc framework for Java8
Stars: ✭ 5,569 (+716.57%)
Mutual labels:  template-engine
Handlebars Rust
Rust templating with Handlebars
Stars: ✭ 632 (-7.33%)
Mutual labels:  template-engine
Scalate
Scalate is a Scala based template engine which supports HAML, Mustache and JSP, Erb and Velocity style syntaxes.
Stars: ✭ 570 (-16.42%)
Mutual labels:  template-engine
Blade
🔪 A standalone version of Laravel's Blade templating engine for use outside of Laravel.
Stars: ✭ 542 (-20.53%)
Mutual labels:  template-engine
Twirl
Twirl is Play's default template engine
Stars: ✭ 498 (-26.98%)
Mutual labels:  template-engine
Ejsexcel
nodejs excel template engine. node export excel
Stars: ✭ 621 (-8.94%)
Mutual labels:  template-engine
Koa2 Note
《Koa2进阶学习笔记》已完结🎄🎄🎄
Stars: ✭ 4,725 (+592.82%)
Mutual labels:  koa
Node Blog
🚀《Node.js从入门到上线》A blog build with Koa2.
Stars: ✭ 640 (-6.16%)
Mutual labels:  koa
Create Graphql
Command-line utility to build production-ready servers with GraphQL.
Stars: ✭ 441 (-35.34%)
Mutual labels:  koa
Grmustache.swift
Flexible Mustache templates for Swift
Stars: ✭ 538 (-21.11%)
Mutual labels:  template-engine
J2html
Java to HTML generator. Enjoy typesafe HTML generation.
Stars: ✭ 604 (-11.44%)
Mutual labels:  template-engine
Lin Cms Koa
🌀使用Node.JS KOA构建的CMS开发框架
Stars: ✭ 649 (-4.84%)
Mutual labels:  koa
Liquidjs
A simple, expressive, safe and Shopify compatible template engine in pure JavaScript.
Stars: ✭ 638 (-6.45%)
Mutual labels:  template-engine

koa-views

koa-views NPM version NPM downloads Dependency Status License

Template rendering middleware for [email protected].

Installation

npm install koa-views

Templating engines

koa-views is using consolidate under the hood.

List of supported engines

NOTE: you must still install the engines you wish to use, add them to your package.json dependencies.

Example

var views = require('koa-views');

const render = views(__dirname + '/views', {
  map: {
    html: 'underscore'
  }
})

// Must be used before any router is used
app.use(render)
// OR Expand by app.context
// No order restrictions
// app.context.render = render()

app.use(async function (ctx) {
  ctx.state = {
    session: this.session,
    title: 'app'
  };

  await ctx.render('user', {
    user: 'John'
  });
});

For more examples you can take a look at the tests.

Simple middleware

If you need to simply render pages with locals, you can install koa-views-render:

npm install koa-views-render

Then simply use it on your routes and its arguments will be passed to ctx.render.

var render = require('koa-views-render');

// ...

app.use(render('home', { title : 'Home Page' }));

API

views(root, opts)

  • root: Where your views are located. Must be an absolute path. All rendered views are relative to this path

  • opts (optional)

  • opts.autoRender: Whether to use ctx.body to receive the rendered template string. Defaults to true.

const render = views(__dirname, { autoRender: false, extension: 'pug' });
app.use(render)
// OR
// app.context.render = render()

app.use(async function (ctx) {
  return await ctx.render('user.pug')
})

vs.

const render = views(__dirname, { extension: 'pug' })
app.use(render)
// OR
// app.context.render = render()

app.use(async function (ctx) {
  await ctx.render('user.pug')
})
  • opts.extension: Default extension for your views

Instead of providing the full file extension you can omit it.

app.use(async function (ctx) {
  await ctx.render('user.pug')
})

vs.

const render = views(__dirname, { extension: 'pug' })
app.use(render)
// OR
// app.context.render = render()

app.use(async function (ctx) {
  await ctx.render('user')
})
  • opts.map: Map a file extension to an engine

In this example, each file ending with .html will get rendered using the nunjucks templating engine.

const render = views(__dirname, { map: {html: 'nunjucks' }})
app.use(render)
// OR
// app.context.render = render()
// render `user.html` with nunjucks
app.use(async function (ctx) {
  await ctx.render('user.html')
})
  • opts.engineSource: replace consolidate as default engine source

If you’re not happy with consolidate or want more control over the engines, you can override it with this options. engineSource should be an object that maps an extension to a function that receives a path and options and returns a promise. In this example templates with the foo extension will always return bar.

const render = views(__dirname, { engineSource: {foo: () => Promise.resolve('bar')}})
app.use(render)
// OR
// app.context.render = render()

app.use(async function (ctx) {
  await ctx.render('index.foo')
})
  • opts.options: These options will get passed to the view engine. This is the time to add partials and helpers etc.
const app = new Koa()
  .use(views(__dirname, {
    map: { hbs: 'handlebars' },
    options: {
      helpers: {
        uppercase: (str) => str.toUpperCase()
      },

      partials: {
        subTitle: './my-partial' // requires ./my-partial.hbs
      },
      
      cache: true // cache the template string or not
    }
  }))
  .use(function (ctx) {
    ctx.state = { title: 'my title', author: 'queckezz' }
    return ctx.render('./my-view.hbs')
  })

Debug

Set the DEBUG environment variable to koa-views when starting your server.

$ DEBUG=koa-views

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].