diff --git a/src/twig.core.js b/src/twig.core.js index d896d5c3..d75c39c2 100644 --- a/src/twig.core.js +++ b/src/twig.core.js @@ -695,6 +695,20 @@ module.exports = function (Twig) { */ Twig.output = function (output) { const {autoescape} = this.options; + const {phpStyleBooleans} = this.options; + + // Conform Javascript boolean to PHP boolean + output = output.map(str => { + if (typeof str === 'boolean') { + if (phpStyleBooleans === undefined) { + console.warn('Deprecation notice: `phpStyleBooleans` is not defined, output differs from PHP behavior, in future versions this behavior will be changed to PHP style booleans.'); + } else if (phpStyleBooleans) { + str = str ? '1' : ''; + } + } + + return str; + }); if (!autoescape) { return output.join(''); diff --git a/src/twig.exports.js b/src/twig.exports.js index e95b33b6..497a29e9 100644 --- a/src/twig.exports.js +++ b/src/twig.exports.js @@ -23,6 +23,8 @@ module.exports = function (Twig) { // TODO: turn autoscape on in the next major version autoescape: (params.autoescape !== null && params.autoescape) || false, allowInlineIncludes: params.allowInlineIncludes || false, + // TODO: turn phpStyleBooleans on in the next major version + phpStyleBooleans: undefined, rethrow: params.rethrow || false, namespaces: params.namespaces }; @@ -39,6 +41,10 @@ module.exports = function (Twig) { Twig.trace = params.trace; } + if (params.phpStyleBooleans !== undefined) { + options.phpStyleBooleans = params.phpStyleBooleans; + } + if (params.data !== undefined) { return Twig.Templates.parsers.twig({ data: params.data, diff --git a/test/test.core.js b/test/test.core.js index ec2b06f5..3119847f 100644 --- a/test/test.core.js +++ b/test/test.core.js @@ -147,6 +147,11 @@ describe('Twig.js Core ->', function () { twig({data: '{{ false }}'}).render().should.equal('false'); }); + it('should be able to output booleans (PHP style)', function () { + twig({phpStyleBooleans: true, data: '{{ false }}'}).render().should.equal(''); + twig({phpStyleBooleans: true, data: '{{ true }}'}).render().should.equal('1'); + }); + it('should be able to output strings', function () { twig({data: '{{ "double" }}'}).render().should.equal('double'); twig({data: '{{ \'single\' }}'}).render().should.equal('single');