Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/twig.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down
6 changes: 6 additions & 0 deletions src/twig.exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions test/test.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down