Open Krinkle opened 5 years ago
Build failure is unrelated, see https://github.com/zordius/lightncandy/issues/307.
Hello, This pull request breaks a handlebars.js spec test, which was definded in https://github.com/wycats/handlebars.js/pull/731 . Refer to these jsfiddle:
handlebars.js 4.1.1: https://jsfiddle.net/0rec6d2f/ mustache.js 3.0.1: http://jsfiddle.net/4nLockuy/
Here are some mustache <=> handlebars difference, you can refer to these document:
About block: https://zordius.github.io/HandlebarsCookbook/0008-block.html#BlockforFalse (search for 'Only mustache.js will think 0 as false')
About #if: https://zordius.github.io/HandlebarsCookbook/0016-if.html (search for '0 means false')
If you like to follow mustache, the FLAG_MUSTACHE
is good, you may also check the document https://github.com/zordius/lightncandy , search 'Mustache Compatibility' for more detail.
If you like to follow handlebars.js, you should use FLAG_HANDLEBARS
or check the document https://github.com/zordius/lightncandy , search 'Handlebars Compatibility' for more detail.
@zordius This is about the string '0'
, not the number 0
.
The if
logic is already working correctly in lightncandy. When passing data [ 'foo' => '0' ]
to {{#foo}}Yes{{/foo}}
the word Yes
renders correctly in lightncandy.
The problem is in the internal lightncandy logic for getting a value from a path, in ::v()
.
The example template:
{{#foo}}<b>{{foo}}</b>{{/foo}}
The example data:
'data' => [
'foo' => '0',
],
When ::v()
is called by the processing of {{#foo}}
, the following happens:
$data
is [ 'foo' => '0' ]
$path
is [ 'foo' ]
(this could also be ['foo','bar',baz']
for {{#foo.bar.baz}}
).::v()
there is a loop over $path
, it is protected by a PHP if
statement for $base
. It sees the array as "true".'0'
.'0'
is true for HTML template (not PHP). That logic, decides '0'
is true.Then, for <b>{{foo}}</b>
we also use ::v()
. There we have the bug.
$data
is [ '0' ]
if
statement for $base
stops immediately, we never look. This means the Mustache and Handlebar logic does not have an opportunity to decide.The value is never found, and never printed. We get <b></b>
.
I might need help for a better fix. Do you have a recommendation?
Well, please refer to https://github.com/zordius/lightncandy/issues/305 , we compared handlebars.js and mustache.js there, again.
Now the handlebars.js behavior is strange but lightncandy is aligned with it. Here are some points:
Fixes issue #305.