Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chaining of env 'add' methods #537

Merged
merged 1 commit into from
Oct 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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