1
votes

I have this function in a CategoryBundle:CategoryTreeBuilderController:

/**
 * Get subcategories based on $parent_id parameter
 *
 * @Route("/category/tree/{parent_id}", name="category_tree", options={"expose"=true})
 * @Method("GET")
 */
public function BuildCategoryTree($parent_id = null) {
    $em = $this->getDoctrine()->getManager();
    $entity = $em->getRepository('CategoryBundle:Category')->findBy(array("parent" => $parent_id));

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Category entity.');
    }

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

I need to call this function recursively from my Twig template so every times the parent has children a kind of indentation occurs. I'm trying to implement a Tree behavior. I check this post but is not helpful at all, can any guide me on the right path?

1

1 Answers

1
votes

I think better make a macro in template and iterate over all childs recursively.

Anyway, if you want to embed check this article. You can simple can call controller in template

{% for entity in entities %}
    {{ render(controller('AcmeArticleBundle:Article:BuildCategoryTree', {
        'parent_id': entity.id
    })) }}
{% endfor %}