0

I would like to create a dynamic breadcrumb nav in Twig. Example url www.example.com/section/page I want to get the url, split it into a breadcrumb such as: home > section > page I've found this php code which works. Can anyone help convert it into Twig?

<?php
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
    // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = array("<a href=\"$base\">".$home."</a>");

    // Find out the index for the last value in our path array
    //$last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"".$base.$crumb."\">".$title."</a>";
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>

<p><?= breadcrumbs() ?></p>
<p><?= breadcrumbs(' > ') ?></p>
<p><?= breadcrumbs(' ^^ ', 'Index') ?></p>
jmiller
  • 578
  • 11
  • 28
  • Made some progress...`{% set lastPage = 'blah' %} ` Can't work out how to get the name of the current page though. Need to work out how to do a `foreach` statement also... – jmiller Jul 25 '16 at 07:37
  • This http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb?rq=1 may help? – jmiller Jul 25 '16 at 07:41

1 Answers1

0

You don't need to convert that function into Twig, you just can tell Twig to make this function available into the template for you by extending twig:

$twig = new Twig_Environment($loader);
$function = new Twig_SimpleFunction('breadcrumb', function ($separator = ' &raquo; ', $home = 'Home') {
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = array("<a href=\"$base\">".$home."</a>");

    // Find out the index for the last value in our path array
    //$last = end(array_keys($path));

    // Build the rest of the breadcrumbs
    foreach ($path AS $x => $crumb) {
        // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"".$base.$crumb."\">".$title."</a>";
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
});
$twig->addFunction($function);

You then can use this function inside twig wherever you want:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
    </head>
    <body>
        <nav>
           {{ breadcrumbs() }}
        </nav>
    </body>
</html>
DarkBee
  • 16,592
  • 6
  • 46
  • 58