Skip to content

Commit

Permalink
Addresses twigjs#63 - Add spaceless tag and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
justjohn committed Feb 17, 2013
1 parent e27b897 commit 3a3a15f
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 6 deletions.
35 changes: 34 additions & 1 deletion demos/node_express/public/vendor/twig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,9 @@ var Twig = (function (Twig) {
endblock: 'Twig.logic.type.endblock',
extends_: 'Twig.logic.type.extends',
use: 'Twig.logic.type.use',
include: 'Twig.logic.type.include'
include: 'Twig.logic.type.include',
spaceless: 'Twig.logic.type.spaceless',
endspaceless: 'Twig.logic.type.endspaceless'
};


Expand Down Expand Up @@ -2116,6 +2118,37 @@ var Twig = (function (Twig) {
output: template.render(innerContext)
};
}
},
{
type: Twig.logic.type.spaceless,
regex: /^spaceless$/,
next: [
Twig.logic.type.endspaceless
],
open: true,

// Parse the html and return it without any spaces between tags
parse: function (token, context, chain) {
var // Parse the output without any filter
unfiltered = Twig.parse.apply(this, [token.output, context]),
// A regular expression to find closing and opening tags with spaces between them
rBetweenTagSpaces = />\s+</g,
// Replace all space between closing and opening html tags
output = unfiltered.replace(rBetweenTagSpaces,'><').trim();

return {
chain: chain,
output: output
};
}
},

// Add the {% endspaceless %} token
{
type: Twig.logic.type.endspaceless,
regex: /^endspaceless$/,
next: [ ],
open: false
}
];

Expand Down
35 changes: 34 additions & 1 deletion demos/twitter_backbone/vendor/twig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,9 @@ var Twig = (function (Twig) {
endblock: 'Twig.logic.type.endblock',
extends_: 'Twig.logic.type.extends',
use: 'Twig.logic.type.use',
include: 'Twig.logic.type.include'
include: 'Twig.logic.type.include',
spaceless: 'Twig.logic.type.spaceless',
endspaceless: 'Twig.logic.type.endspaceless'
};


Expand Down Expand Up @@ -2116,6 +2118,37 @@ var Twig = (function (Twig) {
output: template.render(innerContext)
};
}
},
{
type: Twig.logic.type.spaceless,
regex: /^spaceless$/,
next: [
Twig.logic.type.endspaceless
],
open: true,

// Parse the html and return it without any spaces between tags
parse: function (token, context, chain) {
var // Parse the output without any filter
unfiltered = Twig.parse.apply(this, [token.output, context]),
// A regular expression to find closing and opening tags with spaces between them
rBetweenTagSpaces = />\s+</g,
// Replace all space between closing and opening html tags
output = unfiltered.replace(rBetweenTagSpaces,'><').trim();

return {
chain: chain,
output: output
};
}
},

// Add the {% endspaceless %} token
{
type: Twig.logic.type.endspaceless,
regex: /^endspaceless$/,
next: [ ],
open: false
}
];

Expand Down
35 changes: 34 additions & 1 deletion src/twig.logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ var Twig = (function (Twig) {
endblock: 'Twig.logic.type.endblock',
extends_: 'Twig.logic.type.extends',
use: 'Twig.logic.type.use',
include: 'Twig.logic.type.include'
include: 'Twig.logic.type.include',
spaceless: 'Twig.logic.type.spaceless',
endspaceless: 'Twig.logic.type.endspaceless'
};


Expand Down Expand Up @@ -597,6 +599,37 @@ var Twig = (function (Twig) {
output: template.render(innerContext)
};
}
},
{
type: Twig.logic.type.spaceless,
regex: /^spaceless$/,
next: [
Twig.logic.type.endspaceless
],
open: true,

// Parse the html and return it without any spaces between tags
parse: function (token, context, chain) {
var // Parse the output without any filter
unfiltered = Twig.parse.apply(this, [token.output, context]),
// A regular expression to find closing and opening tags with spaces between them
rBetweenTagSpaces = />\s+</g,
// Replace all space between closing and opening html tags
output = unfiltered.replace(rBetweenTagSpaces,'><').trim();

return {
chain: chain,
output: output
};
}
},

// Add the {% endspaceless %} token
{
type: Twig.logic.type.endspaceless,
regex: /^endspaceless$/,
next: [ ],
open: false
}
];

Expand Down
14 changes: 14 additions & 0 deletions test/test.tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var Twig = Twig || require("../twig"),
twig = twig || Twig.twig;

describe("Twig.js Tags ->", function() {

it("should support spaceless", function() {
twig({
data: "{% spaceless %}<div>\n <b>b</b> <i>i</i>\n</div>{% endspaceless %}"
}).render().should.equal(
"<div><b>b</b><i>i</i></div>"
);
});

});
35 changes: 34 additions & 1 deletion twig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,9 @@ var Twig = (function (Twig) {
endblock: 'Twig.logic.type.endblock',
extends_: 'Twig.logic.type.extends',
use: 'Twig.logic.type.use',
include: 'Twig.logic.type.include'
include: 'Twig.logic.type.include',
spaceless: 'Twig.logic.type.spaceless',
endspaceless: 'Twig.logic.type.endspaceless'
};


Expand Down Expand Up @@ -2116,6 +2118,37 @@ var Twig = (function (Twig) {
output: template.render(innerContext)
};
}
},
{
type: Twig.logic.type.spaceless,
regex: /^spaceless$/,
next: [
Twig.logic.type.endspaceless
],
open: true,

// Parse the html and return it without any spaces between tags
parse: function (token, context, chain) {
var // Parse the output without any filter
unfiltered = Twig.parse.apply(this, [token.output, context]),
// A regular expression to find closing and opening tags with spaces between them
rBetweenTagSpaces = />\s+</g,
// Replace all space between closing and opening html tags
output = unfiltered.replace(rBetweenTagSpaces,'><').trim();

return {
chain: chain,
output: output
};
}
},

// Add the {% endspaceless %} token
{
type: Twig.logic.type.endspaceless,
regex: /^endspaceless$/,
next: [ ],
open: false
}
];

Expand Down
4 changes: 2 additions & 2 deletions twig.min.js

Large diffs are not rendered by default.

0 comments on commit 3a3a15f

Please sign in to comment.