Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

For render as PHP structure #22

Closed
yozman opened this issue Jul 10, 2016 · 9 comments
Closed

For render as PHP structure #22

yozman opened this issue Jul 10, 2016 · 9 comments
Milestone

Comments

@yozman
Copy link

yozman commented Jul 10, 2016

hey first of all,
thanks for make pug for php 馃憤

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
Copy link
Member

kylekatarnls commented Jul 11, 2016

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
Copy link
Author

yozman commented Jul 12, 2016

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
Copy link
Member

kylekatarnls commented Jul 12, 2016

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 kylekatarnls changed the title some ideas Custom keywords Jul 13, 2016
@kylekatarnls kylekatarnls added this to the 2.0 milestone Jul 13, 2016
@kylekatarnls kylekatarnls changed the title Custom keywords For render as PHP structure Jul 13, 2016
@kylekatarnls
Copy link
Member

kylekatarnls commented Jul 13, 2016

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
Copy link
Author

yozman commented Jul 13, 2016

@kylekatarnls thanks for all

kylekatarnls added a commit that referenced this issue Jul 19, 2016
* Implement custom keywords
* Allow to customize the class attribute
* Document custom keywords
@kylekatarnls
Copy link
Member

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
Copy link
Member

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

@partounian
Copy link

@kylekatarnls Hey Kyle, I know this is a bit old. I am coming from BKWLD/laravel-pug#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
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants