Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-jade

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-jade

A Jade middleware for Koa

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Koa-jade

A Jade middleware for Koa.

How to use

npm install koa-jade --save
var koa = require('koa')
  , jade = require('koa-jade')
  , app = koa()

app.use(jade.middleware({
  viewPath: __dirname + '/views',
  debug: false,
  pretty: false,
  compileDebug: false,
  locals: global_locals_for_all_pages,
  helperPath: 'path/to/jade/helpers'
}))

app.use(function* () {
  yield this.render('index', locals_for_this_page, true)
})

app.listen(3000)

Options

viewPath: where Jade templates be stored. Default is process.cwd().

pretty and compileDebug: see Jade's docs. Default is false.

debug: shorthand for pretty and compileDebug. Default is false.

locals: variables that will be passed to Jade templates.

noCache: if true, re-compile templates when page refreshed; if false, use cached compiler first. Can be overrided by render's force.

helperPath: where to load helpers, and make them available on all .jade.

Methods

middleware(options): configure and create a middleware.

render(tpl, locals, force): render tpl with locals.

By default, koa-jade stores the results of Jade.compile as caches. If want to control it manually, use the third argument - force:

true: force to re-compile template instead of use cached compiler.

false: force to use cached compiler first.

force can be ommited.

Content-Type

Koa-jade sets content-type to text/html automatically. if wanna change it, do like this:

yield this.render('index')
this.type = 'text/plain'

Global Helpers

By setting helperPath, koa-jade will load all the modules that under sepecified folder, and make them available on all templates.

Defining Helper

// format-date.js
module.exports = function (input) {
    if (input instanceof Date) {
      return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()
    }

    return input
}

It equals to:

// whatever.js
module.exports = {
  moduleName: 'formatDate',
  moduleBody: function (input) {
    if (input instanceof Date) {
      return (input.getMonth() + 1) + '/' + input.getDate() + '/' + input.getFullYear()
    }

    return input
  }
}

In Jade:

p= formatDate(new Date())

Todo

  • cache for generated HTML file
  • tests

Contributors

Keywords

FAQs

Package last updated on 31 Jul 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc