Giter VIP home page Giter VIP logo

Comments (7)

silentworks avatar silentworks commented on May 30, 2024

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.

from twig-view.

shamotj avatar shamotj commented on May 30, 2024

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.

from twig-view.

akrabat avatar akrabat commented on May 30, 2024

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?

from twig-view.

shamotj avatar shamotj commented on May 30, 2024

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

from twig-view.

shamotj avatar shamotj commented on May 30, 2024

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.

from twig-view.

AlexNodex avatar AlexNodex commented on May 30, 2024

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!

from twig-view.

akrabat avatar akrabat commented on May 30, 2024

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.

from twig-view.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.