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 render function #37

Closed
shamotj opened this issue Aug 27, 2015 · 7 comments
Closed

Add render function #37

shamotj opened this issue Aug 27, 2015 · 7 comments

Comments

@shamotj
Copy link

shamotj commented Aug 27, 2015

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:

{{  render('Admin\\Action\\UserAction:info', { 'msq' : 'test'}) }}

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.

@silentworks
Copy link
Member

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.

@shamotj
Copy link
Author

shamotj commented Aug 27, 2015

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.

@akrabat
Copy link
Member

akrabat commented Aug 27, 2015

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?

@shamotj
Copy link
Author

shamotj commented Aug 28, 2015

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.";

@shamotj
Copy link
Author

shamotj commented Aug 28, 2015

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.

@AlexNodex
Copy link

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:
(Psuedo code)

Hooks::Subscribe('name.of.hook',function($context,$args) {
 // $args['key'] === 'value';
// $args['foo'] = 'bar';
return "{% include '@namespace/path/to/template.html' with ".json_encode($args)." %}";
});

Inside a template file:
{% hook 'name.of.hook' with {'key':'value'} %}

Then you just need the node token parser and node visitor which is simple enough!

@akrabat
Copy link
Member

akrabat commented May 7, 2018

Closing as I don't think that the ability to arbitrarily execute any method on any object registered in container should be possible.

Use {% include %} to render additional templates, or write an custom extension function if application specific logic is required.

@akrabat akrabat closed this as completed May 7, 2018
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

4 participants