2012-10-06 23 views
2

是否可以爲Symfony 2.0中的某個動作分配兩個不同的模板?在下面的代碼中,我想用兩個不同的模板呈現IF部分和ELSE部分。一個Symfony 2.0控制器 - 帶兩個不同模板的動作

/** 
* 
* @Route("/", name="search") 
* @Template("Bundle:Search:search.html.twig") 
*/ 
public function indexAction() 
{ 
    if ($searchText == null) { 

     return array('form' => $form->createView(), 'form2' => $form2->createView()); 

    } else { 

     return array('applicants' => $appCount, 'pagination' => $pagination, 'form' => $form->createView(),'form2' => $form2->createView()); 
    } 
} 

回答

4

你不能這樣做,與@Template註釋,但您可以手動渲染每個:

if ($someCondition) { 
    return $this->render('Bundle:Controller:template.html.twig', array(
     'some' => $thing, 
    ); 
} 

return $this->render('Bundle:Controller:another.html.twig', array(
    'another' => $thing, 
); 
相關問題