Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allow pass additional variables to the Twig template #299

Closed
waiting-for-dev opened this Issue Sep 17, 2012 · 9 comments

Comments

Projects
None yet
3 participants

I don't know if I'm missing something, but I think that what I'm trying to do is very common, but I think it's not possible with FOSRestBundle.

I'm using the same action to deal with an html and a json response, just suffixing the later one route with .json.

The json is working with no problem, but when the response is html I need to pass an additional variable to the Twig template, and I don't know and I don't find how to do it. At the moment, my code is:

// $entity = get my data;

$name = 'foo';

$view = $this->view($entity, 200)
   ->setTemplate('MyBundle:MyController:myTemplate.html.twig')
   ->setTemplateVar('entity')
   ;

return $this->handleView($view);

How can I pass $name to myTemplate.html.twig? What I would need is something like:

//...
->setTemplate('MyBundle:MyController:myTemplate.html.twig', array('name' => $name))
//...
Owner

lsmith77 commented Sep 17, 2012

you will need to pass all data in as the first parameter where you are currently just passing in $entity. now i am not sure if there would be a way to get the same json data structure in this case. so you might need to add some conditional there like if ($view-> isFormatTemplating()) { $view->setData(array('name' => $name, 'data' => $view->getData())) }

Thanks a lot for your response lsmith77. But, how can I know if isFormatTemplating(). First of all, instead of:

if ($view->isFormatTemplating())

I have to use:

if ($this->get('fos_rest.view_handler')->isFormatTemplating())

But maybe this is because I'm not using the listener support.

Anyway, isFormatTemplating() expects one parameter, $format, and $view->getFormat() is always returning NULL

Owner

lsmith77 commented Sep 17, 2012

ah right .. the method is on the ViewHandler. also you need to pass in the format.
if ($this->get('fos_rest.view_handler')->isFormatTemplating($_format))

the $_format variable can just be placed into your action signature.

Ok, thank you very much. This worked, setting $_format as the first parameter in the action signature, and providing _format: html and _format: json for each route.

Shouldn't this be more straightforward? I mean, a way to automatically set other variables for the template, I think ideally in the same setTemplate():

->setTemplate('MyBundle:MyController:myTemplate.html.twig', array('name' => $name))

I think this should be a feature request (I leave the ticket opened just in case this is accepted as a feature request, otherwise close).

Thank you very much again, you saved me a headache day :)

Owner

lsmith77 commented Sep 17, 2012

hmm not sure. we could offer an interface and a default implementation which allows setting different data for templating and non templating formats which is then checked for inside the ViewHandler. will need to think about this.

I think I have been dealing with a very common procedure, because I just generated the controller with the Admin Generator, and in the show action the $deleteForm is passed to the template.

Owner

lsmith77 commented Sep 17, 2012

@schmittjoh wondering if we could annotate a class in such a way that only a single property would be serialized and that the output would be exactly like as if the contents of that property would have been passed to the serializer?

Jumping in on this one with nothing to add other than this caught me out as well.

Owner

lsmith77 commented Nov 3, 2012

maybe you can get some feedback from @schmittjoh by opening a ticket on JMSSerializerBundle

@lsmith77 lsmith77 closed this Jul 10, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment