2

I know of two ways to add a static block to a template. I'm wondering which correct method, I suspect the second method is, but why? Are both correct?

METHOD 1: $this->getLayout()->createBlock()

Create a static block in Magento admin and call it from within your template file with the code below.

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('something_custom')->toHtml() ?>

Method 2: $this->getChildHtml()

Similarly create a static block in Magento admin but first declare it in your layout xml with:

<block type="cms/block" name="something_custom">
    <action method="setBlockId"><block_id>something_custom</block_id></action>
</block>

And then call it from within your template file with the code below.

<?php echo $this->getChildHtml('something_custom') ?>

2 Answers 2

3

tl;dr use Method 2


If you're building the code for a site that you're maintaining and the code is never going to be used by others, then the differences between the two methods are almost irrelevant. I would still use the second method because I don't fancy the logic in the templates from the first method.

If you're building modules that are meant to be used by others, then be kind to them and use the second method since it allows other developers to overwrite your functionality in a kosher way, via layout updates.

/Edit: If you're ever going to change the theme, then again you want to use the second method, even if you're the only maintainer of the code, like I said in the first paragraph.

4
  • thanks what is it about the logic in templates which you don't like
    – Holly
    Commented Sep 29, 2015 at 8:59
  • 2
    Well.. long story.. :D In Magento the view is made out of blocks which represent some data in a specific way; some blocks happen to use templates (core/template) to present this data while other use other methods like direct text output for instance (core/text). The template is only something that the block uses to render itself, so the (data) logic should live in the block class. You could still have iterations or conditional statements in a template, but strictly rendering related logic. Let's say that you want to add the same block in two places in the page. TBC...
    – Adi
    Commented Sep 29, 2015 at 13:32
  • 2
    ... continuing. The block you want to add in two places in the page just renders a static block. Now, let's say you put the static block rendering logic in the template, then use layout updates to place the block in the two sections of the page. So far so good, both methods work OK. Now, let's say that in one of the sections of the page, you need to have slightly different markup, for some reason, maybe different css classes, maybe different HTML tags etc. See where I'm going? Now you'll have to change the template, but duplicate the static block rendering code. TBC...
    – Adi
    Commented Sep 29, 2015 at 13:41
  • 2
    ... continuing. Now, change the static block's ID. You'll have to do the change in two places; this violates the DRY principle. The same thing applies when you have multiple themes, maybe one per store view. You'd have to duplicate non-rendering related code between multiple templates. You wouldn't have this problem if that logic would live in a block class.
    – Adi
    Commented Sep 29, 2015 at 13:47
2

I found both methods are correct and you can explore more by looking at these posts: Add static cms block in home page

Also look at this too: Call static block into another static block through template

I hope your all doubts will be cleard

Your Answer

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

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