根据页面显示不同的侧边栏内容 url - October CMS

Display different sidebar content based on page url - October CMS

我有 2 页 index.htm 和 links.htm 以及部分 sidebar.htm。

我正在尝试根据 index.htm 或 links.htm 是否处于活动状态让我的边栏显示不同的内容。

通读文档,您似乎可以使用 param.tab 来完成此操作,但我无法让它工作,并且不确定这是否是完成此操作的最佳方式,因为它是部分的.

sidebar.htm

description = "sidebar"
url = "/sidebar/:{% this.page.url %}"
==
{% if this.param.tab == 'links' %}
Display this if page = links
{% elseif this.param.tab == 'index' %}
Display this is page = index
{% endif %}

在 blade 中我使用了 - @elseif(isset($guidesslugs) && $guidesslugs->guidesslug == 'area-guide') 但我不确定如何使用 twig 完成同样的事情或者是否可能部分使用 param.tab?

有多种方法。


首先,partials 可能没有 url 参数进入配置部分。

The Configuration section is optional for partials and can contain the optional description parameter which is displayed in the back-end user interface.

Partials - Introduction


如果侧边栏有基于同一页面的不同内容,您可以使用 param。示例:

pages/home.htm

url = "/home/:type"
==
{% partial 'sidebar' type=this.param.type %}

partials/sidebar.htm

{% if type == 'foo' %}
  {# foo type #}
{% elseif type == 'bar' %}
  {# bar type #}
{% else %}
  {# anything... #}
{% endif %}

当包含部分时,您可以传递变量,在这种情况下,我们将 type 作为 this.param.type 值注入。您还可以在 partial:

中访问 this.param (全局)变量

partials/sidebar.htm

{% if this.param.type == 'foo' %}
  {# foo type #}
{% endif %}

如果我理解你的问题,你有两个不同的页面,每个页面都有自己独特的 url 所以看起来不像 param 属性.


使用this.page变量。

您可以访问页面 url 定义到页面配置部分,方法是:

{{ this.page.url }} 

这个 属性 似乎对 docs 隐藏了。

所以考虑你的结构:

pages/home

url = "/home"
layout = "default"
==
{% partial 'sidebar' %}

pages/links

url = "/links"
layout = "default"
==
{% partial 'sidebar' %}

partials/sidebar

{% if this.page.url == '/home' %}
  {# home page #}
{% elseif this.page.url == '/links' %}
  {# links page #}
{% else %}
  {# other pages #}
{% endif %}

正如我所说,有很多方法可以通过定义变量、将参数传递给 partial 和其他方法来实现。