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

co-views-ext

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-views-ext

Higher level thunk-based template rendering for Co and others, built on co-render

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

co-views-ext

This is a fork of co-views with the changes of co-views-helpers merged in, as co-views-helpers is out of date. Ext stands for extended.

Get it on npm with:

npm install co-views-ext

co-views

Template rendering for co using co-render. This module provides higher level sugar than co-render to reduce redundancy, for example specifying a views directory and default extension name.

Installation

$ npm install co-views

And install whichever engine(s) you use:

$ npm install ejs jade

Options

  • map an object mapping extension names to engine names [{}]
  • default default extension name to use when missing [html]
  • cache cached compiled functions [NODE_ENV != 'development']

map

For example if you wanted to use "swig" for .html files you would simply pass:

{ map: { html: 'swig' } }

default

Set the default template extension when none is passed to the render function. This defaults to "html". For example if you mostly use Jade, then you'd likely want to assign this to:

{ default: 'jade' }

Allowing you to invoke render('user') instead of render('user.jade').

helpers

When using Handlebars as your rendering templates, Helper registration failed on older versions of co-views, but this fork allows you to pass these helpers into views() when exporting the render function. For example, if you wanted to add an authentication helper gate to your views:

/*
Gate Helper, to allow use of the same views
for both authenticated and non-authenticated users.

{{gate userObj}}
  <p>Logged In!</p>
{{else}}
  <p>Not Logged In!</p>
{{/gate}}
*/
var gateHelper = function(user, options) {
  if(user) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
};

module.exports = views(__dirname + '/path/to/views', {
  map: { html: 'handlebars' },
  helpers: {
    gate: gateHelper
  }
});

cache

When true compiled template functions will be cached in-memory, this prevents subsequent disk i/o, as well as the additional compilation step that most template engines peform. By default this is enabled when the NODE_ENV environment variable is anything but "development", such as "stage" or "production".

Example

Render several users with different template engines in parallel. View lookup is performed relative to the ./examples directory passed, and the "swig" engine is mapped to ".html" files.

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

var render = views('examples', {
  map: { html: 'swig' }
});

var tobi = {
  name: 'tobi',
  species: 'ferret'
};

var loki = {
  name: 'loki',
  species: 'ferret'
};

var luna = {
  name: 'luna',
  species: 'cat'
};

co(function *(){
  var a = render('user', { user: tobi });
  var b = render('user.jade', { user: loki });
  var c = render('user.ejs', { user: luna });
  var html = yield [a, b, c];
  html = html.join('');
  console.log(html);
});

App-wide views

Dependending on your choice of application structure, you may wish to share these same settings between all of your application, instead of constantly initializing co-views. To do this simply create a views.js module and export the render function returned:

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

module.exports = views('views', {
  map: {
    html: 'swig',
    md: 'hogan'
  }
});

License

MIT

Keywords

FAQs

Package last updated on 17 Aug 2015

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