twigjs / twig.js

JS implementation of the Twig Templating Language
BSD 2-Clause "Simplified" License
1.89k stars 276 forks source link

"Twig supports binary operations (+, -, *, /, ~, %, and, or)" incorrect print of boolean #88

Open m-abs opened 11 years ago

m-abs commented 11 years ago

Twig.php follows the same rules for printing booleans as PHP does.

In PHP "false" is printed as empty string. "true" is printed as 1

{{ 1 and 0 }}

Print:

0

should print:

I'm not sure how to fix this. I should change Twig.expression.operator.parse to add the PHP-like strings, but isn't that function both used for outputting text and the evaluating operator expressions?

I could also, make Twig.expression.operator.parse add the proper types to the stack and then parse the output at the end (the expression "a && b" add a 1 if true and 0 if false), but I don't understand the code well enough to see if this is a good idea or not.

evgenius commented 10 years ago

@justjohn , what was your main goal when you decided to create a port of PHP Twig?

Do you want Twig.js to render templates exactly how Twig.php does it? As @m-abs reported, sometimes there is significant difference between Twig.php and Twig.js results. If you think Twig.js should be an exact port of Twig.php, I can start working on it.

justjohn commented 10 years ago

My goal is to mimic twig PHP as closely as possible. In some cases that's not really possible, since JavaScript and PHP are different languages, but for cases like this, I see no reason not to make the change.

If you're interested in creating a fix, go for it, I'd be happy to merge it in.

I think the suggestion about annotating the output from @m-abs is the right way to go, since that provides an easy path to adding automatic escaping of output, which is currently the major difference between twig php and twig js.