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

  1. author
    1 Ross 30 januari 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. author
    2 Low 30 januari 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. author
    3 Ross 30 januari 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. author
    4 Erwin Heiser 4 mei 2007, 21:25

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

  5. author
    5 amio 26 november 2007, 19:26

    a cute idea, thanks for that

Leave a comment

Some html (a, em, strong, etc) allowed.


Zoeken in loweblog.com