PTML templates

Rendering without layout XML

Render CMS block:

// GravDept:
// Get snippet.
$snippet = $this->getLayout()->createBlock('cms/block')->setBlockId('the_block_name')->toHtml();
echo ($snippet) ? '<div class="std">' . $snippet . '</div>' : '';

Render PHTML template:

// Example
echo $this->getLayout()->createBlock('core/template')->setTemplate('path/to/file.phtml')->toHtml();

// Example with passing variable
echo $this->getLayout()->createBlock('core/template')->setBananaId($bananaId)->setTemplate('path/to/file.phtml')->toHtml();

Get configuration values

Get store email:

<a href="mailto:<?php echo Mage::getStoreConfig('trans_email/ident_support/email'); ?>">
    <?php echo Mage::getStoreConfig('trans_email/ident_support/email'); ?>

Get store phone:

<a href="tel:+1<?php echo preg_replace('/[^0-9]/', '', Mage::getStoreConfig('general/store_information/phone')); ?>">
    <?php echo Mage::getStoreConfig('general/store_information/phone'); ?>

Module detection

Check if a module is enabled:

if (Mage::helper('core')->isModuleEnabled('MyNamespace_MyExtension')) {
    echo 'Enabled!';

Get all layout handles:

// return array

Match a specific layout handle:

if (in_array('catalog_product_view', $this->getLayout()->getUpdate()->getHandles())) {
    // Something

Get the module name (as in URL):

// return string

Via StackExchange: Determine the rendering module


Escaping data and translations in the frontend securely:

<a href="#"><?php echo $this->escapeHtml($product->getName()); ?></a>

<img src="#" alt="<?php echo Mage::helper('core')->quoteEscape($this->__('Gandalf')); ?>">

    alert('<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Fool of a Took!')); ?>');

Testing and debugging

Testing custom error pages

Force an exception for testing error pages:

// See: /app/Mage.php
// Uncomment the lines below.

public static function run($code = '', $type = 'store', $options = array())
    self::$_events = new Varien_Event_Collection();

    // Trigger 404 state
    // See: /errors/custom/404.phtml
    //throw new Mage_Core_Model_Store_Exception('');

    // Trigger 500 state
    // See: /errors/custom/report.phtml
    //throw new Exception('stop');

        'scope_code' => $code,
        'scope_type' => $type,
        'options'    => $options,

Via Alan Storm: Magento’s many 404 pages

Enable maintenance mode

Create an empty file in the Magento root:


The frontend will render /errors/custom/503.phtml for all requests. Delete the file to restore the site.

Testing checkout success

Working on the checkout success page:

// See: /app/code/core/Mage/Checkout/controllers/OnepageController.php
// Comment out the lines below.

public function successAction()
    //if (!$lastQuoteId || (!$lastOrderId && empty($lastRecurringProfiles))) {
    //    $this->_redirect('checkout/cart');
    //    return;


Via Alan Storm: Working on Magento’s success page