pug-php / pug

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

For render as PHP structure #22

Closed yozman closed 8 years ago

yozman commented 8 years ago

hey first of all, thanks for make pug for php :+1:

I have some ideas for pug.

I think pug is for html, just for html

so what does it mean? in pug-php, - for ($i = 0; $i < $j; $i++) render as PHP struct it not right at all

the right way use pug in php

it should base on orther php template engine like blade, twig

@for ($i = 0; $i < $j; $i++)
  p hello {{ $i }}
@endfor

how do u think about it?

kylekatarnls commented 8 years ago

Hi, as a pug port we provide pug js equivalent, the dash allow raw code, it's the pug native feature, in pug js raw code is js code, in this port, it's PHP we just translated it. Ranges are also available in this way:

each i in range(0, $j - 1)

You can also use while, this is the pug native way to do: http://jade-lang.com/reference/iteration/

The dash in pug is nearly the same than the arobase in blade it's nothing more than a PHP injection. It just use for: and endfor; And in Pug we do not want some things like end keyword because pug is structured by indentation. (You will meet the same problem if you try to use ´´´for´´´ this way in the native js pug.)

It you get a too complex loop to do in Pug, you probably should do it in your controller, in a filter, or elsewhere but as you say it's for html and the view and the application logic should not be mixed, so in my humble opinion, the features provided by Pug are just enough to fit the need. If you're stuck with some tricky template you can ask I will try to give you the "clean way".

PS: you're not the very first there are closed issue here and on jade the previous name. Thanks for support.

yozman commented 8 years ago

thanks for your answer ;)

as you say, pug has raw mode, I agree with you, but it's for javascript. not php. when I copy some template from my nodejs project, it doesn't work, this is the point. php is php, javascript is javascript, pug-php should not break pug's js support

then pug-php should be the plus feature, like some keyword @foreach, @if, @extends, @layout,

how do you think about it?

kylekatarnls commented 8 years ago

First, if you want strictly compatible template from pugjs, use a wrapper (php execution of pug-cli). The problem with this method is that you have to pass flat data, you will not be able to pass an object of a custom User class for example. Pug-php give a way to use dynamic object and native php functions in pug templates. For the rest, we try to keep Pug-php as close as possible to the js version.

if, layout (see block) and extends exist, so no need of @ new keywords, and foreach exist as each and allow you to do some repetitive structures.

However, I think about a way to customize the engine to add your own rules as it is possible in blade and twig.

kylekatarnls commented 8 years ago

Hi, I see the point (at last). I never notice this works with pug-php:

- j = 4
- for ($i = 0; $i < $j; $i++)
  p hello #{i}

I permit myself to rename your issue for classification. The dash will be keeped for retrocompatibility. And the trick can be very usefull and I think it's used, so I would not break templates that use it.

My recommandation for those kind of cases is to use the dash only for variables initializations and you should initialize only with static values. Use as few raw code and dynamic expression as possible in your pug templates.

But what you want (a real for keyword) will be possible in 2.0 with customization.

You will have to do something like that:

$pug = new Pug();
$pug->addKeyword('for', function ($args) {
    return array(
        'beginPhp' => 'for (' . $args . ') {',
        'endPhp' => '}',
    );
});

And you will be able to use it in your templates in a nice pug style:

for $i = 0; $i < $j; $i++
  p hello #{i}
yozman commented 8 years ago

@kylekatarnls thanks for all

kylekatarnls commented 8 years ago

Pug keywords are out: https://github.com/pug-php/pug#supports-for-custom-keywords

Soon I will publish a 2.0.0 pre-release with this new feature.

kylekatarnls commented 8 years ago

2.0.0 is out. Do not hesitate, post new issue if you have any suggestion for keywords or other.

partounian commented 7 years ago

@kylekatarnls Hey Kyle, I know this is a bit old. I am coming from https://github.com/BKWLD/laravel-pug/issues/2 , I am having issues passing through values for customKeyword, I have essentially tried the arguments used for the addKeyword function, but can't get it to work. Can you please add some documentation for passing custom keywords?

kylekatarnls commented 7 years ago

There is documentation: https://github.com/pug-php/pug#supports-for-custom-keywords

What's your code, the behaviour you get and the behaviour you expect?