4

I have a code of left side category list in left_cat.phtml file. The code is like below.

<div class="block block-left-category">
  <div class="block-title">
        <strong><span><?php echo $this->__('Categories') ?></span></strong>
    </div>
    <div class="block-content">
        <div class="categories-top-list no-dots">
                <a class="category-top" href="#">APPLE</a>
        </div>
        <div class="categories-top-list">
                <a class="category-top" href="#">AUDIO</a>
        </div>
        <div class="categories-top-list">
                <a class="category-top" href="#">AV</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">CAMERAS</a>
        </div>
        <div class="categories-top-list">   
                <a class="category-top" href="#">CCTV</a>
        </div>
        <div class="categories-top-list">
                <a class="category-top" href="#">COMPONENTS</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">MONITORS & TV's</a>
        </div>
        <div class="categories-top-list">   
                <a class="category-top" href="#">NAS Solutions</a>
        </div>
        <div class="categories-top-list">
                <a class="category-top" href="#">PROJECTORS</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">WIRELESS</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">LAPTOPS & NOTEBOOKS</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">NETWORKING</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">PC'S</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">PERIPHERALS</a>
        </div>
        <div class="categories-top-list">       
                <a class="category-top" href="#">PRINTERS & SCANNERS</a>
        </div>
        <div class="categories-top-list">   
                <a class="category-top" href="#">SOFTWARE</a>
        </div>
        <div class="categories-top-list">   
                <a class="category-top" href="#">CLEARANCE</a>
        </div>      
        </div>
    </div>

and in XML file I've put the code like below.

<default>
        <!-- Mage_Catalog -->
        <reference name="left">
            <block type="core/template" name="left.category.list" template="category/left_cat.phtml"/>
        </reference>
</default>

Temporary I put the category list in the PHTML file. But, now I want to add category list in one of the static block and want to fetch that block in the XML file at particular location. I've tried to find solution using many resources, but hard luck.

6 Answers 6

18

As described on Stackoverflow

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

Below I have the example of add newest products at left side.

<default>
    <reference name="left">
        <block type="cms/block" name="cms_newest_product" as="cms_newest_product" output="toHtml">
            <action method="setBlockId"><block_id>newest_product</block_id></action>
        </block>
    </reference>
</default>
1

Adding a Static Block to your .phtml files in Magento

step 1: Go to Magento admin uder CMS -> Static Blocks and Add New Block. Insert Block Title, Identifier (for example: checkout_logo), set Status to Enabled, insert block Content and Save Block.

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

Use this code to call static block in phtml file. For this From Backend CMS -> Static Blocks and create new block. Give identifier to static block and pass identifier in following code.

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); ?>
1
0
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('static block Identifier')->toHtml(); ?>
-2

Its better and safer using {{ }}

For example

Create Block and pass all the data in that block, then call the block as if you are calling a widget.

i.e {{widget type="cms/widget_block" template="cms/widget/static_block/default.phtml" block_id="53"}}

where block_id is the id of the static block you've created

3
  • pretty sure that won't work in a .phtml Jan 27, 2015 at 10:39
  • i'v used this many times on some projects Aug 13, 2015 at 16:50
  • It would only work in CMS!
    – Shadoweb
    Nov 17, 2015 at 9:13

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