pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
387 stars 42 forks source link

Could not convert a string to a number #202

Closed Freest10 closed 6 years ago

Freest10 commented 6 years ago

Hello, could not convert a string to a number. error-parse

Thanks!

kylekatarnls commented 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

Freest10 commented 6 years ago

Ok, but why it doesnot work totalInt = +total? This convert string to number equally in js and php.

kylekatarnls commented 6 years ago

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

http://pug-demo.herokuapp.com/?embed&theme=xcode&border=silver&options-color=rgba(120,120,120,0.5)&engine=pug-php&input=-%20total%20%3D%20%2245%22%0D%0A-%20totalInt%20%3D%20%2Btotal%0D%0Ap%3Dgettype(totalInt)%0D%0Ap%3DtotalInt&vars=array(%0A%20%20%27pageTitle%27%20%3D%3E%20%27Try%20Pug.php%20and%20never%20recode%20HTML%20again%27%2C%0A%20%20%27youAreUsingJade%27%20%3D%3E%20true%2C%0A)

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.

Freest10 commented 6 years ago

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.

kylekatarnls commented 6 years ago

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.