Comments (7)
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.
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.
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.
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.
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.
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.
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)
- Slim\Views\Twig::create does not exist HOT 3
- Add support for Twig 3 in 3.x branch
- Slim 4 Compatibility In The Pipeline? HOT 1
- Rendering no space HOT 1
- Fix incorrect declaration HOT 3
- Version on Readme HOT 1
- Twig could not be found in the server request attributes using the key "view" HOT 5
- ,
- How to get args for is_current_url()? HOT 2
- Using view outside a request/response cycle does not load the Slim/Views/TwigExtension HOT 2
- Trying to use TwigExtension template functions returns an error HOT 1
- Uncaught Twig\Error\SyntaxError: Unknown "defined" test. HOT 3
- is_current_url() doesn't take basepath into consideration HOT 21
- Support PHP 8.0
- twig->fetch() outputs to browser rather than return string HOT 8
- 3.3.0 Release date HOT 3
- `composer require slim/twig-view:^3.0` handle PHP Version Problem HOT 1
- Rendering a template from a middleware HOT 4
- Error using Twig with Slim 4 & Container HOT 4
- TwigMiddleware::createFromContainer does not set request attribute "view"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twig-view.