Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Fix multiple redefinition of block-level tags
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Aug 29, 2013
1 parent 709a06c commit 2270510
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/swig.js
Expand Up @@ -359,11 +359,6 @@ exports.Swig = function (opts) {
* @private
*/
function remapBlocks(blocks, tokens) {
utils.each(blocks, function (block) {
if (block.name !== 'block') {
tokens.unshift(block);
}
});
return utils.map(tokens, function (token) {
var args = token.args ? token.args.join('') : '';
if (token.name === 'block' && blocks[args]) {
Expand All @@ -376,6 +371,21 @@ exports.Swig = function (opts) {
});
}

/**
* Import block-level tags to the token list that are not actual block tags.
* @param {array} blocks List of block-level tags.
* @param {array} tokens List of tokens to render.
* @return {undefined}
* @private
*/
function importNonBlocks(blocks, tokens) {
utils.each(blocks, function (block) {
if (block.name !== 'block') {
tokens.unshift(block);
}
});
}

/**
* Recursively compile and get parents of given parsed token object.
*
Expand Down Expand Up @@ -415,6 +425,7 @@ exports.Swig = function (opts) {
l = parents.length;
for (l = parents.length - 2; l >= 0; l -= 1) {
parents[l].tokens = remapBlocks(parents[l].blocks, parents[l + 1].tokens);
importNonBlocks(parents[l].blocks, parents[l].tokens);
}

return parents;
Expand Down Expand Up @@ -449,6 +460,7 @@ exports.Swig = function (opts) {
if (parents.length) {
// Remap the templates first-parent's tokens using this template's blocks.
tokens.tokens = remapBlocks(tokens.blocks, parents[0].tokens);
importNonBlocks(tokens.blocks, tokens.tokens);
}

tpl = new Function('_swig', '_ctx', '_filters', '_utils', '_fn',
Expand Down
1 change: 1 addition & 0 deletions lib/tags/macro.js
Expand Up @@ -72,3 +72,4 @@ exports.parse = function (str, line, parser, types, stack) {
};

exports.ends = true;
exports.block = true;
2 changes: 2 additions & 0 deletions tests/cases/extends_layouts/extends_bare.html
@@ -0,0 +1,2 @@
{% block content %}
{% endblock %}
8 changes: 8 additions & 0 deletions tests/cases/macros.expectation.html
@@ -0,0 +1,8 @@


I like burritos!


I like burritos!<p>


11 changes: 11 additions & 0 deletions tests/cases/macros.test.html
@@ -0,0 +1,11 @@
{% extends "./extends_layouts/extends_bare.html" %}

{% import "./macros.html" as m %}
{% macro burritos() %}
I like burritos!
{% endmacro %}

{% block content %}
{{ burritos() }}
{{ m.burritos() }}
{% endblock %}

0 comments on commit 2270510

Please sign in to comment.