I am working on e-commerce store in Magento and about to finish my work. I want to optimize my code its size/time complexity. In my product view there are 4 different sliders one of then include another 4 subslider. For example if slider3.phtml which call another sliders depends on some category id.

Here is code-

if ($result == '26') {    
    echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('slider/themes.phtml')->toHtml();  

if ($result == '27') {
   echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('slider/collabs.phtml')->toHtml();

And same for two more sliders.

The slider which I called in if code block(theme.phtml/collabs.phtml/etc) includes same repeated code only one change is there of category id-. And the code is more than 300 lines.

I want to know if this is fine? or do I need to call it with function? means I will create only one phtml file and create function which call this file with parameter i.e category id.

Or there is no need to do any thing with the code, because code is absolutely working fine. This will affect time complexity of my store?

Please explain me. thanks in advance.

1 Answer 1


There is quite a simple way that you create sub directory named as category id. so you can easily lookup template file more more quickly.

Your source code should be:

echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate("slider/" . $result . "/themes.phtml")->toHtml();

If I understand you correctly, you have 4 sample sliders for all categories, you are able to create a custom attribute for your category with select box. So you are able to define its layout name in your backend.

$template_location = $this->getTemplateLocation($result);
echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate($template_location)->toHtml();

You should define a function getTemplateLocation inside your block for defining template location from custom attribute option. By doing this way, you are able to manage your source code much more easier & cleaner. You can change, add more layouts quickly by magento backend too.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.