Truly independent EE Templates now possible.

One of the bigger improvements in the EE 1.2 beta that some folks might overlook is the new ability to define optional “global variables” via the path.php file. This is primarily of importance to folks who, like me, are running multiple blogs in different subdirectories or domains. What this allows you to do is to define a list of global variables that will only apply to a particular blog based on the subdirectory it is associated with. Among other things, using this feature will allow you to design templates that are truly blog independent.

ExpressionEngine is unique in that it doesn’t force an association between a particular template group and a particular blog, but this flexibility was hampered if you wanted to limit the output to just one blog out of all the ones in your system. Previously to do that you’d have to force an association by specifying in your {exp:weblog:entries} loop which blog it should pull the data from. This was fine if you were using different template groups for each blog, but what if you wanted to use a single template group for all of your blogs? Couldn’t be done. You’d have to duplicate that template group repeatedly for each blog even though the templates in each group were all the same other than being hardcoded for a particular blog. Under 1.2 with this new option, though, it can be done.

Here’s how:

  1. Download and open up the path.php for the blog you want to set a global variable for.
  2. Add the following code at the bottom before the closing ?> tag changing the words “short name of blog” appropriately:
    $global_vars = array(
    "master_weblog_name" => "short name of blog"
  3. Save the path.php and re-upload it to your site making sure it goes back into the same sub-directory you downloaded it from.

Now to make a template universal you’ll need to modify it to add the global variable in you just created. If you’re assigning a global variable at the top of the template such as {assign_variable:master_weblog_name=”myblog”} then you can just delete that line and your templates should still work just fine. If you hardcoded the weblog=“shortname” in all the EE tags that use it then you’ll need to go through and change them to the global variable you defined. Keep in mind that any universal templates you create will require that the blogs using it all share the same weblog fields in order to work properly.

To illustrate exactly what it does take a look at my port of the Kubrick template: Here’s SEB using Kubrick and here’s Momma’s Corner using it as well. Or how about SEB with Neil’s Icy Blue and then Momma with Neil’s Icy Blue. One template group, two different blogs.


7 thoughts on “Truly independent EE Templates now possible.

  1. Can embedded templates use the global variables now?  I created embeds for header, nav bar, sidebar, main body (and some variations) and footer.  This allowed me to make changes in one place.  But, at least through 1.1, global variables didn’t work in the embeds…

  2. I’ll answer my own question – yes.  And I’m not quite sure I stated things correctly above re global variables.

    Yes, Les, this is VERY cool and good news.  It should now allow more portable templates, too, shouldn’t it?

  3. Indeed it should.

    Chris and I had a big discussion over this feature back when I first suggested it in the support forums, but there was the question of how EE would know which blog the visitor had entered from. Then one afternoon it hit me that the path.php file would be the most logical place to put that sort of info and I dashed an email off to Paul who agreed it had merit and forwarded it onto Rick. It was expanded to allow for any blog-specific global variables you wanted to define and is now a reality. It seems to work pretty well too.

  4. I’ll have to think about other variables that might be useful.

    BTW, I’m using the ‘save template as file’ feature quite heavily and it came in handy an hour ago when I was able to go through and replace all my ‘hard-coded’ weblog names with the global variables. 

    BBEdit to the rescue, search/replace across all my template files.  I also find it easier to edit templates in a real editor rather than in a browser.  OW 5.1 allows search/replace in a text-area, otherwise it’s a pain in the a**.

    Rick & Co. have been really responsive with EE enhancements – amazing stuff.

  5. Here’s another place where the master_weblog_variable would be useful – the RSS templates….

    Just so happens I’m getting ready to migrate some templates from my sandbox over to the main site and have been trying to find all of the places I’d hard-coded the weblog name…

    And I had thrown up some temp pages at the new site, using your Kubick templates, and I’m still finding links to SEB in _those_!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.