Skip to content
cstivers78 edited this page Jan 13, 2012 · 3 revisions

Template Context

A context is an object whose members will be exposed as globals within the context of your template. The render() function is added to every template via the context.

To add your own values to the context, you at two levels: Bliss and Compile.

Bliss-level Context

When you instantiate a new Bliss object, you can provide an object as the constructor parameter. If the object contains a field called context, then the members of context will be added to the template context for all templates compiled using this instance of Bliss.

The following is an example of exposing underscore.js to templates:

_ = require('underscore');
bliss = new Bliss({context:{
  _: _
}});

In the template:

<ul>
@_.each(members,function(member){
    <li>@member</li>
})
</ul>

Compile-level Context

Alternatively, you can provide an object as the second parameters to the compile() function. If the object contains a field called context, then the members of context will be added to the template context for that compile.

The following is an example of exposing sass to templates:

sass = require('sass');
bliss = new Bliss();
bliss.compile({context:{
  sass: sass.render.bind(sass)
}});

In the template:

<style>
@sass('\
  $blue: #3bbfce;\
  a {\
    color: $blue;\
  }\
')
</style>
Clone this wiki locally