2016-07-24 17 views
0

Ich möchte eine dynamische Breadcrumb-Nav in Twig erstellen. Beispiel url www.example.com/section/page Ich möchte die URL erhalten, teilen Sie es in einen Breadcrumb wie: home > section > page Ich habe diesen PHP-Code gefunden, der funktioniert. Kann jemand helfen, es in Twig umzuwandeln?So erstellen Sie eine dynamische Brotkrume nav in Zweig

<?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> 
+0

einige Fortschritte gemacht ... '{% set lastpage = 'blah' %}

    \t
  1. home
  2. \t
  3. {{ page }}
  4. \t
  5. {{lastpage}}
'Kann nicht arbeiten, wie wenn Sie den Namen der aktuellen Seite zu erhalten. Müssen Sie herausfinden, wie man eine 'foreach' Aussage auch tut ... – user3464091

+0

Diese http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb?rq=1 kann helfen? – user3464091

Antwort

0

Sie müssen nicht über diese Funktion in Twig konvertieren, kann man einfach Twig sagen Ihnen diese Funktion zur Verfügung in die Vorlage zu machen, indem twig erstreckt:

$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); 

Anschließend können Sie diese verwenden Funktion innerhalb twig wo immer Sie wollen:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8" /> 
    </head> 
    <body> 
     <nav> 
      {{ breadcrumbs() }} 
     </nav> 
    </body> 
</html>