Snippets

PTML templates

Rendering without layout XML

Render CMS block:

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

Render PHTML template:

<?php
// 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'); ?>
</a>

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'); ?>
</a>

Module detection

Check if a module is enabled:

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

Get all layout handles:

// return array
var_dump(Mage::app()->getLayout()->getUpdate()->getHandles());

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
var_dump(Mage::app()->getRequest()->getModuleName());

Via StackExchange: Determine the rendering module

Security

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')); ?>">

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

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();
    self::_setIsInstalled($options);
    self::_setConfigModel($options);

    // 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');

    self::$_app->run(array(
        '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:

/maintenance.flag

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;
    //}

    //$session->clear();
    //-----
}

Via Alan Storm: Working on Magento’s success page