MageBridge template helper
Integrating a Magento theme into Joomla! is not as straight forward as it seems. Magento blocks are only shown on certain MageBridge pages, while Joomla! does not know how to handle empty bodies of Joomla! modules. Also, assigning a Joomla! module to certain MageBridge pages is impossible because the Joomla! Menu Assignment is just not flexible enough.
The best solution for all these problems is to add PHP-code to the template. But because Joomla! still does not know about the Magento theming logic, the MageBridge template helper-class MageBridgeTemplateHelper allows you to configure things while using fairly simple PHP-code.
Adding PHP-code to your Joomla! template
Knowledge of HTML and CSS is nice to change the visual appearance of a Joomla! template, but as soon as you are dealing with logical decisions like hiding certain columns on certain pages, PHP-knowledge is needed. Guides for Joomla! templating are full with examples like the following:
<?php if($this->countModules('left')) : ?>
<div id="left">
<jdoc:include type="module" name="left" style="xhtml" />
</div>
<?php endif; ?>
This checks if the module-position "left" contains any module-instances, because if there are no modules to be shown, the left column need not to appear.
We can extend this example by saying that the left-column should only appear if there are modules available on the position "left", but also if the current page is not the homepage. This practically hides the left-column on the homepage:
<?php
$menu = JSite::getMenu();
$active = $menu->getActive();
$homepage = (boolean)$active->home;
?>
...
<?php if( $homepage == false && $this->countModules('left') ) { ?>
<div id="left">
<jdoc:include type="module" name="left" style="xhtml" />
</div>
<?php } ?>
As you can see: The more logic you want in your template, the more complex the PHP-code becomes.
Adding Magento logic to your template
The same applies with MageBridge: When you add the Magento theme to Joomla!, two theming mechanisms are integrated. But if you want the integration to become seamless, you either need to extend Magento with Joomla! templating logic, or add Magento theming logic to Joomla!. We have chosen for the last option.
The PHP-class MageBridgeTemplateHelper (which can be found in the folder components/com_magebridge/helpers/template.php) helps you with simple tasks, like determining if the current page is the Magento homepage or not:
<?php if( MageBridgeTemplateHelper::isHomePage() == true ) { ?>
...
<?php } ?>
Because the Magento theming engine also adds different page-layouts, these page-layouts might also be needed to determine whether a column in Joomla! should be shown or not:
<?php if( MageBridgeTemplateHelper::hasLeftColumn() && $this->countModules('left')) { ?>
...
<?php } ?>
As you can see, with this new logic the Joomla! templating engine is enhanced tremendously by adding extra MageBridge logic. However, if things are more flexible, they are also more complex: This is for the more advanced webdesigners.
MageBridgeTemplateHelper methods
While the PHP-code of the MageBridgeTemplateHelper class is simple and straight-forward (components/com_magebridge/helpers/template.php), here is an overview of the current methods anyway:
| Method | Argument | Return | Description |
|---|---|---|---|
| hasCss | true|false | Determine if the bridge is loaded with some CSS-stylesheets | |
| hasJs | true|false | Determine if the bridge is loaded with some JavaScript-scripts | |
| hasPrototypeJs | true|false | Determine if the bridge is loaded with Prototype JavaScript | |
| isLoaded | true|false | Sees whether either the MageBridge component or a MageBridge module is loaded | |
| hasModule | string | true|false | Checks whether a specific module (example: mod_magebridge_block) appears on the page |
| countModules | true|false | Copy of the original method, but now skipping modules that have empty content | |
| allowPosition | string | true|false | See whether a certain module-position (left, right) is flushed in the MageBridge configuration or not |
| getRootTemplate | true|false | Get the current page layout | |
| getPageLayout | string | Alternative for getRootTeplate | |
| hasLeftColumn | true|false | Determine if the Magento theme is using the left-column layout | |
| hasRightColumn | true|false | Determine if the Magento theme is using the right-column layout | |
| hasAllColumns | true|false | Determine if the Magento layout uses all three columns (main component plus left plus right) | |
| hasTwoColumns | true|false | Determine if the Magento layout uses two columns (main component plus one side-column) | |
| hasOneColumn | true|false | Determine if the Magento layout uses only one column (main component) | |
| getRequest | string | Get the current Magento page-request | |
| isHomePage | true|false | Determine if the current request is the homepage | |
| isPage | string | true|false | Determine if the current request is a specific page |
| isCatalogPage | true|false | Determine if the current request is a catalog page | |
| isProductPage | true|false | Determine if the current request is a catalog product page | |
| isCategoryPage | true|false | Determine if the current request is a catalog category page | |
| isCustomerPage | true|false | Determine if the current request is a customer page | |
| isCheckoutPage | true|false | Determine if the current request is a checkout page | |
| isCartPage | true|false | Determine if the current request is the shopping cart | |
| getProductId | true|false | Get the currently active product-ID from Magento | |
| getCategoryId | true|false | Get the currently active category-ID from Magento |
Skipping empty modules
Within a Joomla! template you can check with the code $this->countModules('left') if there are any modules available on the left. If you put some MageBridge modules (containing Magento blocks) on the module-position "left", this works without a problem. But if the Magento blocks are empty, you actually want to hide the modules. The first step into dealing with this, is to setup the right module-chrome (see MageBridge and module chromes).
But if all the modules on the position "left" are empty, you actually want to hide the entire left-column. Unfortunately, the Joomla! "countModules" method is not smart enough to detect empty modules (though it is a feature which has been discussed for Joomla! 1.6). The MageBridgeTemplateHelper-class offers an alternative which allows you to hide the left-column if it is empty.
<?php if( MageBridgeTemplateHelper::countModules('left')) { ?>
...
<?php } ?>
Checking for any Magento page
While the MageBridgeTemplateHelper class offers various shortcuts for determining which page you are on, the isPage method allows you to determine any page. The benefit of using this method, instead of using an uglier method like $_SERVER['REQUEST_URI'], is that the Magento SEO URLs are translated back to their original non-SEF URL.
To determine if the current page is the wishlist page:
<?php if( MageBridgeTemplateHelper::isPage('wishlist/*')) { ?>
...
<?php } ?>
The argument wishlist/* is handed over to the isPage method and is used as a simple regular expression. If you wanted to check for a single product page, the argument would be catalog/product/view/*.
Instead of working with the abstract MageBridgeTemplateHelper::X() methods, you can also instantiate the helper and use a shorter variable-name instead:/p>
<?php
$mb = new MageBridgeTemplateHelper();
?>
...
<?php if( $mb->isPage('wishlist/*')) { ?>
...
<?php } ?>
Created on Thursday, 03 December 2009Modified on Monday, 07 March 2011
More tutorials in this section
- MageBridge FAQ: Theming
- MageBridge template overrides
- Switch between Magento theme and Joomla! template
- Adding custom Magento scripts to the Joomla! page
- Applying XML Layout Updates in MageBridge - (2) CMS Page
- Applying XML Layout Updates in MageBridge - (1) Overview
- MageBridge template helper
- MageBridge combined theming
- MageBridge Design Guide
- Template override of fixes.php
- Creating template overrides to support MageBridge
- MageBridge and module chromes
- Theming-options in MageBridge
- Using Lightbox in MageBridge
- Using JoomlArt patches for MageBridge
- Joomla! module-positions in Magento PHTML
- Using the Magento "magebridge" theme
- Adding the Magento header to the Joomla! template
- Hiding and showing module-positions with MageBridge
