-
Notifications
You must be signed in to change notification settings - Fork 87
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 render function #37
Comments
This reminds me of something I have seen in Laravel, myself personally wouldn't have a use case for it and we haven't gotten any request for something like this before. I don't think this would be something that should be added to the Twig-View, probably better if you create an extension repo and we can provide a link to it from this repo. |
One of use case is described for example in Symfony doc http://symfony.com/doc/current/book/templating.html#embedding-controllers. TL;DR: Lets say I have sidebar on my page and I want to show last 3 blog posts (or comments etc). I can create separate action + template and include it in any other twig template. |
If we included this in Twig-View, I'd want to see it as a separate extension class that has to be independently registered. Out of interest, how do you handle setting up the request object for the route callable? |
I bypass routing completly. Because such request should not be reachable by URL, its generated content should be output always within other template with different URL. So I just use this: if (preg_match('/^(.*):(.*)$/', $action, $matches)) {
list($action, $callable, $method) = $matches;
if ($this->container->has($callable)) {
return $this->container->get($callable)->$method($args);
} else {
return "Action $callable is not callable.";
}
}
return "Action '$action' not found."; |
Almost forget. Initialy I had it like this: $headers = $this->app->getContainer()->get('request')->getHeaders();
$cookies = $this->app->getContainer()->get('request')->getCookieParams();
/** @var \Slim\Http\Response $response */
$response = $this->app->subRequest('get', $action, '', $headers, $cookies, '', new Response());
if ($response->isSuccessful()) {
return (string) $response->getBody();
} else {
return "Action '$action' not found.";
} But it was litle bit slower than my current sollution and it requires to register route. Such route will be than reacable on it own as well and that was not intended. |
Why not implement it as a hook? In our CMS (which I am heavily considering porting to Slim) I devised a hook strategy to hook into parts of the template. The concept is a simple one. Inside a controller listen for the hook and display / render a template accordingly:
Inside a template file: Then you just need the node token parser and node visitor which is simple enough! |
Closing as I don't think that the ability to arbitrarily execute any method on any object registered in container should be possible. Use |
Sometimes I need to include small template within other twig template. In symfony there is render() function in twig for this. I wrote twig extension which adds this functionality with Slim 3. It is quite short and I think we can add it to twig-view. I believe that many users with slim+twig could benefit from this.
usage can be as simple as:
where Admin\Action\UserAction is registered callable in container.
What is you opinion on this? Is this usecase scanario common enough to include this function to twig-view? I have it working and I can create pull request if so.
The text was updated successfully, but these errors were encountered: