15

What is the difference between a global variable, a snippet and an embedded template?

When and where should I be using each of them?

31

TLDR; In general, my preference would normally be to use a snippet, unless I need the variable content to be user editable (eg site email), and to only use an embed when parse order issues or similar necessitate it.

Here's why...

Snippets:

Snippets are frequently my first choice for implementing reusable code fragments in EE. They are parsed at a very early stage on each template, making it possible for them to hold dynamic content, ExpressionEngine tags, other variables, PHP, etc.

They are also very lightweight in terms of overhead, in that they don't require another pass of the templating engine in the way that an embed would do. Use a snippet to to reuse dynamic information, but when you don’t need the extra overhead of access control or separate preferences of an embedded template.

It is possible to save snippets out as flat files, but only by using an addon, such as Snippet Sync.

Advantages: Fast! can contain dynamic content/tags due to early parsing.

Disadvantages: Can't be nested, can't be natively saved as files.


Embedded templates

Any ExpressionEngine template can be embedded into any other template using the syntax: {embed="group/template"}.

As embeds are a separate template, if you want any output from the parent template to be available inside the embed you must explicitly pass variables into it as parameters like so: {embed="path/template" variable="foo"}. These which will then be available in your template like so: {embed:foo}.

{embed=""} tags are processed after all of the other tags of the original template are processed, this means you can set these parameters with other variables too:

{exp:channel:entries channel="blog"}
  {!-- on the last loop through our entries embed a template and pass the output of a tag as a parameter to the embed--}
  {if count == total_results}
    {embed="path/template" my_total="{total_results"}
  {/if}
{exp:channel:entries}

According to the EE user guide you should use an embed when:

  • You need Embed Variables.
  • You need separate PHP parsing preferences from the template this code is being added to.
  • You need separate caching preferences from the template this code is being added to.
  • You need separate template Access control from the template this code is being added to.
  • You need to also be able to access this template on its own directly via the URL.
  • You need to edit this content using the flat-file template system. You need to more discretely control which users are allowed to modify this content.

Advantages: Allow avoidance of parse order issues. PHP can be enabled for an embed but not for the parent template. Can be nested. Full template permissions control.

Disadvantages: Slow, require variables to be manually passed in from the parent template.


User Defined Global Variables

Global variables* are pretty much the opposite of snippets. They are parsed late in the template parse order , and thus cannot contain other EE tags. though they can be parsed as parameter values for other tags. Normal global variables cannot easily be stored outside of the database (though again, this is possible with some addons).

They are best suited for repeated static string data such as contact email addresses, phone numbers, company registration numbers and the like, though they can also be used for static text, HTML, JavaScript, and other static content that would not affect other tags and variables in your templates.

I frequently use the (free) Republic Variables addon to allow clients to edit global variables via a more friendly UI.

*The name "global variables" is something of a misnomer IMO, as GV's cannot be changed once assigned - ie they are really constants.

Advantages: minimal parsing cost, easyish for clients to edit.

Disadvantages: parsed late


Third Party Alternatives

There are also a number of excellent third party alternatives which can help reduce the need to use embeds/jump through hoops with the parse order and thus improve site performance, these include:

Mark Croxton's powerful Stash plugin Low Variables, which allows precise control of which variables are parsed late/early (and much more)

If there are others that should feature here add a comment and I will add them to this answer.

  • 1
    I like using snippets also but like to edit them as files. Give SnippetsSync a try! It's awesome – Josh Conner Nov 21 '12 at 23:21
  • Just wanted to add that EE native path variables actually work within Global Variables. – Frequency Feb 14 '13 at 11:51
8

All three of these are used for enabling you to not repeat yourself when building sites, but they vary in functionality quite a lot.

Global Variables Global Variables are used generally for storing pieces of content that might not be be part of an entry, for instance a contact form email address, a store address, etc. You can not use EE tags within a global variable. Oftentimes I use the Low Variables module to extend Global Variables with additional fieldtypes, creating a great settings area in the control panel for the users.

Snippets Snippets are used for storing reusable pieces of templates. The snippet gets parsed before the template they are inside of gets parsed, so you can add any EE tags you like inside a snippet and it'll act just like it's part of the template you put it in.

Embedded Templates Embedded templates have the greatest overhead of all of these. They start the template parser again and run independently from the parent template, and just dump the output from the embedded template into the parent template. It's generally a best practice to avoid using embeds as much as is practical, only using them when it's necessary to nest tags that otherwise couldn't be nested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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