duanhuan5409
2015-09-15 16:05
浏览 102
已采纳

symfony twig数组键显示

I am trying to get the data from the db using array index however i do not want to iterate.

content table:

|id|      content          |active
|1|this is first content   |yes
|2| This is secound Content| yes

Controller.php:

$em = $this->getDoctrine()->getManager();
$entities= $em->getRepository('TestCMSBundle:Content\Content')->GetContents();

return $this->render('TestCMSBundle:Default:index.html.twig', array('entities' => $entities));

mytwig:

{{ entities.content[0] }}

some other contents.... blah blah

{{ entities.content[1]}}

but i canot seems to get the content.

Error: Key "content" for array with keys "0, 1" does not exist in TestCMSBundle:index.html.twig at line 80

please i do not want to iterate over the loop like:

{% for entity in entities %}
{{ entity.content}}
{%endfor%}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用数组索引从数据库获取数据但是我不想迭代。

内容表:

  | id |  content | active 
 | 1 |这是第一个内容| yes 
 | 2 | 这是内容| 是
   
 
 

Controller.php:

  $ em = $ this-> getDoctrine() - >  getManager(); 
 $ entities = $ em-> getRepository('TestCMSBundle:Content \ Content') - > GetContents(); 
 
return $ this-> render('TestCMSBundle:Default:index.html  .twig',array('entities'=> $ entities)); 
   
 
 

mytwig:

   {{entities.content [0]}} 
   
 
 

其他一些内容.... blah blah

   {{entities.content [1]}} 
   
 
 

但我似乎得到了内容。

错误: 在“TestCMSBundle”中不存在键“0,1”的数组的“内容”:第80行的index.html.twig

请我不要 迭代循环,如:

  {%for entity in entity%} 
 {{entity.content}} 
 {%endfor%} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doureng5668 2015-09-15 16:21
    已采纳

    You should use dots instead of brackets:

    entities.0.content
    
    点赞 打赏 评论

相关推荐 更多相似问题