pug-php / pug

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

Errors related to $ #195

Closed roxblnfk closed 6 years ago

roxblnfk commented 6 years ago

Hello,

I encountered an issue with the following code (from official Phug documentation):

- $values = [1, 2, 3]
ul
  each $val in count($values) ? $values : ['There are no values']
    li= $val

I expected to get:

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>

But I actually get:

Invalid argument supplied for foreach()

I know that it will work with code:

each $val in count($values) ? values : ['There are no values']

(without $)


Following code:

- $t = 'hello'
div= true ? $t : 'nope'

presumably causes a PHP error:

syntax error, unexpected '(', expecting variable (T_VARIABLE) or '{' or '$'

Thanks!

kylekatarnls commented 6 years ago

Maybe you miss this part about PHP-style vs. JS-style: https://phug-lang.com/#use-javascript-expressions

Example to use PHP-style with Pug-php:

<?php

use Pug\Pug;

include_once __DIR__ . '/vendor/autoload.php';

$pug = new Pug([
    'expressionLanguage' => 'php',
]);

$pug->display('p=$user->name', [
  'user' => (object) [
    'name' => 'Bob',        
  ],
]);
roxblnfk commented 6 years ago

I run this code in the demo page and in the "PHP style" boxes of documentation pages. Default is there expressionLanguage='auto'. It is strange for the "PHP style" blocks

If i check expressionLanguage='php' then it works. Sorry

kylekatarnls commented 6 years ago

auto mode is JS-style, it's there for backward-compatibility with Pug-php 2.x. I recommend to always set explicitly the mode to js or php. Glad it works now. :)