4

When is it more appropriate to create a .phtml file and place it via local.xml, and when is it more appropriate to create a widget and place it in a static block via the backend?

For example, I once had my custom main navigation as a widget/block on the backend, but moved it into a .phtml file and included it with my local.xml so that I could more easily make changes, use version tracking, etc.

My main concern is the impact it could have on performance, since my main navigation includes a list of dozens of Magento categories.

So whats 'best practice'? Does it matter? Does it impact performance?

8

I don't think there is a right answer for this.
The path you choose depends on your needs.
I usually avoid using widgets. Specially when working with "not so technical" clients.
The widget system in Magento is not that easy to use or to explain to someone else.
I usually put everything in phtml files so the chance of something getting screwed up is smaller. Or if something gets screwed up, I know it's my fault and I know where to look.

From time to time I like to create widgets with a minimum configuration needed, but I do this only when the client wants to use the widgets in the homepage or other static pages.
I rarely tell my clients about the full widget system of magento, that allows you to place widget instances on different sections of each page. I rather spend 30 minutes adding something on the left column, than spending 2 hours looking for how to remove something placed wrong on the left column.

But in almost all the cases I avoid using local.xml. That looks to me like it's a bit dirty. I think it's better to put your custom widgets or blocks or phtml files inside a custom module. The advantages are:

  • portability (you can easily isolate and re use the module in another project)
  • you can easily disable you functionality if needed
  • the chances of something going wrong in an upgrade are lower and if something goes wrong...refer to the point above.

Performance can be affected even if you use a widget or a phtml file. I don't think the difference is significant. Maybe it's a little slower with the widget approach because the widgets have to be parsed and interpreted, but I could be wrong.

  • 1
    Its helpful to get an authoritative view on the topic. Thanks Marius – andyjv Nov 18 '14 at 12:54
  • I don't know about "authoritative". I'm not an "authority" on this. It's just my opinion and what I found during the years. – Marius Nov 18 '14 at 12:55
  • 3
    Well, I guess what I meant is, I trust your opinions :P – andyjv Nov 18 '14 at 13:04
0

You have to create first .phtml file. Link that file to any static block using indentifier.

getLayout()->createBlock('cms/block')->setBlockId('your identifier')->toHtml(); ?>

it's quite easy for further change at any time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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