Skip to content

Commit

Permalink
Add chaining of env 'add' methods
Browse files Browse the repository at this point in the history
  • Loading branch information
robgraeber committed Oct 2, 2015
1 parent b638d15 commit f36a63a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
master (unreleased)
-------------------

* Add support for chaining of addGlobal, addFilter, etc. Thanks Rob Graeber. Merge of
[#537](https://github.com/mozilla/nunjucks/pull/537)
* Fix error propagation. Thanks Tom Delmas. Merge of
[#534](https://github.com/mozilla/nunjucks/pull/534).

Expand Down
10 changes: 5 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ env.addFilter(name, func, [async])

Add a custom filter named **name** which calls **func** whenever
invoked. If the filter needs to be async, **async** must be `true`
(see [asynchronous support](#asynchronous-support)). See
(see [asynchronous #asynchronous-support)). Method returns `env` to allow for further chaining. See
[Custom Filters](#custom-filters).

{% endapi %}
Expand All @@ -244,7 +244,7 @@ addExtension
env.addExtension(name, ext)

Add the custom extension **ext** named **name**. **ext** is an object
with a few specific methods that are called by the extension system.
with a few specific at are called by the extension system. Method returns `env` to allow for further chaining.
See [Custom Tags](#custom-tags).

{% endapi %}
Expand Down Expand Up @@ -273,6 +273,7 @@ Return true if a custom extension named **name** has been added.
addGlobal
env.addGlobal(name, value)
Add a global value that will be available to all templates. Note: this will overwrite any existing global called `name`.
Method returns `env` for further chaining.
{% endapi %}

{% api %}
Expand Down Expand Up @@ -309,11 +310,10 @@ env.express(app)
Install nunjucks as the rendering engine for the express **app**.
After doing this, you can use express normally. Note that you can do
this automatically with the simple API call [`configure`](#configure)
by passing in the app as the **express** option.
by passing in the app as the **express** option. Method returns `env` to allow for further chaining.

```js
var app = express();
env.express(app);
var app = ex nv.express(app);

app.get('/', function(req, res) {
res.render('index.html');
Expand Down
5 changes: 5 additions & 0 deletions src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var Environment = Obj.extend({
extension._name = name;
this.extensions[name] = extension;
this.extensionsList.push(extension);
return this;
},

removeExtension: function(name) {
Expand All @@ -117,6 +118,7 @@ var Environment = Obj.extend({

addGlobal: function(name, value) {
globals[name] = value;
return this;
},

getGlobal: function(name) {
Expand All @@ -133,6 +135,7 @@ var Environment = Obj.extend({
this.asyncFilters.push(name);
}
this.filters[name] = wrapped;
return this;
},

getFilter: function(name) {
Expand Down Expand Up @@ -267,6 +270,7 @@ var Environment = Obj.extend({
};

app.set('view', NunjucksView);
return this;
},

render: function(name, ctx, cb) {
Expand Down Expand Up @@ -348,6 +352,7 @@ var Context = Obj.extend({
addBlock: function(name, block) {
this.blocks[name] = this.blocks[name] || [];
this.blocks[name].push(block);
return this;
},

getBlock: function(name) {
Expand Down
15 changes: 15 additions & 0 deletions tests/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@
finish(done);
});

it('should allow chaining of globals', function(done) {
var env = new Environment(new Loader(templatesPath));

env.addGlobal('hello', function(arg1) {
return 'Hello ' + arg1;
}).addGlobal('goodbye', function(arg1) {
return 'Goodbye ' + arg1;
});

equal('{{ hello("World!") }}', 'Hello World!');
equal('{{ goodbye("World!") }}', 'Goodbye World!');

finish(done);
});

it('should allow getting of globals', function(done) {
var env = new Environment(new Loader(templatesPath));
var hello = function(arg1) {
Expand Down

0 comments on commit f36a63a

Please sign in to comment.