0

我想在 Grav 中创建一个页面,其中有一个内容区域和一个侧边栏区域。

我想在 modules.md 根模板上使用,我在其中引用了一个循环并显示内容和侧边栏模块的模板。

我的问题是:如何区分内容和侧边栏?

我的模板如下所示:

 {% block body %}
   {% block content %}
   <section id="info">
     <div class="container">
      <div class="row">
        <div class="col-sm-12">
          <div class="row">
            <div class="col-sm-8">
              <div id="content" class="site-content bg">
                <h2 class="heading">{{ page.title }}</h2>
                {{ page.content }}
                {% for module in page.collection() %}
                  {{ module.content }}
                {% endfor %}
              </div>
            </div>
            <div id="sidebar" class="col-sm-4 sidebar-inner">
              {% for module in page.collection() %}
                {{ module.content }}
              {% endfor %}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  </section>
 {% endblock %}
 {% endblock %}

我试图实现的是在两个 page.collections 上使用过滤器,以便在一种情况下只使用“内容”(我在这里猜测 taxonomy.tag),而在另一种情况下只使用侧边栏。我可以接受命名约定(侧边栏模块有一个固定的前缀),但我想不通。

4

2 回答 2

0

好的,我带来了一个技巧:我在模块的文件夹名称中包含“侧边栏”,并在 module.folder 名称上进行过滤。不过,我相信有更好的解决方案!

<section id="info">
  <div class="container">
    <div class="row">
      <div class="col-sm-12">
        <div class="row">
          <div class="col-sm-8">
            {% if page.content %}
              <div id="content" class="site-content bg">
                <h2 class="heading">{{ page.title }}</h2>
                {{ page.content }}
              </div>
            {% endif %}

            {% for module in page.collection() %}
              {% if 'sidebar' not in module.folder %}
                {{ module.content }}
              {% endif %}
            {% endfor %}
          </div>
          <div id="sidebar" class="col-sm-4 sidebar-inner">
            {% for module in page.collection() %}
              {% if 'sidebar' in module.folder %}
                {{ module.content }}
              {% endif %}
            {% endfor %}
          </div>
        </div>
      </div>
    </div>
  </div>
于 2017-10-23T01:22:11.790 回答
0

你能做的(我认为)是在模块的 fromtmatter 中设置一个值

sidebar: true

然后你可以过滤你的集合中的那个值,比如

{% for module in page.collection() %}
   {% if page.header.sidebar %}
      {{ module.content }}
   {% endif %}
{% endfor %}
于 2017-11-09T14:32:49.213 回答