1

I created a twig extension that returns me the list of all the blogs I have. This list is an array that I loop through in my twig template.

Here's my extension:

<?php
// src/OSC/BlogBundle/Twig/BlogsListExtension.php
namespace OSC\BlogBundle\Twig;

class BlogsListExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return array(
            'blogsList' => new \Twig_Function_Method($this, array($this, 'blogsList')),
        );
    }



    public function blogsList()
    {

        $em = $this->getDoctrine()
        ->getManager();
        $repository = $em
            ->getRepository('OSCBlogBundle:Blog');


        $blogs = $repository->findBy(array('visibleState' => true));
        usort($blogs, array("\OSC\BlogBundle\Controller\BlogController", "orderBlogByTitle"));


        return $blogs;
    }

    public function getName()
    {
        return 'osc_BlogsListExtension';
    }
}

Here's what I added in my services.yml

services:
    osc_blog.blogsList_extension:
        class: OSC\BlogBundle\Twig\BlogsListExtension
        tags:
            - { name: twig.extension }

In my twig template, I want to do the following:

<ul>
        {% for blog in blogsList()|sort %}
         <li><a href="{{ path('osc_blog_homepage', {'blogId': blog.id })}}"><span>{{ blog.title }}</span></a></li>
        {% endfor %}
        </ul>

I get the following error:

An exception has been thrown during the compilation of a template ("Notice: Array to string conversion

Finally, my question is how can I pass an array to a variable in order to loop through it in a twig template ?

4
  • Have you done any debugging? What would be the output of var_dump($blogs)? Is it an array at all? Also why there is a double sorting? First in the php file (useort) and then another one in for statement. Commented Jun 18, 2013 at 16:24
  • $blogs is an array that returns all the blog objects Commented Jun 18, 2013 at 16:31
  • Is the posted code your entire twig template? Commented Jun 18, 2013 at 16:59
  • @mattexx no, it is not. But that's the only thing that changed before stopping to work. Commented Jun 18, 2013 at 17:51

1 Answer 1

2

This should happen because blogsList() returns an ArrayCollection, wich acts as an array, but it is not.

According to this issue, you should just do:

public function blogsList()
{
    // ...
    return $blogs->toArray();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Even after doing what you proposed (clearing the cache as well), I still get the same error... Why is it telling me that I want to convert an array to a string ?
Pretty strange - be sure to be dealing with a proper array, consider then returning a mock of the data you want in order to see where the problem lies.
Before, I was just passing the variable blogs to every template but it was getting heavy and code was getting duplicated each time. But, when passing blogs and looping through it (without using the toArray), it worked...

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.