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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass a rendering helper to section lambdas #101
Conversation
Great, I'll try how your solution works. |
Your <?php
$template = '{{#dynamicpartial}}{{partialname}}{{/dynamicpartial}}';
$partials = array( 'fullname' => '{{lastname}}, {{name}}' );
$partialname = 'fullname';
$mustache = new Mustache_Engine( array(
'partials' => $partials
));
$data = array(
'name' => "Willy",
'lastname' => 'Moe',
'partialname' => $partialname,
'dynamicpartial' => function( $text, $mustache )
{
return '{{>' . $mustache->render( $text ) . '}}';
}
);
echo $mustache->render( $template, $data ); The template calls the lambda 'dynamicpartial'. The function renders the {{partialname}} to build the string that calls the partial and renders the text. Please add this functionality to Mustache.php, it's a big help. |
You could even register 'dynamicpartial' as a helper so you don't have to add it to the rendering context... Just drop this in after instantiating <?php
$mustache->addHelper('dynamicpartial', function($text, $mustache) {
return '{{>' . $mustache->render($text) . '}}';
}); |
Ah - even better. And from the merge statement I see that you decided to implement the feature in your Mustache.php dev branch - marvelous. |
@uhunkler I haven't quite decided yet. It's open as a pull request so we can discuss before I merge it :) |
Awesome, just the feature I was missing right now. |
Justin, what do you think needs discussion about this feature? Do you need more people confirming the usefulness? Or more arguments? One big argument I see is that in other Mustache implementations like JavaScript and Ruby this functionality is inherent. To be able to implement Mustache as consistent as possible I think this functionality should be possible in your PHP version too. |
@uhunkler Merging :) |
Pass a rendering helper to section lambdas
This has been merged into the |
Marvelous :-) |
Just something ... What'll happen if it's an user string and it contains, like "{{ admin }}" ? secury issue, isn't it ? |
Yup. It's called mustache injection, and it's definitely possible if you return user values from a lambda. I don't see a good way of preventing it at the engine level though. |
Sorry - haven't realized master is still < 2.1
|
@satyago Sorry about that. v2.1 is waiting on logging (see #116), but logging is waiting on an in-progress PHP-FIG discussion trying to standardize logging. Unless that discussion stalls or doesn't come to a resolution, v2.1 will happen after a logging standard is adopted. In the interim, you should feel free to use the |
This means you can call `$this->render()` from inside a section lambda closure. See #101
Absolutely no problem ! Needed this feature quite urgently - seemed the simplest solution for my problem ;) Thanks for the quick support though ! |
I'd use the |
Thanks and done :) 🎱 |
According to this Mustache can make use of helpers like so to, for instance, convert a title uppercase:
I was just curious why this doesn't work for dynamic partials as well that were added through the addHelper method?
|
It should, but only if you're using the |
In addition to the section contents, section lambdas (higher order sections) would receive a lambda helper as a second parameter. This helper consists of a single
render
method, allowing a template to be rendered using the current context stack.The returned result from a section lambda is automatically rendered as a mustache template, which is sufficient most of the time. But if you want to do something with the rendered value of that template, it doesn't quite cut it.
For example, this data:
... and this template:
... would uppercase the
{{ FOO }}
template string, rather than the value of foo. This would then be automatically rendered, resulting in an empty string, since there is noFOO
value available.With this change, a mustache lambda helper is passed as the second parameter to the anonymous function, giving you a way to render the section value:
... which would render as
WIN
, exactly like you'd expect.In other languages (JavaScript, Ruby), this method would be bound to the closure context and just be available inside the anonymous function, but this isn't an option in PHP :)
This change is backwards compatible, as the second value will simply be ignored by any section lambda not referencing it.