Skip to content

Instantly share code, notes, and snippets.

@mattkahl
Last active December 13, 2015 19:29
Show Gist options
  • Save mattkahl/4963386 to your computer and use it in GitHub Desktop.
Save mattkahl/4963386 to your computer and use it in GitHub Desktop.
Add Twig's spaceless token to twig.js (e.g. `{% spaceless %} ... {% endspaceless %}`)
Twig.extend(function(Twig) {
// Add the {% spaceless %} token
Twig.logic.extend({
type: 'spaceless',
regex: /^spaceless$/,
next: [
'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
Twig.logic.extend({
type: 'endspaceless',
regex: /^endspaceless$/,
next: [ ],
open: false
});
});
@mattkahl
Copy link
Author

mattkahl commented Mar 3, 2013

This was integrated into twig.js 0.5.6.

twigjs/twig.js#63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment