pug-php / pug

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

How to pass an array as mixin param? #84

Closed fdorantesm closed 7 years ago

fdorantesm commented 7 years ago

How we can pass array as param with pug 2.4.5?

mixin asd(params)
    div= params.name
+asd(["name":"Pug","lang":"PHP"])

Result Fatal error: Uncaught exception 'ErrorException' with message '['name':"Pug" Missing closing: ]'

If just pass one value, works fine:

+asd(["name":"Pug"])

Return json:

{"name":"Pug"}

Regards!

kylekatarnls commented 7 years ago

Indeed, the , is interpreted as mixin attribute separator. That should not be.

To work around this bug, you can use a variable:

mixin asd(params)
  div= params.name

- a = array("name" => "Pug", "lang" => "PHP")
+asd(a)

Thanks for your report.

fdorantesm commented 7 years ago

Thank you!

That's works fine. How i didn't it before?

Regards!

fdorantesm commented 7 years ago

Hi,

When add &attributes pug appends " " to class attribute.

mixin asd(params)
  div&attributes(attributes)= params.name

- a = array("name" => "Pug", "lang" => "PHP")
+asd(a)
<div class=" "></div>

And, when you add multidimensional arrays doesn't work.

- a = array("icon"=>["background"=>"red","color"=>"white"],"text"=>["color"=>"blue","background"=>"white"]
asd(a)

Regards!

kylekatarnls commented 7 years ago

You have some syntax errors: the ) at the end is missing and the mixin call should start with +, and it seems I tried this:

mixin asd(params)
  div&attributes(attributes)= params.name

- $a = ["name"=>"abc","icon"=>["background"=>"red","color"=>"white"],"text"=>["color"=>"blue","background"=>"white"]]
+asd(a)(a="b")

I had to add the $ but I get params.name and the &attributes:

<div a="b">abc</div>

However, I can not reproduce the bug with class=" ".

kylekatarnls commented 7 years ago

Hi, I consider this last syntax solved you problem, if not, feel free to re-open.

mriabchenko commented 7 years ago

Hello. I already have a huge pug template with a lot of mixins. This template will probably updated by other developers. In this template i have a lot of mixins which accepting params in this way: image It will be very time consuming to refactor all those arrays into php-style arrays and pass them into mixins (how it was discribed in the begging of this topic) How to make this extantion work with native pug way of passing params arrays into mixins?

kylekatarnls commented 7 years ago

With pug-php 3.0, you can choose between PHP-style and JS-style using expressionLanguage option:

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

If you want JS-style, it's the default value, so you don't even need to specify it. You just have to be consistent, both styles can not work together in the same file.

Did you try it, do you get an error?

mriabchenko commented 7 years ago

@kylekatarnls i am using https://github.com/rmrevin/yii2-pug . Do you know how to solve my problem in yii2-pug ?

kylekatarnls commented 7 years ago

The main problem with yii2-pug is that it load pug-php/pug 2.5 that is now obsolete. I opened an issue to ask them to upgrade: https://github.com/rmrevin/yii2-pug/issues/1. If they don't, some day when I will have enough time, I will fork it and propose an yii2 adapter for pug-php 3.

Right now, I can propose you to remove rmrevin/yii2-pug from your packages, install pug-php/pug 3 and try this: https://gist.github.com/kylekatarnls/5b748c8fd351906447305703339bf5cc

And use this ViewRenderer instead of the rmrevin one. And so you will be able to change the $options variable as you want in it.

kylekatarnls commented 6 years ago

Hi, you can now replace rmrevin/yii2-pug with pug/yii2 in your dependencies to enable pug-php 3.