Download EE 2 Docs Download EE 1 Docs
ExpressionEngine

2.10.1 User Guide

Preload Text Replacements

ExpressionEngine allows you to assign text to be replaced prior to a template being parsed, to save time typing and editing certain bits of text that might get used multiple times. Preload Replacements act as a straight string replacement for use later in the same template.

Assignment and replacement occurs instantly when the template is loaded, before any tags are parsed, and therefore may not be affected by the result of another tag’s output.

Creating and Using an Preload Replacements

The basic syntax for replacing text is this:

{preload_replace:variable_name="replacement"}

In the above example, variable_name is the name of the template “variable” and replacement is the content that will be used to replace all occurrences of {variable_name} in the template.

Example Usage

A common usage for preload replacements is to hold the Channel short name(s), to save you from repeatedly typing them into different tags within the same template.

{preload_replace:channels="news|reviews"}

{exp:channel:category_heading channel="{channels}"}
        <h1>{category_name}</h1>
        {if category_description != ""}<p>{category_description}</p>{/if}
{/exp:channel:category_heading}

{exp:channel:entries channel="{channels}" limit="10"}
        <h2>{title}</h2>
        {body}
{/exp:channel:entries}

User Contributed Notes

Posted by: GDmac - expocom on 2 January 2012 3:16pm
GDmac - expocom's avatar

... Assignment and replacement occurs instantly when the template is loaded, before any tags are parsed…
If you assign a value to the same preload variable multiple times, then only the first declaration will be used.
The tag is, as mentioned, instantly replaced, leaving no other tag with the same name in the template to be replaced by a second declaration.

However, (hat-tip to Lodewijk), because of parse order, there are some tags that are parsed before preload_replace:
these are snippets, segment_x, embed:var variables and simple conditionals, and global_vars declared in index.php,  which we can use to our benefit (tested with EE 2.3.1). e.g.

// simple conditionals allow you to override a preload var if a segment is set, remember, only the first preload in a template is used
{if segment_3 != ""}{preload_replace:foo="special"}{/if}
{preload_replace
:foo="default"}

// current_time is also parsed early, So you can use ee-tags, but always be careful to surround single quotes with double quotes!
{preload_replace:party="Welcome to the {current_time format='%Y'} pages"

 

Posted by: e-man on 24 November 2011 2:06pm
e-man's avatar

An excellent article on taking snippets and preload replace vars a step further by codemeister @low can be found here:
http://gotolow.com/blog/snippets-and-preload-replace-variables

Posted by: Chad Crowell on 22 June 2010 3:48am
Chad Crowell's avatar

Updated RSS 2.0 template for EE2 using a channel called Blog and related links in the body. Adapt as needed for your channel

{preload_replace:master_channel_name="blog"}
{exp
:rss:feed channel="{master_channel_name}"}

<?xml version
="1.0" encoding="{encoding}"?>
<rss version="2.0"
    
xmlns:dc="http://purl.org/dc/elements/1.1/"
    
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    
xmlns:admin="http://webns.net/mvcb/"
    
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    
xmlns:content="http://purl.org/rss/1.0/modules/content/">

    <
channel>
    
    <
title>{exp:xml_encode}{channel_name}{/exp:xml_encode}</title>
    <
link>{channel_url}</link>
    <
description>{channel_description}</description>
    <
dc:language>{channel_language}</dc:language>
    <
dc:creator>{email}</dc:creator>
    <
dc:rights>Copyright {gmt_date format="%Y"}</dc:rights>
    <
dc:date>{gmt_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
    <
admin:generatorAgent rdf:resource="http://expressionengine.com/" />
    
{exp:channel:entries channel="{master_channel_name}" limit="10" rdf="off" dynamic_start="on" disable="member_data|trackbacks"}
    
<item>
      <
title>{exp:xml_encode}{title}{/exp:xml_encode}</title>
      <
link>{site_url}/blog/view/{url_title}</link>
      <
guid>{site_url}/blog/view/{url_title}#When:{gmt_entry_date format="%H:%i:%sZ"}</guid>
      
<description>{exp:xml_encode}{cf_blog_body}{/exp:xml_encode}</description>
      <
dc:subject>{exp:xml_encode}{categories backspace="1"}{category_name}{/categories}{/exp:xml_encode}</dc:subject>
      <
dc:date>{gmt_entry_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
    </
item>
{/exp:channel:entries}
    
    
</channel>
</
rss>

{/exp:rss:feed} 

You must have an EllisLab product license and have at least 50 posts to the community forums to contribute notes to the User Guide