Dissecting the Default Templates: Variables, Paths, and weblog=
As we head down the road of dissecting the default templates, we’re going to learn some of the important facets of ExpressionEngine’s infrastructure.
Today, we’re going to focus on the area between the <head> and </head> section of the index template.
In doing that, we’re going to come across three vital elements that you need to understand, and it will be easier to do so going in. The first is the weblog= parameter. This parameter is going to be used in the majority of your tags, and tells that tag what weblog(s) you are pulling data from. So, when someone asks: “How do I relate a weblog to a template?” the answer is invariably “the weblog= parameter”. I encourage you to perform a search for “weblog=” in the docs right now to see just how many tags this parameter can be used in.
The second concept is variables, there are two types: variables that are context sensitive and require a surrounding tag pair, as well as global variables.
The third concept we’ll explore is path statements. Whenever you see something that is in the format of =“template_group/template_name”, then that is a form of path statement. You can read more about those here.
Now then, shall we dig in?
You’ll find this code in our default index template:
<head>
<title>{exp:weblog:info weblog="{my_weblog}"}{blog_title}{/exp:weblog:info}</title>
<meta http-equiv="Content-Type" content="text/html; charset={charset}" />
<link rel='stylesheet' type='text/css' media='all' href='{stylesheet={my_template_group}/site_css}' />
<style type='text/css' media='screen'>@import "{stylesheet={my_template_group}/site_css}";</style>
<link rel="alternate" type="application/rss+xml" title="RSS" href="{path={my_template_group}/rss_2.0}" />
<link rel="alternate" type="application/atom+xml" title="Atom" href="{path={my_template_group}/atom}" />
</head>
By breaking it down we can learn how some important concepts come into play in ExpressionEngine, so that we can use those concepts later.
Line by line we go . . .
<title>{exp:weblog:info weblog="{my_weblog}"}{blog_title}{/exp:weblog:info}</title>
Begin by first searching for the first part of the above tag: “exp:weblog:info” to find the weblog information tag. Now we know that this tag can be used to dynamically pull into the template information about the weblog specified in our weblog= parameter. In this case, we’re using this to populate the title of the browser, so that folks know what page they’re visiting. The advantage to doing this is that later, if you rename your weblog, then it will change anywhere you have used this tag; you do not need to worry about how many templates you place it into.
Here we also see {blog_title}. This is a context-specific variable - what do I mean by that? It is a variable that will be changed to the blog_title that you specified in your weblog preferences, however, this variable can only be used when enclosed in the Weblog Info tag pair; if you place it outside of the tag pair, then it will not know what {blog_title} you want and will thusly echo directly to the screen. Most variables, with the exception of global variables, require a surrounding tag pair for context.
Now on to the next line!
<meta http-equiv="Content-Type" content="text/html; charset={charset}" />
This is standard line declaring the character set of your page; however, in this case, we’re using a standard global variable since this is something that we have defined in our ExpressionEngine settings. Unlike the {blog_title} variable that we discussed before, global variables do not require a context and may be used anywhere in your templates.
The next two lines tell the browser where to locate our stylesheet, so that we can make our pages look beautiful:
<link rel='stylesheet' type='text/css' media='all' href='{stylesheet={my_template_group}/site_css}' />
<style type='text/css' media='screen'>@import "{stylesheet={my_template_group}/site_css}";</style>
Since we see a new tag: stylesheet=, we want to immediately plug “stylesheet” into the documentation to learn about this tag, and we’ll then find Linking to Stylesheets. As we can see from the documentation, the stylesheet= is a variable that allows us to include the template_group and template_name; therefore, it is a type of path variable. ExpressionEngine uses these types of variables almost everywhere that you create a link, so it is important to understand that now so that you can easily create links between pages in your site. Doing so in this manner ensures that moving servers, or changing domains later, is a very simple process, not involving editing every single template.
The final two lines add RSS auto-discovery to the page so that modern browsers can automatically detect and know where your RSS feeds are:
<link rel="alternate" type="application/rss+xml" title="RSS" href="{path={my_template_group}/rss_2.0}" />
<link rel="alternate" type="application/atom+xml" title="Atom" href="{path={my_template_group}/atom}" />
This is a fairly standard auto-discovery line, but again we are leveraging the path variables to ease long-term maintenance needs, as well as to make reading the template easier.
So, now we have the concept of variables: context sensitive and global, path statements, and the weblog= parameter. Remember, these are really basic infrastructure items that are extremely important principles in ExpressionEngine and are the building blocks of your EE sites, so this will give us some solid footing from which to venture further into the template next week.


