jpettersson/lingon — pull request #81 by jpettersson (2014-09-01 21:29:55+00:00)
Title: Fixing the EJS context

I've made tweaks to how the rendering context works. I fixed several bugs and made it more consistent. From the USAGE.md docs:

## Render EJS templates

Lingon comes with out of the box support for EJS templates using the [gulp-ejs](https://github.com/rogeriopvl/gulp-ejs) module.

### Render data with EJS

The Lingon instance has a property `lingon.global` which is available in all EJS templates during rendering. The object can be accessed from all EJS templates & layouts.

**Example: lingon.js**

```js
var lingon = require('lingon');

lingon.global.name = "bob";
```

**Example: index.ejs**

```html

<%= global.name %>

```

### File specific context

EJS templates also have access to a `context` object that contains information about the current file being processed.

Name | Description
-----|------------
file | *Path to current file being rendered*
layout | *Path to current layout (null if no layout is used)*
template | *Path to the current source file**

**"source file" refers to the file being written to the build path after partials and layouts have been included.*

The `context` object can also be extended with custom properties in a pre-processor. For example, use this to get the shasum of each file and write it in the template.

**Example: lingon.js**
```js
var lingon = require('lingon');
var es = require('event-stream');
var spawn = require('child_process').spawn;

lingon.preProcessors.unshift('ejs', function(global, context) {
return es.map(function(file, cb) {
var shasum = spawn('shasum', [
context.file
]);

shasum.stdout.on('data', function (data) {
context.shasum = data.toString().trim();
});

shasum.on('close', function (data) {
cb(null, file);
});
});
});
```

**Example: home.ejs**
```html

file: <%= context.file %>

layout: <%= context.layout %>

template: <%= context.template %>

metadata: <%= context.shasum %>

```