Part of the EllisLab Network

Blog & News

Lisa Wess
Director of Community Services

Dissecting the Default Templates: Assigned Variables

Back in May, 2004, I discovered ExpressionEngine.  Why?  I found out that EllisLab (then pMachine, Inc) was giving away 1000 licenses (and then added another 250 later!).  I got mine.

ExpressionEngine was different than what I was used to: had different ideas on templates and storage and was wonderfully new.  I didn’t even know where to set a limit for number of posts shown on the index!  What else was there to do? 

The installation came with a default template that worked.  I surely didn’t want to read the documentation as I had no context for that information; so, instead, I began to dissect those templates.  Of course, I did have the docs open to look up what those templates were doing and we’ll do some of that during the dissection, as well.

So, how about we do just that, together?  A new series of articles on dissecting the default templates.  We’ll see how it works and from there we can apply that to our own templates, yes? 

It is important that, prior to reading these articles, you review the Getting Started section of the documentation and go through the Quick Start Tutorial, as well as our Video Tutorials.  All of these articles will build on what is in the documentation, focusing on its actual use in a real scenario as shipped with ExpressionEngine.  Hopefully, this will enable you to go out and use these tricks on your own sites.

If you wish to follow along, the easiest way to do so is to create a new weblog, choose the “default” template, so that you can look at those code examples and visit them on your own site as we work through the templates.  You don’t have to do this, but if you’re a hands-on type, then it could help.  I did just that, in a “tutorial” template group.

Let’s begin with the first two lines, because they’re the first things we see and because we need to use that information later in the template.

{assign_variable:my_weblog="default_site"}
{assign_variable
:my_template_group="tutorial"}

Without the documentation, there is no way to know what these do.  The best way to find this in the docs.  Copy assign_variable (the first part of the tag) and paste it into the search box at the top of the documentation.  The first link is to Assign Global Variables.  No cover-to-cover documentation reading here, just some easy, quick, painless cross-referencing.

So, what do the assigned variables do?  Well, nothing on their own, but they do allow you to reference them from anywhere further down in the same template, so that if you are using some information many times, you can just call your assigned variable, and have one easy place to edit it at the top of the template.

So for our purposes, we have this:

{assign_variable:my_weblog="default_site"}

we now know, from reading the documentation, that {my_weblog} is the variable we can use later, and what it actually means is “default_site”.  Although this is getting a bit ahead of ourselves, go ahead and scroll through the template, and locate {my_weblog}.  You’ll see it several times - now if you ever want to change the short-name of your weblog, you need update it only at the very top, rather than searching through the template and replacing all of the instances within.

Just to cement this, in the second assigned variable:

{assign_variable:my_template_group="tutorial"}

{my_template_group} is the variable, and tutorial is what it actually comes out as.  Again, scroll through that template and you’ll see that {my_template_group} is used several times.  Later on, if you decide to rename that template group, it will take only a second to update it at the top of the template.

Now we have the assigned variables down, next week we’ll start digging into more of the guts of the default template, focusing first on the index template.  See you then!