views:

120

answers:

2

SO I am trying to make a restful api system with symfony and I am a little confused about something. So every different route gets sent to a different action method to be handled, however, many different actions display the same sort of data (list of users or a list of groups). I don't want to have to have redundant templates just because the action name and the template need to be in sync. Is there anyway to have several actions forward to the same template?

Thanks!

A: 

Well... basically a template is paired with an action. If two of your actions are able to pass the exact same variables to a template, it is likely that one of the actions is redundant.

You can return a custom template by just adding:

return 'myAmazingTemplate';

... at the end of an action. This would pick it up from the same module's templates/ folder. I haven't played around with paths for these. The template might need to be named 'myAmazingTemplateSuccess' to be recognized, can't recall exactly.

Alternatively, you could look into partials/components/slots and render any common elements via these.

Tom
This would not work, as this would result in lookin for a template called '[actionName]myAmazingTemplate', as the default for an action (no return specified) is 'Success' which results in '[actionName]Success'.
bouke
The answer's just about the notion of returning a custom template... don't be a prude. Ok then, one can use "return myAmazingTemplateSuccess" and ensure there's a "actionMyAmazingTemplateSuccess" available.
Tom
+1  A: 

Your can set the template from within your action by calling:

  $this->setTemplate('myCustomTemplate');

If the template is in a different template you can specifiy 'module/templatename' instead.

You can find more info in the Symfony Docs

Twelve47