Categories
Magento 1

Caching

Retrieving the cache object

<?php Mage::app()->getCache();

Checking if a Cache Type is Enabled

<?php
Mage::app()->useCache($cache_type);

Saving to and Retrieving from Cache

Setting $lifetime to 0 or null means the value will be cached indefinitely, alternately enter a second value.

<?php
$cache->save($value, $key, $tags = array(), $lifeTime=null);
$cache->load($key);
$cache->remove($key);

Clearing a Cache using Tags

<?php $cache->clean($tags = array());

Clearing Cache from the Admin

To clear the custom caches from the admin, use the “Flush Cache Storage” button, as the “Flush Magento Cache” button only deals with Magento’s own cache. Obviously, this is if a custom cache type hasn’t been added to the admin using the method above.

For your custom cache type to appear in the admin, use the following XML to your module’s config file:

<global>
    <cache>
        <types>
             <your_cache_type module="your_module" translate="label description">
                <label>Your Custom Cache Label</label>
                <description>Description of cache type.</description>
                <tags>YOUR_CACHE_TAG</tags>
             </your_cache_type>
        </types>
    </cache>
</global>

Block Caching

To change the caching methods on a block, you can change the cache key to make the block cache for a specific thing. Eg, for a category key:

<?php $block->setCacheKey($category_key);

Or, in XML:

<block type="custom/block" name="custom.block">
    <action method="setCacheKey">
        <key>my_key</key>
        <!-- or to make it dynamic... -->
        <key helper="myhelper/getCacheKey" />
    </action>
</block>

Turning off Caching for a Block

$block->setCacheLifetime(null);

In XML:

<action method=”setCacheLifetime” />

Making a Block Cache Permenantly

<?php $block->setCacheLifetime(false);

Magento’s Topmenu

Very few blocks cache by default in Magento, however the Topmenu block is set up to (presumably because of the possibly heavy queries in generating them)

Caching should be set in a block’s constructor if it’s not being set in XML. E.g, The Mage_Page_Block_Html_Topmenu‘s constructor looks like the following:

<?php
public function _construct()
{
    $this->addData(array(
        'cache_lifetime' => false,
    ));
}

This behaves the same as $this->setCacheLifetime(false), meaning it will cache indefinitely. A problem with the top menu is that it caches regardless of whether the page is secure or not. If a secure page loads an unsecured cache then any links to elements will cause browser warnings; to get around this, we can do something like the following in the constructor which will create a separate cache of the block for http and https:

<?php
$this->addData('cache_key', Mage::app()->getStore()->isCurrentlySecure() ? "secure" : "unsecure");

**TODO: getCacheKeyInfo **

Categories
Magento 1

Email (& Static Block) Directives

Var

Used to pull out any variables which are passed in to the template via the ‘bind’ parameter. Methods can also be called on passed in variables on the email objects:

{{var subscriber.getConfirmationLink()}}
{{var passedInVariable.horse}}
<?php
$bind = array(
	'passedInVariable'  => array(
		'horse' => 'Moose’
	}
);

Block

Creates the block, and sets the parameters on them (or calls the methods). If type is not set as a parameter

{{block type='cms/block’ customVar='1’ methodToCall='test’}} 

Protocol

{{protocol}} - current protocol http or https
{{protocol url="www.domain.com/"}} domain URL with current protocol
{{protocol http="http://url" https="https://url"}

Config

Puts a system config setting into the email

{{config path="path/to/config"}}

CustomVar

{{customvar code="custom_var_code"}}

InlineCss

This is used in Magento’s new email styling method. Styles from the CSS file specified are “Emogrified” and placed into style tags on the corresponding elements.

{{inlinecss file="path/to/file.css"}}

Layout

Loads the layout handle specified and sets all variables on each of the blocks in the layout handle. Methods can also be called on the blocks in the layout handle using this method, if they exist. “area” can also be set as a parameter but will default to the current area if not. Most likely this will be ‘frontend’

{{layout handle="my_custom_layout_handle_to_be_used" variable="test" method="moose"}}

Skin

Embeds the url to a skin asset, _secure can be used if required.

{{skin url="path/to/asset.ext" _secure="true"}}

Media

Embeds a media URL, _secure can be used if required

{{media url="path/to/media/asset.ext" _secure="true"}}

Store

Get the URL for the store. If the url doesn’t end in a suffix, then ‘URL’ can be used, otherwise, direct_url should be used instead. Parameters can also be passed into the url using the syntax _query_paramName=”paramValue”

{{store url="contacts"}} // http://dave.magento.store/contacts/
{{store direct_url="html_file.html"}} // http://dave.magento.store/html_file.html
{{store direct_url="goosey.html" _query_param="horseparam"}} // http://dave.magento-test-2.iweb/goosey.html?param=horseparam

Depends

Allows the contents of the depends to be omitted if the clause doesn’t hold true.

{{depend order.getIsNotVirtual()}}
<!-- Statemement -->
{{/depend}}

If

Allows the contents of the if to be omitted if the clause doesn’t hold true. Like depends, but allows the use of an else clause

{{if order.getIsNotVirtual()}}
<!-- Statemement One -->
{{else}}
<!-- Statemement Two -->
{{/if}}

Template

Allows templates to be inserted via a config path. The config paths in the example are from System > Config > Design > Transactional Emails

{{template config_path="design/email/header"}}
{{template config_path="design/email/footer"}}