Last active
December 13, 2015 19:29
-
-
Save mattkahl/4963386 to your computer and use it in GitHub Desktop.
Add Twig's spaceless token to twig.js (e.g. `{% spaceless %} ... {% endspaceless %}`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was integrated into twig.js 0.5.6.
twigjs/twig.js#63