【问题标题】:How to create a dynamic breadcrumb nav in twig如何在 Twig 中创建动态面包屑导航
【发布时间】:2016-07-26 13:55:20
【问题描述】:

我想在 Twig 中创建一个动态面包屑导航。 示例网址www.example.com/section/page 我想获取网址,将其拆分为面包屑,例如:home > section > page 我发现这个 php 代码有效。任何人都可以帮助将其转换为 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>

【问题讨论】:

  • 取得了一些进展...{% set lastPage = 'blah' %} &lt;ol class="breadcrumb"&gt; &lt;li&gt;&lt;a href="{{base}}/"&gt;home&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="{{base}}/{{page}}/"&gt;{{ page }}&lt;/a&gt;&lt;/li&gt; &lt;li&gt;{{ lastPage }}&lt;/li&gt; &lt;/ol&gt; 但是不知道如何获取当前页面的名称。还需要弄清楚如何做一个foreach 声明......
  • 这个stackoverflow.com/questions/2594211/… 可能有帮助吗?

标签: php templates dynamic twig breadcrumbs


【解决方案1】:

您不需要将该函数转换为Twig,您只需告诉Twig,通过扩展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);

然后,您可以在 twig 中的任意位置使用此函数:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-06
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多