Comments (9)
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.
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.
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.
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.
I confirm the bug with your code, I don't understand why the demo works.
from pug.
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.
Please upgrade to 2.5.1 and retry.
from pug.
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.
Thanks to you for the report!
from pug.
Related Issues (20)
- syntax error, unexpected 'idx' (T_STRING), expecting ';' HOT 1
- Support template literals HOT 1
- Undefined variable fatal error appears on quoted Vue.js variable HOT 13
- Ternary conditional operator fails with linebreak HOT 6
- read-only cache directory HOT 20
- Undefined variable when using mixin with concatenation HOT 3
- Accessing helper functions in a namespace HOT 4
- PHP 7.4 with Pug 2.7.5 minor correction HOT 3
- Compatibility problem with laracast HOT 4
- Interpolation does not support null coalescing operator HOT 4
- enable_profiler => true shows php notices (undefined index) HOT 5
- Apostrophe is escaped HOT 2
- Option to disable html minficiation HOT 1
- Bug when using combinaison of mixins with block in iteration
- Incompatibility with composer 2.0 HOT 1
- Tag Name error HOT 5
- PHP 8 support? HOT 3
- How do i convert back to HTML and keep the variable intact? HOT 1
- Deprecation warning in PHP 8.1 for null values HOT 1
- Pug 2.7.6 minor correction to avoid warning with block append usage HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pug.