7/25/2015 - 9:52 PM

Block/Static Block in magento

Block/Static Block in magento

 Block/Static Block in magento

>> Following is the code by which you can get a Static Block content :
<?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("identifier")->toHtml() ?>


$block = Mage::getModel('cms/block')->load('identifier');
echo $block->getTitle();
echo $block->getContent();

>> You can also get it from CMS page: Admin=>CMS=>Pages=>Content
{{block type="cms/block" block_id="my_block" template="cms/content.phtml"}}


>> Create a block programmatically :
      <?php echo $this->getLayout()->createBlock('myModule/block')->setTemplate('folder/custom.phtml')->toHtml();?>

       Note:* If you want to access a method of an existing block. You can do it like:
       $blockObject = $this->getLayout()->createBlock('existingblock/name');
       Now call any method of that block using the $blockObject->yourMethod()

>> Set new template to an existing Block :

     <?php echo $this->getLayout()->createBlock('newsletter/subscribe')->setTemplate('newsletter/subscribe.phtml')->toHtml();?>

>> You can also Pass a variable through block :

      1. Through Admin CMS page. 
      {{block type="core/template" category_id="10" template="page/custom.phtml"}}

      2. In the XML file.
      <block type="core/template" name="" template="catalog/category/visitourstore/subcatlist.phtml" >
               <action method="setData"><name>category_id</name><value>10</value></action>

     3. In The phtml page :
     <?php echo $this->getLayout()->createBlock('core/template')->setData('category_id',10)->setTemplate('page/custom.phtml')->toHtml();?>

   Now you can easily get the value as: <?php echo $this->getCategoryId();  ?>