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 endif; ?>

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 endif; ?>

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 endif; ?>

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:

MethodArgumentReturnDescription
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
removeMagentoScripts   null Try to remove all Magento scripts from the current page
addMagentoStylesheet * null Add a Magento stylesheet to the page
isLoaded   true|false Sees whether either the MageBridge component or a MageBridge module is loaded
isMobile   true|false Determine whether Joomla! detects the current browser as a mobile browser
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
getHandles   array Get an array of all Magento XML-handles
hasHandle string true|false Check for a specific Magento XML-handle
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
getStore   string Get the current Magento store-code
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
isSalesPage   true|false Determine if the current request is a sales-page
isWishlistPage   true|false Determine if the current request is a wishlist-page
getProductId   true|false Get the currently active product-ID from Magento
getCategoryId   integer Get the currently active category-ID from Magento
isCategoryId integer true|false Check whether current category-ID matches (including subcategories)

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 endif; ?>

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 endif; ?>

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/*.

Checking for a specific Magento category

When a Magento category-page is loaded, or when a Magento product-page is loaded, Magento will mark a specific Magento category as current. MageBridge allows you to reuse the ID of this Magento category in your Joomla! template. The following segment checks for the category-ID 32:

<?php if(MageMageBridgeTemplateHelper::getCategoryId() == 32): ?>
...
<?php endif; ?> 

Checking for multiple categories at once, can be done by using normal PHP-logic - for instance by using an array like this:

<?php if(in_array(MageBridgeTemplateHelper::getCategoryId(), array(32, 45, 47))) : ?>
...
<?php endif; ?> 

Notes

In previous versions of the MageBridgeTemplateHelper class it was possible to instantiate an object from the class. With current versions, this is no longer possible. All methods should be called statically as documented above.