So basically your page.xml creates Data Blocks, your .phtml Outputs that data where you want it. Hence, the names for left, right and so forth in your .phtml.
Even further, Your modules will update the available “block containers” with more specific content. For example, you’ll notice once you navigate past the front page and click a category the “Compare Box” appears. Once you click down further “The Layered Navigation” appears.
page.xml manages the layout and content blocks for all pages in your store, so all the updates you need to make to a page layout can be done from the page.xml file.
You're aware now that page.xml contains a handle called "
catalog/navigation/top.phtml
catalog/product/compare/sidebar.phtml
If you read the code and think about whats going on it makes sense.
it’s telling Magento to insert the following block into the RIGHT column when you get into “Catalog” view. (Remember, its located at layout/catalog/) so it appears once you’ve entered the catalog section of the shop. It also defines a TEMPLATE for the new block to use, so for the example above the compare box has its own template located at catalog/product/compare/sidebar.phtml (in your template folder).
==== Reference name values/attributes: ====
As we’ve seen, the references can use different attributes for displaying blocks on our page. Possible values are:
* **root** - we will change it mostly to set up another .phtml file as a root template ex. (1column.phtml , 2columns-left.phtml ,2columns-right.phtml etc.)
* **head** - as this reffers to our section on page, we will use this reference name to reflect our changes in this section
* **top.menu** - defines our content for top menu section
* **left** - defines our content for left column
* **right** - as above but for right column
* **content** - defines blocks placed in main content of our page
* **before_body_end** - is used to add a block before end of our page so before
There are more reference names that we could use but they are more page-specific than for global use for example:
* customer_account_navigation
* customer_account_dashboard
are used on our clients account page
product.info.additional - sets up additional block for placing related items, shipping estimator etc.
==== Action methods: ====
Action methods allow us to change many theme settings without appending manual changes to our .phtml files. The method listed in the method attribute refers to a method in the Model that is associated with the particular block in question. The most important methods are described below.
**setTemplate**
Action method setTemplate allows us to change the default .phtml file used in particular block.
For example by navigating to app/design/frontend/default/default/layout/catalog/product/view.xml we can see the reference:
page/2columns-right.phtml
and by using another value we are allowed to change default .phtml file used on our products page. Possible values are:
1column.phtml
2columns-left.phtml
2columns-right.phtml
3columns.phtml
one-column.phtml
dashboard.phtml
As we see in **app/design/frontend/default/default/layout/checkout/cart.xml** , there also additional 2 values for empty and non-empty cart
checkout/cart.phtml
checkout/cart/noItems.phtml
The method **chooseTemplate** is used to set a template (setCartTemplate / setEmptyTemplate) depending on quantity of items in our cart. If we have more than 0 than the
checkout/cart.phtml
is used. If we have no items in cart then the following will be used.
checkout/cart/noItems.phtml
The function provided by the Model is shown below:
public function chooseTemplate()
{
if ($this->getQuote()->hasItems()) {
$this->setTemplate($this->getCartTemplate());
} else {
$this->setTemplate($this->getEmptyTemplate());
}
}
That should clarify how we can use this particular switch. Depending on our needs we can write custom functions in our blocks and than assign a template depending on parameters returned by a function.
**addCss**
This method allows us to add an additional CSS file to our page on per-page basis or globally for our template. If we use a reference name “head” and action method addCss by using
style.css
then our page will have an additional line of code to attach the CSS file, for example:
As we can see, the path refers to the /skin/frontend/default/default/css/ folder.
**addCssIe**
This method is very similar to the above but it will output a css file when a User-Agent (browser) is Internet Explorer or Maxthon. So using this method we can attach a specific css file for those browsers. This is very helpful if our page will requires changes in css dependant on a specific browser.
Usage:
style.css
Output in page source:
**addJs**
The above method allows us to attach a .js script in the same way as we attached a CSS file. The script path refers to the /js/varien/ folder but we can change it to suit our needs. FIXME How is that done?
Usage:
varien/script.js
It will add a script to our page with src attribute of
**addJsIe** - adding a .js file to head section of page and using it if User Agent (browser) is Internet Explorer.
If we can add different css files depending on User-Agent we can do the same with our .js files. Than we can use different scripts for our IE/Maxthone users and another for other browsers.
Usage:
our/script.js
It will add a script to our page with src attribute of
but also inside IE comments
**setContentType**
This method can override default headers sent by our page to the browser. So we can set a text/xml instead of text/html (or another as we wish).
Usage:
text/xml
**setCharset**
allows us to override default page encoding on per-page basis or globally. As long as we are saving our files with proper encoding this will not be necessary.
Usage:
ISO-8859-2
So in this case our page will have Central European encoding instead of default UTF-8
**addLink**
addLink methods can be used to set a setting to which we can refer in our .phtml template files also without manually changing the .phtml files.
Example usage :
By adding a block which was found in app/design/frontend/default/default/layout/customer/account.xml into our
customer/account/navigation.phtml
account customer/account/
address_book customer/address/
account_edit customer/account/edit/
The cart.xml file should look like below
page/1column.phtml
customer/account/navigation.phtml
account customer/account/
address_book customer/address/
account_edit customer/account/edit/ {{baseSecureUrl}}
checkout/cart.phtml
checkout/cart/noItems.phtml
checkout/cart/coupon.phtml
checkout/cart/shipping.phtml
checkout/cart/crosssell.phtml
Of course according to the previous references we can also change
page/1column.phtml
in the above code to suit our needs. I’ve used for example
page/one-column.phtml
to show only our cart without other blocks.
==== Layout blocks ====
As we’ve seen before, most of our xml files have a
catalog/product/view/super/config.phtml
type="catalog/product_view_super_config" - defines the type of our block on page. This example would refer to the file /app/code/core/Mage/Catalog/Block/Product/View/Super/Config.php which defines the class Mage_Catalog_Block_Product_View_Super_Config
name="product.info.config" - defines a name for our block and should be unique
as="super_config" - defines a shortname for our block which can we use with getChild function on particular page
Blocks used in our XML files can change or override most every aspect of our design. We can use them to simply change used phtml files on per-page basis, to add additional scripts, stylesheets to our page , to move particular sections of page without needing to change phtml files.
===== Understanding .phtml files =====
==== Introduction ====
Phtml files are template files that handle the output to browser. They are nothing more than html files with nested php tags. We use them to style our page and the look of our site.
Changing .phtml files requires basic knowledge about XHTML, CSS and understanding how to use PHP functions on a page.
Let’s have look at header.phtml placed in templates/page/html.
?>)
=__('Skip to main content')?> »
=$this->getWelcome()?> =$this->getChildHtml('topLeftLinks')?>
=$this->getChildHtml('topRightLinks')?>
=$this->getChildHtml('topMenu')?>
In this file we see basic XHTML tags, usage of CSS classes but most important - Magento functions used to get layout XML blocks and render it in our phtml file.
Mostly in our template we’ll see ?> tags used for functions calls. Basing on above example:
**=$this->getUrl('')?>** - used without parameters will return base path of our store
**=$this->getLogoSrc()?>** - will render a logo image based on path used in etc/config.xml
but this resolution isn’t recommended since we should use core functions and learn their usage.
**=$this->getLogoAlt()?>** - this function will allow us to change the alt tag for our logo so if it will be unavailable , the alt tag will appear. Any changes we can also append by changing
As we see, getChildHtml(’topLeftLinks’) uses its alias “as” and calls it from the XML. The getChildHtml() function only allows Magento to call a block if that block was defined in the corresponding XML file.
We can also override this mechanism by using another function call:
**=$this->getLayout()->getBlock('top.left.links')->toHtml()?>**
This structure will call the top.search block (based on its name, not its alias “as") from anywhere in any of our templates so we do not need to define it everywhere in our XML files. Remember to use the “name” attribute instead of the “as” attribute with this workaround.
We must be aware that every phtml file and every function will always refer to the corresponding XML file or files. We can identify used phtml files simply by searching for the following:
wishlist/sidebar.phtml
which tells us which phtml file will be used.
==== Folder structure ====
Every View used in our application has separate folders and subfolders to store template files. Let’s have a look at the folder structure:
* **callouts** - folder where are files for our callouts and ads are placed
* **catalog** - folder to store files used on our category, layered navigation, product, comparision pages
* **catalogsearch** - here we find files that are used to skin our search engine and result pages
* **checkout** - all the pages utilised during checkout and shopping in our shop. Here we’ll also find the shopping cart templates and blocks for cross-selling and coupons.
* **cms** - folder for static pages templates.
* **core** - folder containing store-switching templates (not yet active)
* **customer** - all customer pages (ex. account dashboard, address forms, orders list and reviews)
* **datafeed** - folder to store our csv, txt, xml files, used for comparision engines and other external applications
* **directory** - here we find currency switcher for our shop
* **email** - here we’ll find all pages that require email transport so for example order, password recovery, newsletter signup
* **install** - template files for Magento’s installer
* **newsletter** - subscribe.phtml placed in this folder will allow us to change the look of newsletter signup box on our page
* **page** - the most important folder in which we’ll find all main files used to style our site. More about it will be described in following subchapter.
* **payment** - templates used to style our payment forms (ex. CC payment.)
* **poll** - 2 files to change the look of our poll depending on state (didn’t vote yet / show result)
* **rating** - rating block used on our products pages
* **review** - all files used to render the blocks used by reviews
* **sales** - pages for order details, invoices, recent orders
* **searchlucene** - result output for Zend_Lucene controller used in Magento
* **tag** - product tags templates are stored here
* **wishlist** - template files to handle the output of our wishlist actions.
==== Appending basic changes to our templates ====
Every file used to skin the basic look of our template is placed in template/page folder. Here we’ll see
1column.phtml
2columns-left.phtml
2columns-right.phtml
3columns.phtml
one-column.phtml
dashboard.phtml
These are essentially the HTML skeleton files for our pages. By changing those files we can set up a new look of the page structure.
There is also an html subfolder placed under template/page in which we can change footer, header and links blocks of our template.
Every one of them uses simple function calls (ex. getChildHtml() ) so we can also identify the block by searching in layout XML files .
For example when we’ll see
page/2columns-right.phtml
this tells us that the page will use 2columns-right.phtml as a skeleton for our page.
==== Calling layout blocks using Magento functions ====
As it was described above, there are two ways of calling blocks .
**=$this->getChildHtml('as')?>** - by using the block alias “as” from the XML file we can display a block on our page providing that it was defined already in the corresponding XML file
**=$this->getLayout()->getBlock('name')->toHtml()?>** - by using the block name from the XML file, we can call any block whether or not it was already defined in corresponding XML file
==== e. Adding custom CSS and JS files to a layout (part II) ====
There are 2 ways of adding custom js and css files to our template. The recommended way is by extending the head section in the default XML file. But you also have the ability to add the files directly in the particular root template file.
As we see here , we can also use getJsUrl method which adds a scripts path to our src attribute. This should output http://yoursite.com/js/ so the source will look like the following:
Adding a css file isn’t harder than adding a js file. We do this also by using a function that outputs a path to our skins folder.
And the output will be
so getSkinUrl() sets actual path to our skins folder : http://yoursite.com/skin/frontend/default/default/
==== Opening .phtml files in Dreamweaver ====
By default, Dreamweaver cannot read PHTML files. You can add the file type to the “Open in Code View” section of the preferences if you wish to have fast access, however you cannot view the file in design view if you do that. So if you use Dreamweaver (versions 4, MX, MX2004, 8, or 9, aka CS3) to design your sites, and you wish to open Magento’s Template files (they have .phtml extensions) in Dreamweaver, you can follow these steps to add support for .phtml and make Dreamweaver render PHP code (with coloring, hinting, et al) as well as allow you to see the design in code view if desired. Below are three steps to follow.
Restart or Open Dreamweaver and you shouldn’t have any problems with PHTML files any longer.