Git Product home page Git Product logo

underscore-express's Introduction

underscore-express

Use Underscore templates easily in Express.

Install

This package is registered in npm as underscore-express, so a simple...

npm install underscore-express

...will do it.

Usage

In your Express app setup...

// To use the default 'tmpl' extension...
require('underscore-express')(app);
// Or set your own...
require('underscore-express')(app, 'ut');

...and that's it!

Including Subtemplates

underscore-express comes with a baked in include method. Here's an example...

views/header.tmpl

<html>

  <head>
    <title>Header!</title>
  </head>

  <body>

views/footer.tmpl

  </body>
</html>

views/index.tmpl

<%= include('header') %>
    Welcome to my homepage!
<%= include('footer') %>

app.js

res.render('index');

RESULT

<html>

  <head>
    <title>Header!</title>
  </head>

  <body>
    Welcome to my homepage!
  </body>
</html>

include is relative to the file it is called from. Feel free to use relative paths like ../../some/other/subtemplate.

underscore-express's People

Contributors

caseywebdev avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nelsonic cefn

underscore-express's Issues

Include cannot access variables in scope

It looks like scope variables can't be accessed given the existing implementation of include, is that right?

I think it means that iteration like the following is impossible within a 'parent' template, since it's only the options which are made available to the subtemplate, and it's currently impossible to modify the options themselves.

        _.forIn(questions, function(question, questionIdx){
            var answer = answers[questionIdx]
            print(include("editor_form_field"));
        })

Underscore-Express blows chunks when errors in included templates

Thanks for underscore-express.

It's a real inspiration and is already fixing some serious architectural issues for me, by being able to wrangle my own 'DSL' for my app's very specific templating needs.

However, there's a missing callback argument in the embedded call to render made by the include(...) function. Because there's no cb passed in, the sync error handling clause is hit and the node server gets taken down by errors in sub-templates.

To fix it, you just change the line...

var str = render(path.resolve(dir, rel + '.' + ext), options);

...to read...

var str = render(path.resolve(dir, rel + '.' + ext), options, cb);

...and then the error handling gets passed down the middleware path to the proper location.

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.