From 2bbd5eaef1d2550ae72f2ec2da869e8896a0c767 Mon Sep 17 00:00:00 2001 From: Antoine Veldhoven Date: Wed, 28 May 2025 19:35:33 +0200 Subject: [PATCH 1/2] Provide PHP style output of booleans by specifying option. --- src/twig.core.js | 12 ++++++++++++ src/twig.exports.js | 1 + test/test.core.js | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/src/twig.core.js b/src/twig.core.js index d896d5c3..d2229c6c 100644 --- a/src/twig.core.js +++ b/src/twig.core.js @@ -695,6 +695,18 @@ module.exports = function (Twig) { */ Twig.output = function (output) { const {autoescape} = this.options; + const {phpStyleBooleans} = this.options; + + // Conform Javascript boolean to PHP boolean + if (phpStyleBooleans) { + output = output.map(str => { + if (typeof str === 'boolean') { + str = str ? '1' : ''; + } + + return str; + }); + } if (!autoescape) { return output.join(''); diff --git a/src/twig.exports.js b/src/twig.exports.js index e95b33b6..5a0f85b3 100644 --- a/src/twig.exports.js +++ b/src/twig.exports.js @@ -23,6 +23,7 @@ module.exports = function (Twig) { // TODO: turn autoscape on in the next major version autoescape: (params.autoescape !== null && params.autoescape) || false, allowInlineIncludes: params.allowInlineIncludes || false, + phpStyleBooleans: params.phpStyleBooleans || false, rethrow: params.rethrow || false, namespaces: params.namespaces }; 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'); From 5220b0e2371aa65afb35ec0e82ae658da2308178 Mon Sep 17 00:00:00 2001 From: Antoine Veldhoven Date: Mon, 2 Jun 2025 21:34:29 +0200 Subject: [PATCH 2/2] Show deprecation notice when phpStyleBooleans is not defined and a boolean is encountered. Co-Authored-By: Will Rowe --- src/twig.core.js | 14 ++++++++------ src/twig.exports.js | 7 ++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/twig.core.js b/src/twig.core.js index d2229c6c..d75c39c2 100644 --- a/src/twig.core.js +++ b/src/twig.core.js @@ -698,15 +698,17 @@ module.exports = function (Twig) { const {phpStyleBooleans} = this.options; // Conform Javascript boolean to PHP boolean - if (phpStyleBooleans) { - output = output.map(str => { - if (typeof str === '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; - }); - } + return str; + }); if (!autoescape) { return output.join(''); diff --git a/src/twig.exports.js b/src/twig.exports.js index 5a0f85b3..497a29e9 100644 --- a/src/twig.exports.js +++ b/src/twig.exports.js @@ -23,7 +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, - phpStyleBooleans: params.phpStyleBooleans || false, + // TODO: turn phpStyleBooleans on in the next major version + phpStyleBooleans: undefined, rethrow: params.rethrow || false, namespaces: params.namespaces }; @@ -40,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,