Skip to content

Commit

Permalink
Merge pull request #537 from robgraeber/master
Browse files Browse the repository at this point in the history
Add chaining of env 'add' methods
  • Loading branch information
carljm committed Oct 2, 2015
2 parents b638d15 + 6fbebdd commit 17bdf4c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 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
9 changes: 5 additions & 4 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 support](#asynchronous-support)). Returns `env` for further method 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 methods that are called by the extension system. Returns `env` for further method chaining.
See [Custom Tags](#custom-tags).

{% endapi %}
Expand Down Expand Up @@ -272,7 +272,8 @@ Return true if a custom extension named **name** has been added.
{% api %}
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`.
Add a global value that will be available to all templates. Note: this will overwrite any existing global called `name`.
Returns `env` for further method chaining.
{% endapi %}

{% api %}
Expand Down Expand Up @@ -309,7 +310,7 @@ 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. Returns `env` for further method chaining.

```js
var app = express();
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 17bdf4c

Please sign in to comment.