Adding a static block to a Magento template

Sometimes, you may need to add an editable block to your template to allow content to be easily updated through Magento's administration panel. Magento's static blocks allow you to do this, and they can be embedded in the Magento templates.

Creating a new static block

Firstly, you will need to create a static block in Magento. Log in to your store's administration panel and navigate to CMS | Static Blocks, as shown in the following screenshot:

Creating a new static block

Here, click on the Add New Block button at the top-right of your screen, as shown in the following screenshot:

Creating a new static block

You can create your block here: the block Title field allows you to give your block a name, while the Identifier field is a machine-readable way to identify this specific block (remember this value, as you'll need it soon!).

Tip

Note that the value of the Identifier field cannot contain spaces or special characters, and it's typical to use an underscore character (_) here to separate words in the identifier's name.

The Status field allows you to enable or disable this specific block: ensure this is set to Enabled to be able to make use of the block in your template. Finally, the Content field allows you to specify content for this block; you can either make use of the Magento text editor tool here, or disable it and enter raw HTML. The following screenshot shows an example block:

Creating a new static block

Once you're ready, click on the Save Block button towards the top-right corner of your screen.

Inserting the static block into a template

Now that you have a static block ready, you can include it in a template within your Magento theme. The example static block created previously is for use in the footer of the website to give customers an idea what the store is about.

Before you do this, you will need to copy the footer.phtml file from the /app/design/frontend/base/default/template/page/html/ directory to the /app/design/frontend/default/m18/page/html/ directory and locate the following lines:

<div class="footer-container">
<div class="footer">

Below these lines, insert the following snippet to insert the static block you created into the page at this point:

<div class="footer-container">
<div class="footer">
<div class="footer-about footer-col">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('footer_about')->toHtml(); ?>
</div>

In the section that reads setBlockId('footer_about'), note that the footer_about value is the identifier value of the block you created earlier. In the preceding code, the echo $this->getLayout()->createBlock('cms/block')->setBlockId('footer_about')->toHtml() code tells Magento to insert the contents of the static block into Magento with the identifier footer_about.

If you refresh your Magento theme, you will see the new block's content appear in the footer area of your store:

Inserting the static block into a template
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247