I've seen examples in extensions and in core where blocks are created through PHP code, e.g. in a controller action:

$block = $this->getLayout()->createBlock("company_module/...");

Or a block sets its own template, usually in the _construct()

$this->setTemplate("... .phtml");

I can see the programmatic approach being necessary if there is business logic involved that decides which block to create (or perhaps not create) and which .phtml template to set, depending on the circumstances.

But there are plenty of examples where there appears to be no business logic involved, yet the developer decided NOT to use the declarative approach of the .xml

Why would one want to NOT use .xml for creating blocks? What is best practice?

3 Answers 3


It depends on several things. This is because each and every developer has different approaches to different scenarios.

For an example, suppose I need to do change a template of a core block. I can do it programmatically or via layout update. Now some developers think in this way

"What I want to do is to just change a template. Then why should I define a new layout update file in config.xml, put the layout file in the theme folder and then construct xml to change the template of that block?? Let us approach this programmatically"

Now some people think this way

"I want to change the template of this block. It is clearly a layout update. So it should be done via layout update. Let us go with a new layout update file and do this in a fraction of second"

So this is purely depends upon a programmer. Someone like to do certain things in programmatic way. Some others love layout update technique. Both are correct and we can't blame anyone.

But suppose, if our module requires a lot of layout updates, then doing all those things programmatically will not be a good approach. In that case, we should consider to use a layout update xml file. That is more elegant and cleaner method. But for small layout udpates or if we want to apply some "dynamic logics" to layout, then it is convenient to go on with programmatic approach.


Great question, although a question like this I don't think has any definitive answer. There are also bound to be a dozen answers on this topic.

The only compelling reasons I can give to not use layout XML to declare blocks:

  • You need to dynamically select the block to instantiate
    • ie: in the case of "type renderers"
  • You are lazy

Honestly, it's very convenient and very easy to use the programmatic method for creating blocks, but I would argue against it as being "bad practice." Anybody who has tried to deal with extending the Magento dashboard system will get my drift here.

Using layout XML to declare your blocks (or even containers for blocks that might have more advanced logic) makes your design much more accessible to the developer. Such that, to do simple things like changing a template, this can be done without having to configure a block rewrite in a new module (or worse, editing your code directly).

  • +1 for the points ->being lazy -> bad practices. thought to add those points in my answer. But forgot. Jul 13, 2015 at 3:41

If you're using Lesti FPC (https://gordonlesti.com/projects/lestifpc/), one reason for creating a block dynamically could be that you can't holepunch blocks that have children, except if their children are created dynamically in the constructor of the parent block.

Other than that, doing it the XML way is better imho and is a best practice.

For some reason though, the adminhtml uses the programmatic method a lot more than using layout xml. Maybe it's due to the complexity, as Rick pointed out.

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.