I am using creating template parts (header, footer) but would like to know how I can add a variable, a body class to individual templates.

I see people are using stash but is there another way to set a variable on a per-page basis.



2 Answers 2


The easier and faster way to do this is using Preload Text Replacements and snippets.

Snippets are parsed before Preload Text Replacements. So you can put them inside snippets.

Let me show you an example.

This is part of a snippet called doctype-and-styles-snip:

{preload_replace:pre-rootclass=""}<!doctype html>
<html class="no-js no-jquery {if segment_1 == ""}home{/if} {pre-rootclass}">

I put the CLASS on HTML instead of BODY, but the principle is the same. The {preload_replace:pre-rootclass=""} is intended to remove the unused CLASS.

On templates, you can use something like this:

{exp:channel:entries limit='1' require_entry='yes' disable='pagination' [...]}
<title>{title} - {site_name}</title>

When you need a custom class, just add a preload_replace before the snippet. For example, this is a 404 page:

<title>Not found - {site_name}</title>
<meta name="robots" content="noindex,follow,noarchive" />
<meta name="description" content="not found" />

This way, you can have custom classnames per page avoiding embeds and stash.


Just wanted to add another way round this which might be helpful:

You could do something like this if you wanted to use the partial in lots of places but maybe change its styling a little depending on template (without use of conditionals).

In the template:


In the snippet {s_block}:

<div class="{pre_s_block_class}">
... content ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.