Introduction to Block and Template in Magento
This blog is not the authors original, instead his notes on the key steps in playing with blocks and templates. Objective: To create a blo...
https://www.czetsuyatech.com/2021/07/magento-block-and-template.html
This blog is not the authors original, instead his notes on the key steps in playing with blocks and templates.
Objective: To create a block that will call a template and will be displayed on the home page.
Steps:
1.) Create a namespace inside the local directory, ex. Kalidad.
2.) Create another folder inside the Kalidad, ex Candles.
*this makes local/Kalidad/Candles.
*Candles is a module
3.) Inside the Candles there are a number of predefined subdirectory that can be created. We will create the minumum for this tutorial.
Let us create:
Kalidad/Candles/Block - module blocks
Kalidad/Candles/etc - where configuration files are stored
4. Then create the configuration file inside etc. Let's name it config.xml.
Tags that requires special attention:
a.) blocks should be place inside the global tag, or it will not be read
b.) specify the class, take note: Kalidad_Candles_Block - _ is replaced by / which correspond to its directory
5.) Create a file named Price.php inside Block, insert the below codes:
*This is our block, which we will call in our template to display a text.
6.) Define a template inside the selected theme: app/design/frontend/default/default/template/ (assuming we used the default template).
The folder will be name kalidad: app/design/frontend/default/default/template/kalidad
7.) Inside the kalidad template, create a file named price.phtml and insert the following code:
8.) Now, we need to create a configuration that will notify the magento of our newly created module.
Inside app/etc/modules, create Kalidad_Candles.xml <- code="" codes:="" ff="" insert="" is="" module.="" our="" the="" this="">
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Kalidad_Candles>
<version>0.1.0$lt;/version>
<active>true$lt;/active>
<depends>$lt;/depends>
<codePool>local$lt;/codePool>
</Kalidad_Candles>
</modules>
</config>
->
9.) For the changes to take effect clear the var/cache directory (delete everything - crucial), or inside magento;s admin System->Cache Management, disable all the items.
10.) If everything goes well, we should see our newly created module inside magento's System->Configuration->System.
11.) Change the home page. Goto magento's CMS->Manage Pages. Select Home Page and insert the following codes:
That's it.
Objective: To create a block that will call a template and will be displayed on the home page.
Steps:
1.) Create a namespace inside the local directory, ex. Kalidad.
2.) Create another folder inside the Kalidad, ex Candles.
*this makes local/Kalidad/Candles.
*Candles is a module
3.) Inside the Candles there are a number of predefined subdirectory that can be created. We will create the minumum for this tutorial.
Let us create:
Kalidad/Candles/Block - module blocks
Kalidad/Candles/etc - where configuration files are stored
4. Then create the configuration file inside etc. Let's name it config.xml.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Kalidad_Candles>
<version>0.1.0</version>
</Kalidad_Candles>
</modules>
<global>
<blocks>
<Kalidad_Candles>
<class>Kalidad_Candles_Block</class>
</Kalidad_Candles>
</blocks>
</global>
</config>
Tags that requires special attention:
a.) blocks should be place inside the global tag, or it will not be read
b.) specify the class, take note: Kalidad_Candles_Block - _ is replaced by / which correspond to its directory
5.) Create a file named Price.php inside Block, insert the below codes:
<?php
class Kalidad_Candles_Block_Price extends Mage_Core_Block_Template {
public function getPrice() {
$msg = "showPrice";
return $msg;
}
}
?>
*This is our block, which we will call in our template to display a text.
6.) Define a template inside the selected theme: app/design/frontend/default/default/template/ (assuming we used the default template).
The folder will be name kalidad: app/design/frontend/default/default/template/kalidad
7.) Inside the kalidad template, create a file named price.phtml and insert the following code:
<h1>Kalidad Price</h1>
<?php echo $this->getPrice(); ?>
8.) Now, we need to create a configuration that will notify the magento of our newly created module.
Inside app/etc/modules, create Kalidad_Candles.xml <- code="" codes:="" ff="" insert="" is="" module.="" our="" the="" this="">
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Kalidad_Candles>
<version>0.1.0$lt;/version>
<active>true$lt;/active>
<depends>$lt;/depends>
<codePool>local$lt;/codePool>
</Kalidad_Candles>
</modules>
</config>
->
9.) For the changes to take effect clear the var/cache directory (delete everything - crucial), or inside magento;s admin System->Cache Management, disable all the items.
10.) If everything goes well, we should see our newly created module inside magento's System->Configuration->System.
11.) Change the home page. Goto magento's CMS->Manage Pages. Select Home Page and insert the following codes:
{{block type="Kalidad_Candles/price" template="kalidad/price.phtml"}}
That's it.
6 comments
Hi nice post
Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long.
Thanks for the post.
I forgot to remove the name of the method from xml file (_Block_Method instead of _Block only).
Good post.
Thanks.
If I would like to include your block in my controller action, how would I do that ?
I have done a lot of search in order to find what i want!! great tuto and thank you!!
it is not worked for me
Post a Comment