2017-09-20 38 views
0

我想回報呈現模板,如果一些條件是在這個函數中真正:

* @Route("/results", name="front.tour.results") 
* @Template 
*/ 
public function indexAction(Request $request, $countryUrl=NULL, $placeUrl=NULL) 
{ 
    ... 

    if(condition) { 

    $twig = $this->get('twig'); 

    return $twig->render('@App/FrontModule/Tour/hotelList.html.twig', [ 
      'allPossiblePlaces' => $allPossiblePlaces 
     ]); 
    } 

但我得到的異常與消息:

參數2傳遞給 的Symfony \捆綁\ TwigBundle \ TwigEngine ::的renderResponse()必須給出的 型陣列,串的

我100%確定變量$allPossiblePlaces是數組,所以我不明白。有任何想法嗎?

+1

您使用Symfony的?嘗試使用'$ this-> render'。你在renderResponse方法中有個例外,所以我認爲問題出在twig服務中......壞的使用或其他的東西 –

+1

爲什麼你沒有使用'$ this-> render'? –

+0

感謝它的工作。是的,我正在使用symfony。但仍然我不明白我有另一個功能(只有沒有路線註釋)它的作品就像我寫在我的問題。有什麼不同? (也有數組參數) –

回答

1

試試這個:

* @Route("/results", name="front.tour.results") 
* @Template 
*/ 
public function indexAction(Request $request, $countryUrl=NULL, $placeUrl=NULL) 
{ 
    ... 

    if(condition) { 

    return $this->render('@App/FrontModule/Tour/hotelList.html.twig', [ 
     'allPossiblePlaces' => $allPossiblePlaces 
    ]); 
} 

如果你想在周圍的Symfony視圖引擎更多的功能,請閱讀本文件:https://symfony.com/doc/2.7/components/templating.html

-12

你行更改爲

return $twig->render('@App/FrontModule/Tour/hotelList.html.twig', array(
      'allPossiblePlaces' => $allPossiblePlaces 
     )); 

,它會工作。

基本上,它的一個語法問題。 Twig解析它收到的每個參數,並且它不能將[]'表示法解析爲一個數組,因此也不能解析問題。

+1

這是不正確的,我在我的代碼中使用'[]'無處不在,當渲染小枝時,我從來沒有遇到過任何問題。 – kunicmarko20

+5

你從哪裏得到這個錯誤信息? –

相關問題