Skip to content
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

Add LambdaHelper->getContext() #301

Open
levu42 opened this issue Jul 28, 2016 · 0 comments
Open

Add LambdaHelper->getContext() #301

levu42 opened this issue Jul 28, 2016 · 0 comments

Comments

@levu42
Copy link

levu42 commented Jul 28, 2016

I'd like to add a helper function($arg, $lambda) {…} that is able to load dynamically a subtemplate from the argument. So I'd like to accomplish {{> {{variable}} }} by doing {{#loadPartial}}{{variable}}{{/loadPartial}} but I'm sticking with this solution until now:

function mustacheLambdaHelperGetContext($lambda) {
  $f = function(Mustache_LambdaHelper $lambda) {
    return $lambda->context;  
  };
  $f = Closure::bind($f, null, $lambda);
  return $f($lambda);
}
/** … the same with ->mustache instead of ->context **/

$engine->addHelper('loadPartial', function($args, $lambda) {
  $partialName = $lambda->render($args);
  $context = mustacheLambdaHelperGetContext($lambda);
  $engine = mustacheLambdaHelperGetMustache($lambda);
  if ($partial = $engine->loadPartial($partialName)) {
    return $partial->renderInternal($context);
  }
});

And this solution is very ugly. So at least, if not implementing a LambdaHelper->renderPartial($name) or something, please implement a getContext() and a getMustache() method or make $context and $mustache protected instead of private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant