Commonly used PHP Functions in Magento

Magento is known as a robust platform and hence, a bit difficult to control CMS. This platform is built on the Zend framework, and the tasks to edit or create Magento templates usually become difficult for one. This article brings a list of several effective functions that are more often used in Magento template development while theme development phase. This is a brief list in which we have only covered few greatly useful functions. So, let’s start reading on the details of these functions right here.

Effective XML Functions In Magento Development

If we talk about a unique features or function of Magento platform, it would surely be the layout feature. It is basically an additional layer that does not exist in several other CMS platforms. With XML tags, the layouts of Magento are being described. For the best practice, the XML custom code is often included to the local.xml file located in layout folder. This file loads at last and all the features written to this file override rest of the functions.

Inclusion Of CSS/JS Files For Magento Development

By utilising XML code, you can include various JS and CSS files to the Magento blocks. The biggest advantage of this practice is that it helps to determine JS and CSS files gets loaded only to the layouts which require them. This is how one avoids unrequired CSS and JS file loading which ultimately minimises the time for loading page.

For including the CSS file to any block of certain layout, use code as mentioned below:

<reference name=”head”>
<action method=”addItem”>
<params />

In this code, the name of the reference combines the block name to which you will add the JS/CSS file. To this instance, we are trying to include the CSS file, hence the parameter type we used skin_css; however when there is a need to include JS file, the parameter type will change to skin_js. We have purposely left the tag empty here.

Removing Or Adding Blocks

We have mentioned a code here that can be used to unset the child block from a parent block. After that, you can easily assign that unset block to any other parent block. For example unset a poll (child) block right from the old parent block that is located at the sidebar and then, assigns the poll block to new parent block anywhere.

<!– Removing Block from old parent –>
<reference name=”old_parent”>
<action method=”unsetChild”>
<!– Now adding it to new parent –>
<action method=”insert”>

In case, there is a need of the block removal, it would require only the first segment of the code mentioned above. Just as if there is a need of assigning any new block to an existing block, utilise the second segment of this code.

Just remember that CMS block is specifically very handy while the creation of templates. It is because a novice or a person with less technical knowledge a power to conveniently include content to the block right from admin panel. By the use of the following code, you can easily insert the CMS blocks to any desired layout block.

<!– Inserting CMS block –>
<block type=”cms/block” name=”relevant_name”>
<action method=”setBlockId”>

Making Changes In Block/Page Templates

Another greatly used and critical function that is regularly utilized in XML file is to assign templates to various layout blocks. This function can be performed conveniently by following the template code mentioned below:

<!– Insert template file –>
<block type=”cms/template” name=”relevant_name” template=”page/html/template_file.phtml”/

In a case of already assigned templates you have and you need to make changes to it, use the code as stated below:

<!– Change page template –>
<reference name=”root”>
<action method=”setTemplate”>

So, these are few the most useful functions in Magento development that are used frequently. Though here you must remember that Magento is a big CMS platform that comprises of numerous functions which we cannot mention in one post. However, this list contains the functions that are very effective and used extensively in template creation for Magento development. By properly analysing these functions, you can surely get an understanding of the way these functions affect Magento. In the end, we would suggest you study PHTML and XML files on the base theme of Magento as it is the only way to be an expert in using Magento functions.


by Lalita Rawaton July 21st 2017