2

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%}
6
  • entities.content.0 Commented Sep 15, 2015 at 16:06
  • {{ entities.content.0 |raw }} i still get the same error Commented Sep 15, 2015 at 16:10
  • try {% set entity = entities|first %}, then you can simply write entity.content Commented Sep 15, 2015 at 16:12
  • okay i got the first item... but how can i get second or third index ..??? Commented Sep 15, 2015 at 16:18
  • Sorry, in your case should be: entities.0.content. Commented Sep 15, 2015 at 16:20

1 Answer 1

4

You should use dots instead of brackets:

entities.0.content
Sign up to request clarification or add additional context in comments.

1 Comment

i tried with {{ entities.0.content }} {{ entities.1.content }} and it worked.... Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.