pug-php / pug-symfony

Pug (Jade) template engine for Symfony

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pug-php ternary parse error

kodal opened this issue · comments

title= title ? title : 'Title'
Works on gulp-pug, but not on pug-php

commented

Yes, it's expected because pug-php expect PHP expressions to allow all native PHP stuff. When you have expressions, you have 2 options in pug-php:
White them in PHP title=$title ? $title : 'Title'
Or just title=$title ?: 'Title'

Or you can set the expressionLanguage to 'js' to use the translator js-phpize that will convert every expression from JS to PHP.

Thank you!

Can I use js-phpize in Symfony Framework?

commented

Yes with: $this->container->get('templating')->setOption('expressionLanguage', 'js');

Sorry, but it doesn't work for me. Can I set this in settings? (services.yml in Symfony). I want that expressionLanguage was 'js' by default.

How the expressionLanguage:auto works?

commented

Auto: just add $ for single variables and dot for arrays. With simple expressions, this mode allow both JS and PHP to work with less conflicts but is limited.
JS: all translated, PHP won't work.
PHP: no translation, expressions given as it to PHP.

It should be a way with services.yml but I'm in travel, I cannot test right now. It also depend on your Symfony version. This is an example that get the pug service: https://github.com/Evpok/LEjeu/blob/master/src/AppBundle/ViewSettings/ViewSettings.php

Once you succeed to get the container, you can get pug from it with ->get('templating.engine.pug') and set option on it to pass to the engine. If it does not work, please give me the error.

Great! Thx, I successfully set expressionLanguage to js.
But, I can't solve my problem:
I want that:

if title
  // if var setted
else
  // if var not setted

But it doesn't works to php.
I want the code that works on gulp-pug and pug-symfony identically.
Is it possible?
I've tried:

if $title
if isset($title)
if typeof(title) != undefined
if !empty($title)

Generally is it possible identically code on pug-js and pug-php? For front-end + back-end. Or is there another solution?

commented

Please precise the error you get because your code perfectly works on the live tester with both JS and auto modes. The aim of pug-php is to allow you to call PHP functions, complex PHP objects in your pug template. If you want exactly pug-js with no PHP support at all, then use directly pug-js (wrap pug-cli in a PHP call such as exec()). But I don't understand your point about front-end, why would you use pug on front side?

if title
  = title
else
  | Title

That works on gulp-pug, but not on pug-symfony.
Error:

Notice: Undefined variable: title

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: Undefined variable: title" at jade.stream://data;<!DOCTYPE html> <html> <head> <title> <?php if($title) { ?> <?php echo \Jade\Compiler::getEscapedValue($title, '"') ?> <?php } else { ?> Title <?php } ?> </title> <meta charset="utf-8"<?php $__classes = implode(" ", array_unique(array_merge(empty($__classes) ? array() : explode(" ", $__classes), array ( ), array())) ); ?><?php if (!empty($__classes)) { echo ' class="' . $__classes . '"'; } unset($__classes); ?>> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1"<?php $__classes = implode(" ", array_unique(array_merge(empty($__classes) ? array() : explode(" ", $__classes), array ( ), array())) ); ?><?php if (!empty($__classes)) { echo ' class="' . $__classes . '"'; } unset($__classes); ?>> </head> <body> <header> <h1>header</h1> </header> <main> <h1>Content</h1> </main> <footer> <h1>footer</h1> </footer> </body> <foot> <link rel="stylesheet" href="/css/style.css"<?php $__classes = implode(" ", array_unique(array_merge(empty($__classes) ? array() : explode(" ", $__classes), array ( ), array())) ); ?><?php if (!empty($__classes)) { echo ' class="' . $__classes . '"'; } unset($__classes); ?>> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"<?php $__classes = implode(" ", array_unique(array_merge(empty($__classes) ? array() : explode(" ", $__classes), array ( ), array())) ); ?><?php if (!empty($__classes)) { echo ' class="' . $__classes . '"'; } unset($__classes); ?>></script> <script type="text/javascript" src="/js/script.js"<?php $__classes = implode(" ", array_unique(array_merge(empty($__classes) ? array() : explode(" ", $__classes), array ( ), array())) ); ?><?php if (!empty($__classes)) { echo ' class="' . $__classes . '"'; } unset($__classes); ?>></script> </foot> </html> line 6

Simply I want pug code, that should for gulp-pug and pug-symfony.

commented

Hi, gulp-pug is a wrapper of pug-js and has nothing to do with PHP, so it is normal it use JS to run it. Pug-php can run pug-js via nodejs with the following option (you will need to composer update and must have node and npm installed):

$services = $kernel->getContainer();
$services->get('templating.engine.pug')->setOption('pugjs', true);

But you will lose the benefits you have as a PHP template engine: cache, access to magic methods from objects like Doctrine, DateTime and other custom objects.

As this is more important for many usages than having JS syntaxes in expressions, we choose to use PHP engine in pug-symfony and we recommend you to do the same.

For your notice error, the best practice is to always declare variables your templates need even if empty (here it means explicitly pass "title" => false when you want to hit the else statement. An other solution is use explicitly isset in your template:

if isset($title) && $title
  = title
else
  | Title

Last solution is to disable notice errors using the error level: http://php.net/manual/en/function.error-reporting.php

Thank you very much