簡體   English   中英

$ twig-> render()異常-參數必須是數組,給定字符串

[英]$twig->render() exception - argument must be array, string given

如果此功能滿足某些條件,我將嘗試返回渲染的模板:

* @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
        ]);
  }

但是我收到消息異常:

傳遞給Symfony \\ Bundle \\ TwigBundle \\ TwigEngine :: renderResponse()的參數2必須為數組類型,字符串為給定

我100%保證變量$allPossiblePlaces是數組,所以我不會$allPossiblePlaces 有任何想法嗎?

嘗試這個:

* @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

將行更改為

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

它將起作用。

基本上,這是一個語法問題。 Twig解析收到的每個參數,並且無法將“ []”符號解析為數組,因此無法解析問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM