1

I have an action for login process. Now I want to use the same action for normal login process and for ajax requests also.

        /**
         * @Route("/sponsor/login", name="sponsor_login", options={"expose"=true})
         * @Template("SponsorBundle::login.html.twig")
         * @return array
         */
         public function loginAction()
         {}

I want this action to render different view files for xmlhttp request and for normal http request.How do i do that? and I want to pass view file in a json object

aditya
  • 996
  • 2
  • 12
  • 25
  • You could also check this [answer](http://stackoverflow.com/a/11715894/970721) – Vitalii Zurian Sep 12 '12 at 05:48
  • But how did you managed to render the template inside a json? Something like `json_encode(array('output'=> rendertemplate()))` – Adib Aroui Apr 29 '15 at 14:03
  • This maybe will help others to render view as part of a json http://stackoverflow.com/questions/13257156/how-do-i-return-a-twig-rendered-template-as-part-of-a-json-response – Adib Aroui Apr 29 '15 at 14:04

1 Answers1

1

You can do this:

return $this->getRequest()->isXmlHttpRequest()
 ? $this->render(.... "form.html.twig" ....)
 : $this->render(... full page that will include the form ...) ;

or

if ($this->getRequest()->isXmlHttpRequest()){
  $template = "form.html.twig" ;
  $params = ....
} else {
  $template = "login.html.twig" ;
  $params = ....
}

return $this->render($template, $params) ;
Zeljko
  • 5,048
  • 5
  • 36
  • 46
  • But how to render the template inside a json as OP mentionned? – Adib Aroui Apr 29 '15 at 14:01
  • This maybe will help others to render view as part of a json http://stackoverflow.com/questions/13257156/how-do-i-return-a-twig-rendered-template-as-part-of-a-json-response – Adib Aroui Apr 29 '15 at 14:04