3

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!!!!

3
  • you need to pass this products $products = $em->getRepository('MpShopBundle:Product')->findAll(); in return statement, return $this->render('MpShopBundle:Frontend:product_details.html.twig', array( 'product'=>$product, 'products'=> $products )); Commented Apr 16, 2015 at 11:00
  • That doesnt work because now im getting an undefined variable product. Commented Apr 16, 2015 at 11:06
  • @Dominykas55 my point to you is same as Jean answer Commented Apr 16, 2015 at 11:28

1 Answer 1

1
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
    ));

}
Sign up to request clarification or add additional context in comments.

1 Comment

I fixed it with embedd controller but I tried your code and it worked too. 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.