Closed Riant closed 7 years ago
Hi,
You should not mix JS and PHP in expressions. The following will work with default settings:
mixin avatar_img(default_src)
- $default_src || ($default_src = 'default-xxx.png')
img(src=$userinfo['user_thumb'] ? $userinfo['user_thumb'] : $default_src)
To get full JS syntax every where, you need the expressionLanguage option set to 'js':
use Pug\Pug;
$pug = new Pug(array(
'expressionLanguage' => 'js',
));
But this will not properly handle the JS ||
because in JS ||
returns any kind of value, in PHP it returns only booleans.
So be aware var _default_src = default_src || 'default-xxx.png'
is exactly the same in JS as a ternary:
var _default_src = default_src ? default_src : 'default-xxx.png'
In PHP not, you have to use ternary:
$_default_src = $default_src ? $default_src : 'default-xxx.png'
O, Got it, Thanks.
With pug-php 3, ternary expressions can now be used inline. Hope you will enjoy the new version.
Mixin:
When I call the mixin with
+avatar_img('abc')
or+avatar_img()
, I got the error like below:It looks like did not support the ternary operator like
a ? b : (c ? d : 'xxx')
ora ? b : ( c || 'xxx')
.Thanks.