Global Variables

Some variables are global in scope, meaning they are not limited to a particular tag. You can use global variables anywhere in any template.

Paths

{path="template_group/template"}

The path variable is substituted with the template group and template name you specify, and it will automatically include your base site path. For example if your base site is at http://www.yoursite.com/ and your site index page is index.php, then this variable:

{path="weblog/comments"}

Will be substituted with:

http://www.yoursite.com/index.php/weblog/comments/

You can also use SITE_INDEX in your path to point to your main site index:

{path="SITE_INDEX"}

Additionally, you can use LOGOUT in your path to point to the member logout link:

{path="LOGOUT"}

Stylesheets

{stylesheet=weblog/weblog_css}

The stylesheet variables is used exclusively in the CSS declaration in your pages. It must contain the template group/template name where your stylesheet is located.

<link rel="stylesheet" type="text/css" media="all" href="{stylesheet=weblog/weblog_css}" />

Global Conditionals

There are a number of conditionals which can be checked globally, regardless of whether or not you are inside a particular tag. Conditional variables allow you to add scripting in order to show data if certain criteria that you define are met. Conditional variables have this prototype:

{if variable operator condition}

Data between the tags that gets shown if the condition is met.

{/if}

You can use any of the following operators:

A conditional variable will look like this:

{if username == "joe"}

<h1>Hi Joe!</h1>

{/if}

Note: The left side of the condition must always be the item you are comparing. The right side must be the condition for which you are testing. If you are testing against a word, you should enclose the word in single or double-quotes. If you are testing against a number, then you do not need to use quotes.

if group_id

{if group_id == '7'}

You're an "Editor"!
{/if}

You can test against the Member Group. This tests the Member Group ID number. The alternate {if member_group == '3'} version of this conditional should be used inside of {exp:weblog:entries} tags.

if logged_in

{if logged_in}

Welcome back to the site!<br />
<a href="{path="LOGOUT"}">Logout</a>

{/if}

This tag pair will display content within the pair if the person viewing the page is currently a logged in member.

Note: This only tests whether or not someone is logged in. If you want to restrict a particular page based on the member group assignment you'll do that in your Template preferences in the Control Panel.

if logged_out

{if logged_out}

You aren't a member or aren't logged in.<br />
<a href="{path="member/login"}">Login</a> | <a href="{path="member/register"}">Register</a>

{/if}

This tag pair will display content within the pair if the person viewing the page is not currently a logged in member.

You'll notice in the "logout" link above that a special path is used: {path="LOGOUT"}. This is a special-case path value that will automatically render the correct path for someone to log out.

if member_group

{if member_group == '7'}

You're an "Editor"!
{/if}

You can test against the Member Group. This tests the Member Group ID number. This variable/conditional is identical to the group_id one available above. {member_group} will work correctly inside a {exp:weblog:entries} tag, however.

if member_id

{if member_id == '147'}

Ooh, you're really special, Frank!!
{/if}

Test for the member ID of the currently logged in user.

if screen_name

{if screen_name == "Mr. Ed"}

Thanks for all your hard work on the site, Ed!
{/if}

You can test against the screen name of the currently logged in user.

if total_comments

{if total_comments < 1}

What?? No one has commented on my site at all?!?!
{/if}

Test against the total number of comments submitted for the entire site.

if total_entries

{if total_entries > 1000}

Yowza! This is one hot site!
{/if}

Test against the total number of entries submitted for the entire site.

if username

{if username == "elvira"}

Hi, mom! I know it's you!
{/if}

You can test against the username of the currently logged in user.

Other Global Variables

app_version

{app_version}

This variable will be substituted with the currently installed software version.

charset

{charset}

This variable will be substituted for the global character set preference.

current_time

{current_time}

This variable displays the current server time localized to each user's particular setting. As with other date variables, you use the standard date variable formatting:

{current_time format="%Y %m %d %H:%i:%s"}

debug_mode

{debug_mode}

This variable will be substituted with either "on" or "off" based on your debug mode settings.

email

{email}

The email address for the currently logged-in user.

embed

{embed=news/local}

This variable allows you to embed one Template within another. More information can be found in the Sub-Templates section. Similar to the {path=} variable, {embed=} allows you to specify the Template_Group/Template that you wish to include.

encode

{encode="you@yoursite.com" title="Email Address"}

This variable will automatically encode the specified email address to make it more difficult for spam harvesters to grab an email address.

The title parameter on the variable allows you to specify the text you want to use for the link. So, if you used the above example, then the text "Email Address" would appear on your page as an encoded link to the address "you@yoursite.com".

group_id

{group_id}

The Member Group ID number for the currently logged-in user.

gzip_mode

{gzip_mode}

This variable will be substituted with either "on" or "off" based on your output compression settings mode settings.

hits

{hits}

This variable will be substituted with the number of hits that any given template containing the variable has received.

ip_address

{ip_address}

This variable will be substituted with the IP address of the currently logged in user.

ip_hostname

{ip_hostname}

This variable will be substituted with the host name that corresponds to the IP address of the currently logged in user.

location

{location}

The location (as entered in their profile) for the currently logged-in user.

member_group

{member_group}

The Member Group ID number for the currently logged-in user.

member_id

{member_id}

The Member ID for the currently logged-in user.

member_profile_link

{member_profile_link}

This variable will be substituted with a link to the public profile page for the currently logged in user. The text of the link will be the member's screen name. For instance, the output might be:

<a href="http://www.example.com/index.php/member/1/">Joe Smith</a>

screen_name

{screen_name}

The screen name for the currently logged-in user.

total_comments

{total_comments}

The total number of comments posted by the currently logged-in user.

total_entries

{total_entries}

The total number of entries posted by the currently logged-in user.

username

{username}

The username for the currently logged-in user.

URL Segments

{segment_1}
{segment_2}
{segment_3}
{segment_4}
{segment_5}
{segment_6}

These global variables represent data stored in the various segments of your page URL. By "segments", we mean the segments of the URL that appear after your standard index.php file (or whatever you might have changed the name to be). A standard URL might look like:

http://www.example.com/index.php/products/shirts/

In the above URL, "products" is the Template Group, which happens to be in the "segment 1" position. "shirts" is the Template and is segment 2. If needed, you could access the values of either of those segments ("products" or "shirts") using {segment_1} or {segment_2}. The real power, however, will appear if you add additional information. For example:

http://www.example.com/index.php/products/shirts/large/blue/

With this, you can pass a shirt size ("large") as well as color ("blue") and access those values with {segment_3} and {segment_4}, respectively. You can use the data in any Template.

User-Defined Variables

Custom, user-defined global variables can be created for use in any Template. In the Control Panel, access the "Global Variables" link in the Templates section. User-defined global variables can hold any static information you need. User-defined global variables can be useful for things like site footers, copyright notices, etc.

Note: User-defined global variables are not parsed by the ExpressionEngine script, so you cannot place tags or variables inside them. If you need something to hold tags or variables, use a regular Template and then {embed=} that Template where you need it.

You cannot give a user-defined global variable the same name as an existing variable. This includes variables native to ExpressionEngine.