Skip to content

Commit 2bbd5ea

Browse files
Provide PHP style output of booleans by specifying option.
1 parent 8f5d4e5 commit 2bbd5ea

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/twig.core.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,18 @@ module.exports = function (Twig) {
695695
*/
696696
Twig.output = function (output) {
697697
const {autoescape} = this.options;
698+
const {phpStyleBooleans} = this.options;
699+
700+
// Conform Javascript boolean to PHP boolean
701+
if (phpStyleBooleans) {
702+
output = output.map(str => {
703+
if (typeof str === 'boolean') {
704+
str = str ? '1' : '';
705+
}
706+
707+
return str;
708+
});
709+
}
698710

699711
if (!autoescape) {
700712
return output.join('');

src/twig.exports.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = function (Twig) {
2323
// TODO: turn autoscape on in the next major version
2424
autoescape: (params.autoescape !== null && params.autoescape) || false,
2525
allowInlineIncludes: params.allowInlineIncludes || false,
26+
phpStyleBooleans: params.phpStyleBooleans || false,
2627
rethrow: params.rethrow || false,
2728
namespaces: params.namespaces
2829
};

test/test.core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ describe('Twig.js Core ->', function () {
147147
twig({data: '{{ false }}'}).render().should.equal('false');
148148
});
149149

150+
it('should be able to output booleans (PHP style)', function () {
151+
twig({phpStyleBooleans: true, data: '{{ false }}'}).render().should.equal('');
152+
twig({phpStyleBooleans: true, data: '{{ true }}'}).render().should.equal('1');
153+
});
154+
150155
it('should be able to output strings', function () {
151156
twig({data: '{{ "double" }}'}).render().should.equal('double');
152157
twig({data: '{{ \'single\' }}'}).render().should.equal('single');

0 commit comments

Comments
 (0)