Giter Club home page Giter Club logo

Comments (9)

kylekatarnls avatar kylekatarnls commented on May 25, 2024

Hi, it's not about the context, it's about js-to-php transformation. You cannot mix PHP and JS in the same expression. So you can write it all in PHP:

#{$format('money', $item->getPrice())}

Or with 'languageExpression' => 'js' option, I did not test but you can theorically do:

#{format('money', item.price)}

If you get trouble with the first syntax, it's a pug-php bug, with the second, it's probably an enhancement to implement in js-phpize. For mixed JS/PHP syntaxes, we won't fix it because the hybrid mode 'languageExpression' => 'auto' will disappear in the next version.

from pug.

dadajuice avatar dadajuice commented on May 25, 2024

Thanks a lot for your prompt response! I've tested the second option with the expressionLanguage option and I still get some errors, but the generated PHP code seems to be more around what I'm looking for :)

each item in items
  li Example #{item.name} #{format('money', item.price)}

This shoots an error : Notice: Undefined index: __jpv_dot in pug.stream://data; The generated PHP code looks like :

<?php foreach ($items as $item) { ?>
        <li>Example <?php echo \Jade\Compiler::getEscapedValue(call_user_func($GLOBALS['__jpv_dot'], $item, 'name'), '"') ?> <?php echo \Jade\Compiler::getEscapedValue(function_exists('format') ? format('money', call_user_func($GLOBALS['__jpv_dot'], $item, 'price')) : call_user_func($format, 'money', call_user_func($GLOBALS['__jpv_dot'], $item, 'price')), '"') ?></li>
      <?php } ?>

Also, the item.name do not work anymore (triggers same error with the __jpv_dot index). If the $GLOBALS['__jpv_dot'] would be set I think it would do exactly what I want. For information, I've outputted the $GLOBALS variable and I didn't see anything related to Pug.

from pug.

kylekatarnls avatar kylekatarnls commented on May 25, 2024

Indeed $GLOBALS['__jpv_dot'] is the helper from js-phpize to get a property. When you render a template, the helper declarations should be added first at the begenning of the rendered PHP.

Is this the complete render? Because I get an other error: when I test on the demo, format is not converted to $format, that's a tricky part because both could be valid in PHP. But you can bypass it by grouping your helpers in an array:

Try this template:

p Example #{item.name} #{helpers.format('money', item.price)}

With this data:

array(
  'item' => array(
    'name' => 'Foo',
    'price' => 12,
  ),
  'helpers' => array(
    'format' => function ($type, $price) {
      return $type . '-' . $price;
    },
  ),
)

The template compile well in js mode:

 <p>Example Foo money-12</p>

I hope this could fit your needs.

from pug.

dadajuice avatar dadajuice commented on May 25, 2024

Sadly, I'm still getting the same error about $GLOBALS['__jpv_dot']. Here's the generated PHP (obtain in my cache folder).

<?php  ?> 
<p>Example <?php echo \Jade\Compiler::getEscapedValue(call_user_func($GLOBALS['__jpv_dot'], $item, 'name'), '"') ?> <?php echo \Jade\Compiler::getEscapedValue(function_exists('helpers') ? helpers('money', call_user_func($GLOBALS['__jpv_dot'], $item, 'price')) : call_user_func(call_user_func($GLOBALS['__jpv_dot'], $helpers, 'format'), 'money', call_user_func($GLOBALS['__jpv_dot'], $item, 'price')), '"') ?></p>

I see at the top of the generated PHP file an empty tag (<?php ?>). Is the global declarations supposed to go there normally ?

For information, here's how I construct the Pug instance :

$options = [
  'cache' => Configuration::getConfiguration('pug', 'cache'),
  'basedir' => ROOT_DIR . '/public',
  'prettyprint' => true, /* debug purpose */
  'expressionLanguage' => 'js'
];
$pug = new Pug($options);

$args = array(
  'item' => array(
    'name' => 'Foo',
    'price' => 12,
   ),
   'helpers' => array(
     'format' => function ($type, $price) {
       return $type . '-' . $price;
   })
);
$pug->render("p Example #{item.name} #{helpers.format('money', item.price)}", $args)

Also, I'm running on PHP 7.0.10 and I've included Pug to my project using composer. Here's the composer.lock segment if it can be of any help.

{
            "name": "pug-php/pug",
            "version": "2.5.0",
            "source": {
                "type": "git",
                "url": "https://github.com/pug-php/pug.git",
                "reference": "50c47a9f6446086cd8581596a53ab663bc936926"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/pug-php/pug/zipball/50c47a9f6446086cd8581596a53ab663bc936926",
                "reference": "50c47a9f6446086cd8581596a53ab663bc936926",
                "shasum": ""
            },
            "require": {
                "js-phpize/js-phpize": "^1.0",
                "php": ">=5.3.0"
            },
            "replace": {
                "kylekatarnls/jade-php": "self.version"
            },
            "require-dev": {
                "codeclimate/php-test-reporter": "dev-master",
                "phpunit/phpunit": ">=4.0"
            },
            "type": "library",
            "autoload": {
                "psr-0": {
                    "Pug\\": "src/",
                    "Jade\\": "src/"
                }
            },
            ...
}

Thanks again for your prompt response!

from pug.

kylekatarnls avatar kylekatarnls commented on May 25, 2024

I confirm the bug with your code, I don't understand why the demo works.

from pug.

kylekatarnls avatar kylekatarnls commented on May 25, 2024

I identified the bug, if js-phpize is called many times, the dependencies are replaced and not merge. Just release a js-phpize version, add a unit test for this issue, and you will be able to update and fix it.

from pug.

kylekatarnls avatar kylekatarnls commented on May 25, 2024

Please upgrade to 2.5.1 and retry.

from pug.

dadajuice avatar dadajuice commented on May 25, 2024

Just tested with 2.5.1 and I confirm that it works perfectly! Thank you very very much for your time. I'm taking the liberty to close this issue :)

from pug.

kylekatarnls avatar kylekatarnls commented on May 25, 2024

Thanks to you for the report!

from pug.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.