Giter VIP home page Giter VIP logo

co-views's Introduction

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').

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 perform. 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

co-views's People

Contributors

armed avatar chunpu avatar queckezz avatar simov avatar steambap avatar timmarinin avatar tj avatar vendethiel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

co-views's Issues

Nunjucks support

It will be nice to have a new version of co-views/co-render that support the Nunjucks engine. Since Consolidate.js 0.10.0 already supports it, I suppose is just a matter of change dependencies, but I do not know the impact of this change.

how to debug jade?

How can I get express like jade error page, so I can get the line number of error.

relative path error with ninjucks and extending base templates

If you extend a base layout using nunjucks with co-views, it seems to lose the relative path and requires you to set the full path

For instance, this template will only work if you use the full absolute path to layout.html, otherwise it will error Error: template not found: layout.html

render.js

var views = require('co-views');
module.exports = views(__dirname + '/../views', {
    map: { html: 'nunjucks', default:'html' }
});

index.html

{% extends "layout.html" %}
{% block body %}
<h1>{{ title }}</h1>
This is a test page with a layout
{% endblock %}

Options for engines

Hello, I ran into situation when ect was throwing an error because it couldn't locate parent template ex:<% extend 'layout.html' %>. I was simply trying to read layout.html an of course failing. That happens because there is no way right now to pass options to engines. So... I found two ways of fixing the issue - 1) fix it on ECT side and check if root path wasn't set figure out prefix path for the first template which was passed into render function, get beginning path and assign it to the root. 2) Add engine settings parameter to render in co-views, co-render engine call and ect within consolidate (with proper fallback for backwards compatibility).
First approach would require less changes, but doesn't fix the root of the issue. Second approach opens possibility to adjust other engines as well.

I already have fixes and can create requests. Please let me know what you think.

I'll also reference that issue at ect repository to see what creator thinks about it.

Thank you

Should template functions be cached when NODE_ENV is "test"?

This threw me off for a bit when using supertest and koa. When the cache is on I get something like:

2 >> dispatch
status: undefined Not Found
header:
x-powered-by: koa
body: undefined

It would be good to understand why this happens too.

Views won't reload except on server restart

(I don't know if this is the right place to post this)

I'm using Koa and co-views together but then I make an update to a view (I use HTML views) and reload the browser nothing happens. I need to restart the server to see the updates I've made. I'm running in development mode so there shouldn't be any caching.

What am I doing wrong? Is there a way to make the views update without restarting the server while developing?

Thanks!

Trouble changing templating engine

Here is my server.js

var logger = require('koa-logger');
var route = require('koa-route');
var views = require('co-views');
var koa = require('koa');

app = koa();

app.use(logger());

app.use(route.get('/', index));
app.use(route.get('/about', about));

var render = views(__dirname + '/views', { map: { html: 'swig' }});

function *index() {
  this.body = yield render('index');
}

function *about() {
  this.body = yield render('about');
}

app.listen(3000);
console.log("App is now running at localhost:3000");

My trouble seems to come on line 13

var render = views(__dirname + '/views', { map: { html: 'swig' }});

I don't want to use any templating engine, or if I have to I'd prefer handlebars. The docs say that the default template extension is html, but if I remove that { map: { html: 'swig' }} I just get 500 errors. I've tried passing in { map: { html: 'html' }}, removing map and passing in default: 'html', just passing in an empty object, and can't seem to figure out how to just use nothing. Everything I've tried gives me a 500 error, but it works perfectly as intended with { map: { html: 'swig' }} included. Thanks for your help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.