简体   繁体   中英

symfony twig array key display

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%}

您应该使用圆点而不是方括号:

entities.0.content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM