Dominykas55
Dominykas55

Reputation: 1231

Symfony2 including twig with a controller variable

I am learning symfony2 and for the first time Im really stuck and have no idea what to do. I have 3 pages created, an index page, product page and a special offer page. All of these pages need to use one dynamic sidebar from another template.

Here is the controller for the actions:

public function indexAction()   ///// im showing the products form mysql here
    {

            $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
               return $this->render('MpShopBundle:Frontend:index.html.twig',  array(
               'products'=>$products       
               ));

    }
    public function viewAction($id) //// im showing a single product based on the id
    {

        $em = $this->getDoctrine()->getManager();
        $product = $em->getRepository('MpShopBundle:Product')->find($id);

        return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
        'product'=>$product
        ));

    }

     public function specialAction()    //// a simple page to test the sidebar
    {

            $em = $this->getDoctrine()->getManager();
            $products = $em->getRepository('MpShopBundle:Product')->findAll();
               return $this->render('MpShopBundle:Frontend:special_offer.html.twig',  array(
               'products'=>$products       
               ));

    }

This is the sidebar template code for generating the categories(category is only shown if a product has it):

{% for product in products %}

                <li class="subMenu"><a> {{ product.category }} [{{ product|length }}]</a>
                <ul>
                    <li><a href="{{ path('products') }}">{{ product.subcategory }} ({{ product|length }})</a></li>

                </ul>
                </li>

            {% endfor %}

I am including the sidebar template on my pages like this:

{% block sidebar %} {% include 'sidebar.html.twig' %} {% endblock %}.

The problem: It works perfectly on index and special offer pages but on the view page im getting an undefined variable products error. I think i know the problem, its because my variable for that page in the controller is product(to get a specific product). So how can i make this sidebar work dinamically on all of the webpages?

FIXED The problem was fixed by using the http://symfony.com/doc/2.0/book/templating.html#embedding-controllers tutorial. If anyone has the same issues read this!!!!

Upvotes: 3

Views: 2405

Answers (1)

Alessandro Lai
Alessandro Lai

Reputation: 2274

public function viewAction($id) //// im showing a single product based on the id
{

    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository('MpShopBundle:Product')->find($id);
    $products = $em->getRepository('MpShopBundle:Product')->findAll();

    return $this->render('MpShopBundle:Frontend:product_details.html.twig', array(
        'product' => $product,
        'products' => $products
    ));

}

Upvotes: 1

Related Questions