You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Not sure if it's not possible or if I have not found the right way to do it ?? I am not very experienced in php scopes sorry if this is a stupid question...)
I would like to be able to pass a php function to pug at render time, to be able to call it directly in my template. Without resorting to a global function.
That way I would be able to use the same helper function name in all my template, even if its behaviour is dependent of the route context.
Something like:
// in HomeController.phppublicfunctionget() {
functionpathFor($pathName) use ($this) {
return$this->router->pathFor($pathName);
}
return$this->pug->render('productPage.pug', $this->templateParams, [pathFor?] )
}
//- in productPage.pug
p
#[a href=pathFor('/home')) Click here quick quick quick QUICK!]
would get me
<ahref="/amazing-product-you-will-like-and-buy-home-page"> Click here quick quick quick QUICK ! </a>
Thanks for your help !
Btw: Amazing package 💯
The text was updated successfully, but these errors were encountered:
The easiest in your case would probably be a closure:
publicfunctionget() {
$routerPathFor = array($this->router, 'pathFor');
$pathFor = function ($pathName) use ($routerPathFor) {
return call_user_func($routerPathFor, $pathName);
};
// Depending on your PHP version you can use directly $routerPathForreturn$this->pug->render('productPage.pug', $this->templateParams, array(
'pathFor' => $pathFor
));
}
You can also use share to get the function available in any template:
// somewhere in a global place after $this->pug is instanciated// for example, the constructor of an abstract class that all controllers extend$this->pug->share('pathFor', $pathFor);
(Not sure if it's not possible or if I have not found the right way to do it ?? I am not very experienced in php scopes sorry if this is a stupid question...)
I would like to be able to pass a php function to pug at render time, to be able to call it directly in my template. Without resorting to a global function.
That way I would be able to use the same helper function name in all my template, even if its behaviour is dependent of the route context.
Something like:
would get me
Thanks for your help !
Btw: Amazing package 💯
The text was updated successfully, but these errors were encountered: