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 more APIs to manage extensions of environement #512

Merged
merged 5 commits into from
Sep 15, 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
22 changes: 18 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)). See
[Custom Filters](#custom-filters).

{% endapi %}
Expand All @@ -249,12 +249,26 @@ See [Custom Tags](#custom-tags).

{% endapi %}

{% api %}
removeExtension
env.removeExtension(name)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the blank line here and not in the other api blocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For removeExtension, I copied/paste the doc for addExtension, which contain blank line. For hasExtension, I copied the one from getExtension, which doesn't contain blank lines.

Remove a previously added custom extension named **name**.

{% endapi %}

{% api %}
getExtension
env.getExtension(name)
Get an extension named **name**.
{% endapi %}

{% api %}
hasExtension
env.hasExtension(name)
Return true if a custom extension named **name** has been added.
{% endapi %}

{% api %}
addGlobal
env.addGlobal(name, value)
Expand Down Expand Up @@ -444,7 +458,7 @@ MyLoader.prototype.getSource = function(name) {
It can get a little more complex. If you want to track updates to
templates and bust the internal cache so that you can see updates, you
need to extend the `Loader` class. This gives you `emit` method that
can fire events. You need to call it
can fire events. You need to call it

```js
var MyLoader = nunjucks.Loader.extend({
Expand All @@ -453,7 +467,7 @@ var MyLoader = nunjucks.Loader.extend({
// and call `this.emit('update', name)` when a template
// is changed
}

getSource: function(name) {
// load the template
}
Expand All @@ -474,7 +488,7 @@ asynchronously.
```js
var MyLoader = nunjucks.Loader.extend({
async: true,

getSource: function(name, callback) {
// load the template
// ...
Expand Down
12 changes: 12 additions & 0 deletions src/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,22 @@ var Environment = Obj.extend({
this.extensionsList.push(extension);
},

removeExtension: function(name) {
var extension = this.getExtension(name);
if (!extension) return;

this.extensionsList = lib.without(this.extensionsList, extension);
delete this.extensions[name];
},

getExtension: function(name) {
return this.extensions[name];
},

hasExtension: function(name) {
return !!this.extensions[name];
},

addGlobal: function(name, value) {
globals[name] = value;
},
Expand Down