Splash of Style...Macs, Photography, Design, and other Passions

Expression Engine: Assign Global Variables

November 8, 2004 by debbie T | Expression Engine

I was working on my templates tonight. I want them to be as easy to re-use for other template groups. Since Expression Engine creates dynamic pages, the paths to images, css files, etc can be difficult to keep from breaking.

In other blog programs, I include a homepage or site-root tag when structuring image and css file paths. When the php file is parsed, the site url will be automatically added to the path.

Well, I couldn’t find such a tag in Expression Engines list of tags or variables. The closest I found was the {homepage} variable, but that added ‘index.php’ to the url, which causes the path to break.

Expression Engine allows users to create our own global variables.

I can specify a variable for just one template by including it at the top of the template coding:

{assign_variable:weblog_root="http://www.mysite.com/directory"}

Or I can assign a variable to be shared with any or all templates, by choosing Control Panel Home>Templates>Global Variables.

The name of the variable is “weblog_root” so whenever this variable is used in a template, my site root url will be automatically inserted into the coding. For example:

<style type="text/css" media="screen">
@import url('{weblog_root}/colorful.css');</style>

The end result will look like:

<style type="text/css" media="screen">
@import url('http://www.mysite.com/directory/colorful.css');</style>

Commments are closed for this article.