section 组件定义了一个网站分区,并带有一个支持条目。此分区将在从后端面板预览条目时使用。
以下属性由该组件支持。
| Property | Description |
|---|---|
| handle | The handle of the entry blueprint. |
| identifier | Use this identifier key to look up the entry, supported values are slug, fullslug or id. Default: slug |
| value | Use this identifier value to use to look up the entry, optional. Leave empty to use the identifier key as the URL parameter name. Can be set to a hard coded value, or a custom parameter, eg: {{ :slug }} |
| isDefault | Make this the default page when previewing the entry. Default: true. |
以下为 Blog\Author 条目创建一个部分,并使用默认的 :slug URL 参数 定位它。 作者名称通过访问 {{ section.title }} Twig 变量作为标题显示。
url = "author/:slug"
[section]
handle = "Blog\Author"<h1>Posts by {{ section.title }}</h1>当同一页面使用多个区域时,可使用组件别名为页面分配不同的变量名。以下组件别名 author 使标题可通过 {{ author.title }} Twig 变量来代替。
[section author]
handle = "Blog\Author"<h1>Posts by {{ author.title }}</h1>默认标识符是 slug,这可以通过更改 identifier 属性来改变,例如,改用 id 字段来定位记录。注意页面 URL 也会随之更改,以使用 :id 参数名称。
url = "author/:id"
[section]
handle = "Blog\Author"
identifier = "id"您可以使用 value 属性硬编码标识符查找,例如,将其设置为 7 将显示采用静态查找的记录。
url = "author/ceo"
[section]
handle = "Blog\Author"
identifier = "id"
value = 7value 属性也接受外部参数,以下使用 foobar URL 参数来查找记录。
url = "author/:foobar"
[section]
handle = "Blog\Author"
identifier = "id"
value = "{{ :foobar }}"预览链接和使用页面查找器小组件不支持带有自定义值的标识符。
在大多数情况下,当无法找到记录时,你会希望显示一个 404 页面。这可以通过使用一个 {% if %} 语句结合 abort(404) 函数来实现。
{% if author is empty %}
{% do abort(404) %}
{% endif %}
abort()Twig 函数 用于在记录未找到时显示 404 页面。
当使用条目蓝图的内容组特性时,您可以使用 content_group 属性访问组代码。例如,一篇博文可能具有regular_post和markdown_post这样的内容组,其中内容的处理方式有所不同。
{% if post.content_group == 'markdown_post' %}
<!-- Render content as Markdown -->
{{ post.content|md }}
{% else %}
<!-- Render content as HTML -->
{{ post.content|raw }}
{% endif %}如果条目类型是structure,则会有一个fullslug可用属性。要在页面 URL 中使用完整 slug,应将其定义为通配符 URL 参数。组件的identifier属性应设置为fullslug
url = "/wiki/:fullslug*"
[section article]
handle = "Wiki\Article"
identifier = "fullslug"作为一种替代方法,我们建议针对 ID 并将其放在 URL 的末尾。这使得记录可以自由移动,而不会破坏您网站中的链接。以下示例展示了如何通过重定向任何以前使用的 URL 来实现这一点。
cmstemplate
url = "/wiki/:fullslug*/:id"
[section article]
handle = "Wiki\Article"
identifier = "id"{% if article is empty %}
{% do abort(404) %}
{% elseif article.fullslug != this.param.fullslug %}
{% do redirect(this|page({ fullslug: article.fullslug }), 301) %}
{% endif %}
<!-- Contents here -->:::
redirect()Twig 函数用于在 slug 不匹配时进行永久 301 重定向。|pageTwig 过滤器提供当前页面并重写fullslugURL 参数以匹配正确的值。