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

Inheritable Blocks Sections #473

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 71 additions & 2 deletions mustache.js
Expand Up @@ -68,7 +68,7 @@
var spaceRe = /\s+/;
var equalsRe = /\s*=/;
var curlyRe = /\s*\}/;
var tagRe = /#|\^|\/|>|\{|&|=|!/;
var tagRe = /#|\^|\/|>|\{|&|=|!|\$|</;

/**
* Breaks up the given `template` string into a tree of tokens. If the `tags`
Expand Down Expand Up @@ -190,7 +190,7 @@
token = [ type, value, start, scanner.pos ];
tokens.push(token);

if (type === '#' || type === '^') {
if (type === '#' || type === '^' || type === '$' || type === '<') {
sections.push(token);
} else if (type === '/') {
// Check section nesting.
Expand Down Expand Up @@ -259,6 +259,8 @@
token = tokens[i];

switch (token[0]) {
case '$':
case '<':
case '#':
case '^':
collector.push(token);
Expand Down Expand Up @@ -344,6 +346,7 @@
*/
function Context (view, parentContext) {
this.view = view;
this.blocks = {};
this.cache = { '.': this.view };
this.parent = parentContext;
}
Expand All @@ -356,6 +359,42 @@
return new Context(view, this);
};

/**
* Set a value in the current block context.
*/
Context.prototype.setBlockVar = function set (name, value) {
var blocks = this.blocks;

blocks[name] = value;

return value;
};

/**
* Clear all current block vars.
*/
Context.prototype.clearBlockVars = function clearBlockVars () {
this.blocks = {};
};

/**
* Get a value only from the current block context.
*/
Context.prototype.getBlockVar = function getBlockVar (name) {
var blocks = this.blocks;

var value;
if (blocks.hasOwnProperty(name)) {
value = blocks[name];
} else {
if (this.parent) {
value = this.parent.getBlockVar(name);
}
}
// Can return undefined.
return value;
};

/**
* Returns the value of the given name in this context, traversing
* up the context hierarchy if the value is absent in this context's view.
Expand Down Expand Up @@ -478,6 +517,8 @@
if (symbol === '#') value = this.renderSection(token, context, partials, originalTemplate);
else if (symbol === '^') value = this.renderInverted(token, context, partials, originalTemplate);
else if (symbol === '>') value = this.renderPartial(token, context, partials, originalTemplate);
else if (symbol === '<') value = this.renderBlock(token, context, partials, originalTemplate);
else if (symbol === '$') value = this.renderBlockVariable(token, context, partials, originalTemplate);
else if (symbol === '&') value = this.unescapedValue(token, context);
else if (symbol === 'name') value = this.escapedValue(token, context);
else if (symbol === 'text') value = this.rawValue(token);
Expand Down Expand Up @@ -540,6 +581,34 @@
return this.renderTokens(this.parse(value), context, partials, value);
};

Writer.prototype.renderBlock = function renderBlock (token, context, partials, originalTemplate) {
if (!partials) return;

var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
if (value != null)
// Ignore any wrongly set block vars before we started.
context.clearBlockVars();
// We are only rendering to record the default block variables.
this.renderTokens(token[4], context, partials, originalTemplate);
// Now we render and return the result.
var result = this.renderTokens(this.parse(value), context, partials, value);
// Don't leak the block variables outside this include.
context.clearBlockVars();
return result;
};

Writer.prototype.renderBlockVariable = function renderBlockVariable (token, context, partials, originalTemplate) {
var value = token[1];

var exists = context.getBlockVar(value);
if (!exists) {
context.setBlockVar(value, originalTemplate.slice(token[3], token[5]));
return this.renderTokens(token[4], context, partials, originalTemplate);
} else {
return this.renderTokens(this.parse(exists), context, partials, exists);
}
};

Writer.prototype.unescapedValue = function unescapedValue (token, context) {
var value = context.lookup(token[1]);
if (value != null)
Expand Down
2 changes: 1 addition & 1 deletion mustache.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/_files/inheritance_context_cleaned.js
@@ -0,0 +1 @@
({ })
1 change: 1 addition & 0 deletions test/_files/inheritance_context_cleaned.mustache
@@ -0,0 +1 @@
{{$section}}rendered{{/section}}|{{<partial}}{{$section}}inherited{{/section}}{{/partial}}|{{$section}}rendered{{/section}}
1 change: 1 addition & 0 deletions test/_files/inheritance_context_cleaned.partial
@@ -0,0 +1 @@
{{$section}}ignored{{/section}}
1 change: 1 addition & 0 deletions test/_files/inheritance_context_cleaned.txt
@@ -0,0 +1 @@
rendered|inherited|rendered
1 change: 1 addition & 0 deletions test/_files/inheritance_empty_list_section.js
@@ -0,0 +1 @@
({ "inheritable": [] })
1 change: 1 addition & 0 deletions test/_files/inheritance_empty_list_section.mustache
@@ -0,0 +1 @@
<{{$inheritable}}rendered{{/inheritable}}>
1 change: 1 addition & 0 deletions test/_files/inheritance_empty_list_section.txt
@@ -0,0 +1 @@
<rendered>
1 change: 1 addition & 0 deletions test/_files/inheritance_false_section.js
@@ -0,0 +1 @@
({ "inheritable": false })
1 change: 1 addition & 0 deletions test/_files/inheritance_false_section.mustache
@@ -0,0 +1 @@
<{{$inheritable}}rendered{{/inheritable}}>
1 change: 1 addition & 0 deletions test/_files/inheritance_false_section.txt
@@ -0,0 +1 @@
<rendered>
1 change: 1 addition & 0 deletions test/_files/inheritance_missing_section.js
@@ -0,0 +1 @@
({ })
1 change: 1 addition & 0 deletions test/_files/inheritance_missing_section.mustache
@@ -0,0 +1 @@
{{$inheritable}}rendered{{/inheritable}}
1 change: 1 addition & 0 deletions test/_files/inheritance_missing_section.txt
@@ -0,0 +1 @@
rendered
1 change: 1 addition & 0 deletions test/_files/inheritance_no_new_context.js
@@ -0,0 +1 @@
({ "subject": "rendered", "inheritable": { "subject": "ignored" } })
1 change: 1 addition & 0 deletions test/_files/inheritance_no_new_context.mustache
@@ -0,0 +1 @@
<{{$inheritable}}{{subject}}{{/inheritable}}>
1 change: 1 addition & 0 deletions test/_files/inheritance_no_new_context.txt
@@ -0,0 +1 @@
<rendered>
1 change: 1 addition & 0 deletions test/_files/inheritance_only_once.js
@@ -0,0 +1 @@
({ "inheritable": [0,1,2,3] })
1 change: 1 addition & 0 deletions test/_files/inheritance_only_once.mustache
@@ -0,0 +1 @@
<{{$inheritable}}rendered{{/inheritable}}>
1 change: 1 addition & 0 deletions test/_files/inheritance_only_once.txt
@@ -0,0 +1 @@
<rendered>
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed.js
@@ -0,0 +1 @@
({ })
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed.mustache
@@ -0,0 +1 @@
before {{<partial}}ignored{{/partial}} after
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed.partial
@@ -0,0 +1 @@
partial
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed.txt
@@ -0,0 +1 @@
before partial after
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed_section.js
@@ -0,0 +1 @@
({ "items": [0,1,2,3] })
@@ -0,0 +1 @@
{{#items}}{{<partial}}ignored{{/partial}}{{/items}}
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed_section.partial
@@ -0,0 +1 @@
{{.}}
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_embed_section.txt
@@ -0,0 +1 @@
0123
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_override.js
@@ -0,0 +1 @@
({ })
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_override.mustache
@@ -0,0 +1 @@
{{<partial}}{{$inheritable}}inherited{{/inheritable}}{{/partial}}
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_override.partial
@@ -0,0 +1 @@
{{$inheritable}}ignored{{/inheritable}}
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_override.txt
@@ -0,0 +1 @@
inherited
@@ -0,0 +1 @@
({ })
@@ -0,0 +1 @@
{{<partial}}{{$inheritable}}inherited{{/inheritable}}{{/partial}}
@@ -0,0 +1 @@
{{$inheritable}}ignored{{/inheritable}} {{$inheritable}}ignored{{/inheritable}}
@@ -0,0 +1 @@
inherited inherited
@@ -0,0 +1 @@
({ "items": [0,1,2,3] })
@@ -0,0 +1 @@
{{<partial}}{{$inheritable}}{{.}}{{/inheritable}}{{/partial}}
@@ -0,0 +1 @@
{{#items}}{{$inheritable}}ignored{{/inheritable}}{{/items}}
@@ -0,0 +1 @@
0123
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_can_override_recursive.js
@@ -0,0 +1 @@
({ "content": "X", "nodes": [ { "content": "Y", "nodes": [] } ] })
@@ -0,0 +1 @@
<{{<partial}}{{/partial}}>
@@ -0,0 +1 @@
{{content}}<{{#nodes}}{{>partial}}{{/nodes}}>
@@ -0,0 +1 @@
<X<Y<>>>
@@ -0,0 +1 @@
({ "content": "X", "nodes": [ { "content": "Y", "nodes": [] } ] })
@@ -0,0 +1 @@
<{{<partial}}{{$value}}{{>partial}}{{/value}}{{/partial}}>
@@ -0,0 +1 @@
{{content}}<{{#nodes}}{{$value}}overriden{{/value}}{{/nodes}}>
@@ -0,0 +1 @@
<X<Y<>>>
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_cannot_overide.js
@@ -0,0 +1 @@
({ "section": true })
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_cannot_overide.mustache
@@ -0,0 +1 @@
{{<partial}}{{$section}}inherited{{/section}}{{/partial}}
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_cannot_overide.partial
@@ -0,0 +1 @@
{{#section}}success{{/section}}
1 change: 1 addition & 0 deletions test/_files/inheritance_partials_cannot_overide.txt
@@ -0,0 +1 @@
success