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.

January 8th 2007 | ExpressionEngine, Path.php, Segments | 8 comments

Comments

  1. 1 Ross January 30th 2007, 13:29

    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

    ?>

  2. 2 Low January 30th 2007, 15:31

    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?

  3. 3 Ross January 30th 2007, 17:34

    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!

  4. 4 Erwin Heiser May 4th 2007, 21:25

    A nice solution, bookmarked this! Thanks for posting this.

  5. 5 amio November 26th 2007, 19:26

    a cute idea, thanks for that

  6. 6 Travis Schmeisser June 7th 2008, 23:21

    This is great! Is there a simple way to add “second to last” so I could check the two against eachother?

  7. 7 Low June 9th 2008, 13:27

    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.

  8. 8 Sean November 18th 2009, 09:10

    This is great - using this to make breadcrumbs in combination with this plugin http://www.iain.co.nz/blog/entry/crumbee_expressionengine_plugin

Leave a comment



Some html (a, em, strong, etc) allowed.
Email won’t be displayed and takes care of your Gravatar.