Adding a Static Block to your .phtml files in Magento

So I scoured the interwebs looking for this answer and finally pieced it together…so I’m saving it here for my own posterity. I found lots of results for embedding the blocks in other CMS pages but if you are working in the .phtml and .xml files then you’d have some reading to do just to get it to work.

Step 1:
Create your static block in the admin area of Magento. (CMS > Static Blocks) Click on ‘+ Add New Block’. The ‘Identifier’ is what you’ll need to take note of for embedding the block.

Step 2:
Navigate your way to the layout folder. (app > design > frontend > default > your_theme > layout) Open the file that references the page you intend to put the block into. In my case ‘page.xml’.

Step 3:
Find the spot in the .xml where you would like your block to appear and insert the following code where xxxxxx is the ‘Identifier’ you set earlier when creating your block.

xxxxxx

Step 4:
Navigate your way to the template folder (app > design > frontend > default > your_theme > template) Open the file that you would like the block to appear in and insert the following code in the appropriate position where xxxxxx is the ‘Identifier’ you set earlier when creating your block.

echo $this->getChildHtml('xxxxxx');

Finale:
That should do it. If not, I’m sure something is wrong and I suggest you keep scouring the internet and let me know what you find. Of course if there are any questions I’ll do all I can to answer them. Not sure how much help I can be since I struggled with this so much (hence the suggestion to continue scouring) but I’ll try.

Additional Information

From: blogswindow

echo $this->getLayout()->createBlock('cms/block')->setBlockId('my-new-block')->toHtml();

Share

Comments