How to call Magento block in phtml template?
Asked Answered
A

5

10

i need to display some more links in footer. i created those links in magento admin as static blocks (id = sample_links ).

and then i added following code page.xml file

<reference name="foot_lnk">  
<block type="cms/block" name="sample_block" before="-">
      <action method="setBlockId"><block_id>sample_links</block_id></action>
    </block>
</reference>

i called this one in footer.phtml as,

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

but it does not display the CMS static block content. what is the issue?.

Asci answered 18/5, 2012 at 6:17 Comment(0)
O
14

The reference is the block previously defined that you want your block to be inside, e.g.:

<reference name="footer">
  <block type="cms/block" name="sample_links">
    <action method="setBlockId"><block_id>sample_links</block_id></action>
  </block>
</reference>

Then

<?php echo $this->getChildHtml('sample_links') ?>
Oklahoma answered 18/5, 2012 at 8:25 Comment(1)
its working thanks. already i have default footer links in <default> tag. i added block codes <block type="cms/block" name="sample_links"> <action method="setBlockId"><block_id>sample_links</block_id></action> </block> with in it.Asci
E
20
$this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() 
Eustashe answered 18/5, 2012 at 6:24 Comment(1)
yes i used above one it's working. but i need to display using XML like above.Asci
O
14

The reference is the block previously defined that you want your block to be inside, e.g.:

<reference name="footer">
  <block type="cms/block" name="sample_links">
    <action method="setBlockId"><block_id>sample_links</block_id></action>
  </block>
</reference>

Then

<?php echo $this->getChildHtml('sample_links') ?>
Oklahoma answered 18/5, 2012 at 8:25 Comment(1)
its working thanks. already i have default footer links in <default> tag. i added block codes <block type="cms/block" name="sample_links"> <action method="setBlockId"><block_id>sample_links</block_id></action> </block> with in it.Asci
O
8

You can call a statick block like:

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

And call a block like:

<?php echo $this->getLayout()->createBlock('sidebar/left')->setTemplate('bannerslider/left.phtml')->tohtml(); ?>

Visit magevn.com to see more usecase to use block in magento.

Oleoresin answered 7/8, 2015 at 13:9 Comment(1)
more complete answer, thanks for the added info, makes a google search much more complete :)Wernick
C
4

If you don't want to bother with XML, same as swapnesh's answer, I'm just making it clearer for the php noobs out there (like me)

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

your_identifier is the code you decide to use when creating your block in CMS > Blocks > Create New Block, second line called "Identifier"

Chevalier answered 22/1, 2015 at 16:34 Comment(1)
Well, you should have edited his answer to add precisions, then, instead of posting an almost identical one.Yellow
A
0

change your reference name to footer

like

<reference name="footer">  

then it will work.

Agglutinate answered 27/6, 2016 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.