Template Tags

{exp:speedy:fragment}

Fragment caching uses a tag pair to cache a specific block of content, as opposed to the entire page when using Static caching. You can put anything you want inside of a {exp:speedy:fragment} tag pair, including {embed}, {exp:channel:entries}, or any other ExpressionEngine tag.

Parameters

  • driver="dummy|file|redis|memcache|memcached" The name of the cache driver you want to use for this specific instance of the fragment cache tag. This will override the default global setting in your config.php file. If not defined, the global driver defined in $config['speedy_driver'] will be used. (Default: "")

  • global="yes|no" Whether this item should be cached globally or not. Non-global items use the current url as part of their key. (Default: "no")

  • key="featured_item"The key for the item cached in this tag pair. If this key is used for a non-expired cache item, the content of that item will be used instead of parsing the content of the tag. (Required when global="yes") (Default: "item")

  • ttl="3600" The number of seconds before this cache item with expire. If left blank the default TTL (1 hour) will be used. Use ttl="0" to cache this item until the cache it broken. (Default: "")

  • tags="apple|orange|etc" A pipe-separated list of tags to apply to this item. Tags can be used in the cache breaking settings. (Default: "")

  • url_override="path/to/page"Override the url used as part of the key for non-global items. (Default: "")

  • url_prefix="something"Add a prefix to the key. Useful for multi-lingual sites, e.g. url_prefix="{publisher:current_language_code}" (Default: "")

    • If using Publisher, and you are not displaying the default language segment in the URL, you may need a conditional similar to the url_prefix parameter: url_prefix="{if publisher:current_language_id != publisher:default_language_id}{publisher:current_lang_code}{/if}"

Example

{exp:speedy:fragment}
... content to be cached ...
{/exp:speedy:fragment}

{exp:speedy:escape}

When using fragment caching, it may be desirable to prevent certain sections of the template that are inside your {exp:speedy:fragment} from being cached. ExpressionEngine will still parse the contents of an escape tag every page load.

Pre-escaping

Some template variables such as {segment_*} are always parsed before the module. For these tags we have implemented pre-escaping using a core extension.

To use pre-escaping, add a unique suffix to the tag.

Example

{exp:speedy:fragment}
... content to be cached ...
{!-- Regular escape --}
{exp:speedy:escape}
{if logged_in}
You are logged in
{if:else}
You are not logged in
{/if}
{/exp:speedy:escape}
{!-- Pre-escape --}
{exp:speedy:escape:foo}
You are on the {segment_2} page
{/exp:speedy:escape:foo}
... content to be cached ...
{/exp:speedy:fragment}

Template Layouts

Using ExpressionEngine's {layout} feature works as expected if you are using the Static cache driver, or Redis with static enabled, but if you are using fragment caching you'll need to cache your layout file as a separate global cache item, and escape the {layout:contents} tag. For example:

{exp:speedy:fragment key="global-layout" global="yes"}
<head></head>
{exp:speedy:escape}
{layout:my_custom_variable}
{layout:my_other_custom_variable}
{/exp:speedy:escape}
{exp:speedy:escape}
{layout:contents}
{/exp:speedy:escape}
<footer></footer>
{/exp:speedy:fragment}

Then inside your template:

{layout="layouts/main"}
{exp:speedy:fragment key="home"}
This is my home page content
{/exp:speedy:fragment}

Note, this technique will result in an additional cached item for each layout that is globally cached.