Friday, 30 August 2013

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.

Eg:
 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.

Eg: 
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


Go to Magento admin uder CMS -> Static Blocks and Add New Block. Insert Block Title, Identifier (for example: my-new-block), set Status to Enabled, insert block Content and Save Block.
If you want to put your static block in left column open file /app/design/frontend/your_theme/your_theme/template/callouts/left_col.phtml and insert code below where you want the static block to apear:

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

You can put this code wherever you want the block to apear, for exapmle right_col.phtml, 3columns.phtml, … Sky is your limit …

No comments:

Post a Comment