Reading list Switch to dark mode

    Magento Block Series – Day One

    Updated 4 March 2013

    Magento Blocks are the soul of magento template system , Any magento template system if full of block elements .

    What is Magento Block 

    Blocks are basically combination of three entities

    ★ Class

    ★ Phtml Filles

    Searching for an experienced
    Magento Company ?
    Find out More

    ★ XML

    Here is the sample declaration for the block

    <block type="webkul/slideshow" name="webkulslideshow"

    As you can see in the above block code  here the class name is inside the type

    Type = Php class(Mage_Webkul_Block_Slideshow) where all the function are defined which are used in phtml file or view file inside webkul/slideshow.phtml

    name – is a unique identifies for this particular block

    <block type="Mage_Webkul_Block_Slideshow" 
    name="webkulslideshow" template="webkul/slideshow.phtml"/>

    template = file location for the view file

    Best thing with blocks are you can call those block inside cms pages or under static block of magento  . using  curly braces e.g

    {{block type=”webkul/slideshow” template=”webkul/slideshow”}}

    [stextbox id=”info”]In Magento2 we will use full name of the class instead of type e.g [/stextbox]

    <block type="Mage_Webkul_Block_Slideshow" 
    name="webkulslideshow" template="webkul/slideshow.phtml"/>

    Example : 

    suppose that i have a phtml file (magento view file new.phtml)  and i want to call this phtml file as a block  in magento default template .

    i have added following code on new.phtml at app/design/frontend/default/default/template/Sleekaccordian

    <h1 style="border:5px solid red;">WebKul Test Block</h1>

    now i am going to call this block using following block code from poll.xml ( location app/design/frontend/base/default/layout)

    and added the following code in poll.xml under <deafult>

    <reference name="left">
    	<block type="core/template" name="sleek" template ="Sleekaccordian/new.phtml" />

    And here is the output of the block

    Custom Block


    Dont worry about reference thing will explain in details in next series of the magento blocks .

    You can also enable template path hints and magento block hints from magento admin (under system->configuration->developer->advance->developer)

    Magento custom Blocks

     Play With Blocks

    You may change the location of the block . e.g suppose that you want to change the location of the block from left to right . so you need to change only reference code

    Custom Magento Block


    also suppose that if you want to change the order of the block e.g you want your block after the newsletter block , In this case magento provide before and after handlers  which we can call in our block code

    <reference name="left">
    <block type="core/template" after="left.newsletter" 
    name="sleek" template ="Sleekaccordian/new.phtml" /></reference>

    and here is the output

    block handler

    Another note about the type attribute, its not actually a directory/file structure, rather, it’s a URI that is mapped with the Magento autoloader. “Core” relates back to the Mage_Core_Block_Core class (under the app/code/core/Mage/Core directory) and then the info after the slash relates to the folders inside of that directory. So type=”core/template” resolves to this class Mage_Core_Block_Core_Template which is located at app/code/core/Mage/Core/Block/Template.php. All the type attribute is doing is telling Magento which methods you need to load inside of your block.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home