On 20.10.2010, at 17:33, Thibault D wrote:

> If you need a variable in your layout, you may consider writing a
> template helper for it. If it's a menu, MenuBundle provides an easy
> way to create a helper for your menu (http://github.com/knplabs/
> MenuBundle/blob/master/Resources/doc/02-Integrate-With-
> Symfony.markdown)


yes, template helpers can certainly cover some of the task.
however i dont think that they are the way to go for everything one might want 
to inject dynamically. it just seems wrong to just offer a way for templates to 
pull data in. this seems like it will inevitably lead to more and more logic in 
the templates.

i have another use case that we might need to address somehow. i am actually 
AJAJ-ifying DoctrineUserBundle. that means i will actually need to return the 
HTML as a JSON response. I rather not do AJAX. now the tricky bit is that while 
there is the magic _format handling inside the routes, the issue is that it 
immediately makes all of your templates magically look for other templates. but 
in my case i want to return HTML wrapped inside a JSON structure. for now the 
only good approach I have found here is to actually have a generic wrapper 
controller which gets a URI passed to it, creates a subrequest by matching the 
uri.

    protected function handleRequest($request, $i)
    {
        if (empty($request['uri'])) {
            throw new \Exception('no uri or controller given for index: '.$i);
        }
        if (empty($request['query'])) {
            $request['query'] = array();
        }

        $request['uri'] = 
preg_replace('/^('.preg_quote($this->request->getScriptName(), '/').')?\//', 
'', $request['uri']);
        $subRequest = $this->request->create($request['uri'], 'get', 
$request['query']);
        if (false === ($parameters = 
$this->router->match($subRequest->getPathInfo()))) {
            throw new \Exception('uri did not match a route for index: '.$i);
        }
        $subRequest->attributes->add($parameters);
        $response = $this->kernel->handle($subRequest, 
\Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST);

        return array(
            'id' => $i,
            'status' => $response->getStatusCode(),
            'html' => $response->getContent(),
        );
    }

this method is also capable of issuing multiple such requests in one call, so 
this is a bit of added functionality for the cases where i need to fetch output 
from multiple controller (actions). but i wonder if we can make this use case 
also a bit easier for Bundle developers to support. aka making it possible to 
also return JSON structures. so far the only alternative approach i could think 
of using a php template as a layout template that does the call to 
json_encode() and potentially also sets the headers. but again this feels very 
unclean.

regards,
Lukas Kahwe Smith
[email protected]



-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to