DEV Community

Cover image for Preload Replace Variables
ExpressionEngine for ExpressionEngine

Posted on • Originally published at u.expressionengine.com

Preload Replace Variables

Did you know that ExpressionEngine’s preload replace tags support prefixes? Preload replace variables are pretty straight forward. You set a new variable and give it a text value, then later in the template you can access that value. For example if you create a new preload replace variable {preload_replace:foobar="bazz"} the variable name {foobar} has no context, or namespace. So at first glance it may not be apparent where the variable came from. You can add a prefix to all your preload_replace variables, e.g. {preload_replace:pre:foobar="bazz"}, and when accessing it later in the template, the syntax will be {pre:foobar}.

Using preload replace variables are pretty straight forward, but order does matter in some cases. For example, the following works as you would expect depending on the value of segment_2 {pre:variable} will print either bar or bazz.

{if segment_2 == "foo"}
    {preload_replace:pre:variable="bar"}
{if:else}
    {preload_replace:pre:variable="bazz"}
{/if}

Enter fullscreen mode Exit fullscreen mode

You can also set a default value without the {if:else}:

{if segment_2 == "foo"}
    {preload_replace:pre:variable="bar"}
{/if}

{preload_replace:pre:variable="bazz"}

Enter fullscreen mode Exit fullscreen mode

However, you can’t set the default value before the conditional. If segment_2 is equal to foo, {pre:variable} will still print bazz.

{preload_replace:pre:variable="bazz"}

{if segment_2 == "foo"}
    {preload_replace:pre:variable="bar"}
{/if}

Enter fullscreen mode Exit fullscreen mode

Order matters because ExpressionEngine’s template parser only parses the first occurrence of a preload replace variable. If the conditional above evaluates true, the second occurrence of the same preload replace variable is ignored. In most programming languages you define the variable with the default value first, then override it based on how a conditional evaluates.

The following is a practical example of using preload replace. In this case the {segment:child_category} variable is created with a custom extension, which could contain a string of IDs, such as 677|678|700, and we are using a regular expression to check if it contains a specific ID.

{if segment:child_category ~ "/(677)/"}
    {preload_replace:pre:full_width="yes"}
{/if}

{preload_replace:pre:full_width="no"}

{special_events_sections category="{segment:child_category}" limit="1"}
    <div class="slab slab--flex theme--aqua-haze">
        <section class="slab__cell--{if '{pre:full_width}' == 'yes'}100{if:else}66{/if}">
            <div class="slab--flex__container">
                <div class="cell__content">
                    <!-- content -->
                </div>
            </div>
        </section>
        {if "{pre:full_width}" != "yes"}
            <section class="slab__cell--33">
                <div class="slab--flex__container">
                    <div class="cell__content">
                        <!-- content -->
                    </div>
                </div>
            </section>
        {/if}
    </div>
{/special_events_sections}

Enter fullscreen mode Exit fullscreen mode

You can also use preload replace variables to define parameters for a partial. ExpressionEngine’s template partials do not support embed parameters, e.g. {embed="group/template" foo="bar"}, but you can mimic that behavior with a preload replace. The following is a bit of a contrived example, but it illustrates how you can use preload replace to make a partial that can be configured based on how and where it is used.

<article class="heading heading--{pre:section}">
    <{pre:heading_tag}>{title}</{pre:heading_tag}>
</article>

Enter fullscreen mode Exit fullscreen mode

When using that partial you can define the preload replace variables just before the partial:

{preload_replace:pre:heading_tag="h2"}
{preload_replace:pre:section="products"}
{article_heading_partial}

Enter fullscreen mode Exit fullscreen mode

--
Originally published by Brian Litzinger at u.expressionengine.com

Runner H image

An AI Agent That Handles Life, Not Just Work

From ordering flowers to booking your dinner — let Runner H turn your ideas into actions. No prompts, no hassle. Just outcomes.

Try Runner H

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

Announcing the First DEV Education Track: "Build Apps with Google AI Studio"

The moment is here! We recently announced DEV Education Tracks, our new initiative to bring you structured learning paths directly from industry experts.

Dive in and Learn

DEV is bringing Education Tracks to the community. Dismiss if you're not interested. ❤️