Download EE 2 Docs Download EE 1 Docs
ExpressionEngine

2.9.2 User Guide

Snippets

Snippets are small bits of reusable template or tag parts. You could create a Snippet for any number of purposes, anywhere that you need to reuse a small portion of a template, including partial or complete tags, other variables, etc. Snippets add flexibility and reusability, while making it simple to make site-wide changes by editing the Snippet’s source instead of having to modify many templates.

One idea would be to hold a particular date format string that you wish to reuse over and over. By making it a Snippet you can change it in one place and immediately see the effects everywhere that you’ve used it. For example, you could create a Snippet named my_date_formatting with a value of format=”%m-%d-%Y” and use it in any date variable thusly:

{entry_date {my_date_formatting}}

It will be instantly expanded before your template is parsed, just as if you had put the expanded text into the template itself:

{entry_date format="%m-%d-%Y"}

You can create and edit Snippets at Design ‣ Templates ‣ Snippets.

Note

Snippets may not be nested inside other snippets.

What is the difference between a Snippet and a User-defined Global Variable?

Snippets are expanded at a very early stage on each template, making it possible for them to hold dynamic content, ExpressionEngine tags, other variables, PHP, etc. (Read more about the rendering stages of the template engine.) They shine when you need to reuse dynamic information but don’t need the extra overhead of access control or separate preferences of an embedded template. User-defined Global Variables are the polar opposites, expanded during one of the final rendering stages of the template engine, and should be used for static text, HTML, JavaScript, and other static content that would not affect other tags and variables on the template.

What is the difference between a Snippet and an Embedded Template?

Snippets can be considered to actually be part of the template that they are used on, with their expanded contents parsed simultaneous to other tags and variables on the template. Embedded templates are separate templates, with their own preferences (caching, PHP parsing, access, etc.), and are parsed individually. Put another way, embedded templates are not included in the parent template, but rather added to them after the fact, using a separate query and full page parsing resources for each template.

Multiple Site Manager Tip

If you are using the Multiple Site Manager, you’ll notice that you have a new preference when editing each Snippet: make it available to all your MSM sites or this site only. To easily identify the difference when reading your templates, consider prefixing your Snippet names with the site’s short name or, for Snippets available to all sites, global:

{ellislab_date_formatting}

{codeigniter_date_formatting}

{global_date_formatting}

User Contributed Notes

Posted by: Visiluna on 5 September 2014 12:53pm
Visiluna's avatar

There is a “missing feature” I’ve always wanted, but that never seems to make the developers’ priority list. The {categories} variable pair inside the {exp:channel:entries} loop provides access to the {parent_id} variable (the category_id of the category’s parent category), but not a {parent_url_title} variable. What gives?

Snippets to the rescue!

I created a snippet with the Snippet Name “parent_url_title” and the Variable Content as follows:

{exp:query sql="SELECT `cat_url_title` as purl FROM `exp_categories` WHERE `cat_id` = '{parent_id}'"}{purl}{/exp:query} 

Now, when I include {parent_url_title} inside the {categories} variable pair of a {exp:channel:entries} tag, I have the “variable” I always wanted.

The possibilities are many.

Posted by: Chad Crowell on 30 April 2011 12:46am
Chad Crowell's avatar

Note that you can’t put a channel:entries tag into a snippet as a replacement for putting it inside an embed. For instance, if you have a channel:entries tag pair and need another one inside of it, you can’t do that in a snippet, it will need to be in an embedded template.  The reason is that when the snippet is parsed and the rendered code placed into the template, you’ll essentially have a c:e tag pair inside of another c:e tag pair, which won’t work. More info: http://ellislab.com/forums/viewthread/187760/

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