Last segment: a global variable
As you might know, ExpressionEngine uses segments to access information in your URLs. These segment variables are available throughout your templates and are useful for conditionals, other EE tags, and a whole lot more. Retrieving an absolute segment is easy. Retrieving a relative segment, like the last one? Not so much. Path.php global variables to the rescue!
Path to relativity
You can define any number of variables in your path.php file, which will be globally available, just like the standard URL segment variables. This means we can also define a {last_segment} variable, if we add some clever code.
function getLastSegment($uri)
{
if (!strlen($uri)) return '';
if (substr($uri,-1,1) == '/')
{
$uri = substr($uri,0,-1);
}
return substr($uri,(strrpos($uri, '/')+1));
}
At the bottom of your path.php file (but before the ?>) add the above function. This function will check $uri — a variable created by the index.php file and available for use — for the last segment, after stripping a possible trailing slash. Now you only have to call the function to retrieve your last segment. Add it to the $global_vars array.
$global_vars['last_segment'] = getLastSegment($uri);
Presto, you’re done! Just use {last_segment} anywhere in your templates and experience the joy of relative segment variables.
Comments
Hi there!
I’m not sure how you got this working. Can you post the entire code of your path.php file (obviously without site-specific code)? If I put the function code in my PHP file it appears on my site. And I don’t know where to put the single line of code either. my path.php files looks like this:
<?php
// ——————————————————
// DO NOT ALTER THIS FILE UNLESS YOU HAVE A REASON TO
// ——————————————————
// Path to the directory containing your backend files
$system_path = “./system/”;
// ——————————————————
// MANUALLY CONFIGURABLE VARIABLES
// See user guide for more information
// ——————————————————
$template_group = “”;
$template = “”;
$site_url = “”;
$site_index = “”;
$site_404 = “”;
$global_vars = array(); // This array must be associative
?>
Sure, Ross. Just replace the line “$global_vars = array(); // This array must be associative” with this:
$global_vars = array(
‘last_segment’ => getLastSegment($uri)
);
function
getLastSegment($uri) {if (!strlen($uri)) return ”;
if (substr($uri,-1,1) == ‘/’) { $uri = substr($uri,0,-1); }
return substr($uri,(strrpos($uri, ‘/’)+1));
}
I condensed the function a bit for readability, but it’s still the same… Does this clarify things?
Great, thanks a lot for your help! This is very useful and combines nicely with the Static Pages module that I am using at the moment.
Much appreciated!
A nice solution, bookmarked this! Thanks for posting this.
a cute idea, thanks for that
This is great! Is there a simple way to add “second to last” so I could check the two against eachother?
Hi Travis. You’d have to change the getLastSegment() function. Use
explode("/",$uri)to get an array containing all segments and get the (second to) last segment that way.This is great - using this to make breadcrumbs in combination with this plugin http://www.iain.co.nz/blog/entry/crumbee_expressionengine_plugin