pug-php / pug

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

Pug/Jade old interpolation syntax #{url} deprecated #135

Closed sandrodz closed 7 years ago

sandrodz commented 7 years ago

We are using pug as a bridge between front-end and back-end repos. Our aim is to have same pug files both while building front-end with nodejs or when I compile it with php.

Obviously we need to pass some variables to pug files. From php I make a big array and pass it as $data. In nodejs we have same $data exported as json and we feed pug that.

Problem is that in php-pug only #{this_is_var} works and in nodejs version its deprecated.

https://pugjs.org/language/attributes.html#attribute-interpolation

What do you think, how can we deal with this?

kylekatarnls commented 7 years ago

If you need exact same behavior than in nodejs, use the new option pugjs, it will exec nodejs and pug-cli through PHP:

$pug = new Pug(array(
  'pugjs' => true
));
echo $pug->render('yourfile.pug');

Else you should not use #{var} as it will also be dropped in the next version of pug-php.

sandrodz commented 7 years ago

hm, I don't think it is working.

screen shot 2017-05-11 at 2 15 26 pm
sandrodz commented 7 years ago

I still get php errors

screen shot 2017-05-11 at 2 18 31 pm

sandrodz commented 7 years ago

I'm using nvm, and I've source ~/.nvm/nvm.sh in my .zshrc

How does 'pugjs' => 'js', run npm ? will it work if I source nvm in .bashrc ?

kylekatarnls commented 7 years ago

First, I never say to use 'pugjs' => 'js', it's a boolean option: 'pugjs' => true

Then clear your cache.

On the pug-php package install, a post-install script will try to install pug-cli using npm install, if pugjs option is true, ->render() will try to run pug-cli, if it fails, it will fallback to the PHP engine.

sandrodz commented 7 years ago

@kylekatarnls its in README.md probably a typo.

I just realized I've containerized php (docker) - and node is installed on the system. They don't see each other. Any ideas?

Also is there a timeline for v3 of pug-php which will be a match for pug-js ?

kylekatarnls commented 7 years ago

Thanks for the README mistake.

I hope to release phug-php (the new engine) this summer. But the very closest solution will still be to use the same engine on both sides (pugjs via nodejs). Pug-php has no yet customization for node environment settings, it means the node program should be executable by the PHP user. Allow this on your hosting would be the best option.

Else you can use https://github.com/kylekatarnls/nodejs-php-fallback on your own. Example:

$nodePath = '/path/to/your/node/program';
$pugPath = '/path/to/pug-cli';
$template = '/path/to/the/template-file.pug';
$vars = array(
  'title' => 'My blog'
);
$options =
  ' --obj ' . json_encode(json_encode($vars)) .
  ' --basedir ' . escapeshellarg('/path/to/templates-directory') .
  ' --out' . escapeshellarg('/output/directory');
$node = new NodejsPhpFallback($nodePath);

echo $node->nodeExec($pugPath . $options . ' ' . escapeshellarg($template) . ' 2>&1');

In this example, you don't need to install pug-php, just nodejs-php-fallback/nodejs-php-fallback with no extra configuration.

sandrodz commented 7 years ago

okay few more q.

  1. Are you planning to merge interpolation-fix branch anytime soon?
  2. nodejs version, in docs it says no caching is available, so node interprets pug file on every file load is this right? Seems it will be slow.
kylekatarnls commented 7 years ago
  1. fix-interpolation is yet merged.
  2. It's no longer true, now cache is compatible with pugjs option, I will update the README.

Then, I meet the same problem than you with the Heroku hosting for the pug demo, I will make a fix to be able to set a custom path for the node program, maybe it will help you.

sandrodz commented 7 years ago
  1. yes but this doesn't work still
    `${blah}-someothertextstring`
  2. great!

custom path for node would be great.

kylekatarnls commented 7 years ago
`${blah}-someothertextstring`

Will not be planned. Be ware, this is not a pug feature, this is a pure javascript feature. Try the following in a browser console:

foo = 42;
alert(`${foo}-bar`);

We will not rewrite the whole js engine in our PHP port, this would not be relevant.

You will not benefit of both PHP features and JS features into the same engine. So you have to choose between PHP (use the port and write expressions in PHP) and JS (flat your data into a JSON, then use the native pug engine, that's what pugjs option do).

sandrodz commented 7 years ago

I see, so my only option is custom path for node.

kylekatarnls commented 7 years ago

You can now update and specify a custom node path, example:

$pug = new Pug((array(
  'pugjs' => true,
  'nodePath' => __DIR__ . '/../bin/node'
));
kylekatarnls commented 7 years ago

You also may want to use an other pug-cli path (to use a global node_modules path or use a custom pug-cli version):

NodejsPhpFallback::setModulePath('pug-cli', __DIR__ . '/../node_modules/pug-cli');

Example: https://github.com/pug-php/pug-demo/blob/master/api/index.php

Can I close this issue?

sandrodz commented 7 years ago

Yes! thank you :)

can't wait to see v3 though.

kylekatarnls commented 6 years ago

Pug-php 3 released, I hope you enjoy it.

sandrodz commented 6 years ago

Super! thank you for the hard work.