Closed Freest10 closed 6 years ago
We provide tools to make some JS stuffs converted into PHP code, but i we wanted to make all JS stuff compatible, first it would make some PHP stuff not available in templates (which is the first purpose of this port), then it would need nodejs or V8Js to be installed on every machine you want to render templates (right now it's not mandatory and that allows do render templates with pure PHP).
So I recommend you to replace such functions (parseInt
) with syntaxes available in both PHP and JS such as totalInt = ~~total
Ok, but why it doesnot work totalInt = +total?
This convert string to number equally in js and php.
I never say it does not. ~~
is just a way to do it among other. It also works perfectly in pug-php with +
:
- total = "45"
- totalInt = +total
p=gettype(totalInt)
p=totalInt
I just would not recommend +
because it output different results depending on what you have before and after. ~
is a bitwise operator, so it has a constat behavior with any data type and it only take into account what you have at the right hand. So it's my favorite way if needed.
But the very better solution will always be to calculate all your data properly in the controller. So you can have a clean cast (int) $myVar
on the PHP hand.
In pug +
does not convert a string to a number.
Try
- total = "45"
- totalInt = +total + 7
p=gettype(totalInt) p=totalInt
It was 457. Pug concat it as string, because +45
was not converted string to number.
Yes it does if the precedence is respected: - totalInt = (+total) + 7
Wanting the exact same behavior using JS code when porting in a PHP environment would be in many cases a bad idea. But if you want the exact same output, you can use the option 'pugjs' => true,
it will disable completely the PHP renderer (and so make unavailable options using it and flatten the data: class instances become simple objects), but it will use the pug nodejs package to render the template, so with the same data (passed internally as JSON) and the same template, you will get the same HTML as you would get in nodejs.
Hello, could not convert a string to a number.
Thanks!