【问题标题】:Multiple Header Styles In Timber/Twig木材/树枝中的多种标题样式
【发布时间】:2016-01-15 02:50:21
【问题描述】:

我正在使用 Timber for Wordpress 编写一个主题,它使用 twig 模板引擎。

我目前在三个页面上的标题部分需要不同的数据。

  1. 主页 > BG 滑块和标题
  2. 博客页面 > 背景和固定帖子
  3. 其他页面 > 常规 BG 图片

每个页面标题的导航也应该是相同的。

我不确定使用 twig 解决此问题的最佳方法。

在我的 base.twig 中,我目前拥有:

{% block header %}
            <div class="wrapper">
                <h1 class="hdr-logo" role="banner">
                    <a class="hdr-logo-link" href="/" rel="home">{{site.name}}</a>
                </h1>
                <nav id="nav-main" class="nav-main" role="navigation">
                    <ul class="nav">
                    {% for item in menu.get_items %}
                        <li class="nav-item {{item.classes | join(' ')}}">
                            <a class="nav-link" href="{{item.get_link}}">{{item.title}}</a>
                            {% if item.get_children %}
                                <ul class="nav nav-drop">
                                {% for child in item.get_children %}
                                    <li class="nav-drop-item">
                                        <a class="nav-link" href="{{child.get_link}}">{{child.title}}</a>
                                    </li>
                                {% endfor %}
                                </ul>
                            {% endif %}
                        </li>
                    {% endfor %}
                    </ul>
                </nav><!-- #nav -->
            </div>
        {% endblock %}

我可以将此块复制并粘贴到每个页面上,即添加到

home.twig、blog.twig 和 other.twig

但我不断地重复自己,感觉不对。我需要在每个页面上浏览此内容,而无需每次都重复导航。

有人对如何修复有建议吗?

【问题讨论】:

    标签: php wordpress twig


    【解决方案1】:

    你想要的是 Twig 宏和/或包含

    Includes

    可用于在模板中包含另一个 .twig 文件。 您也可以将变量添加到包含中,但默认情况下包含中的代码可以访问全局上下文。例如。所有模板变量。

    {# template.html will have access to the variables from the current context and the additional ones provided #}
    {% include 'template.html' with {'foo': 'bar'} %}
    
    {% set vars = {'foo': 'bar'} %}
    {% include 'template.html' with vars %}
    

    Macros

    与全局上下文隔离,只能使用您提供给它的变量/参数。

    {# defineit #}
    {% macro input(name, value, type, size) %}
        <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
    {% endmacro %}
    
    {# then calling it #}
    <p>{{ _self.input('username') }}</p>
    

    使用 Includes 和 Macros,当您将自身包含在循环中时,您可以轻松地创建无限嵌套的树。

    包含嵌套树的示例:

      {# content of tree.twig file for include#}
      {% for item in menu.get_items %}
            <li class="nav-item {{item.classes | join(' ')}}">
                <a class="nav-link" href="{{item.get_link}}">{{item.title}}</a>
                {% if item.get_children %}
                    <ul class="nav nav-drop">
                    {# recursion #}
                    {% include 'tree.twig' with { menu : item.get_children } %}
                    </ul>
                {% endif %}
            </li>
        {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 2021-11-20
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      相关资源
      最近更新 更多