|页
这| page
filter 使用页面文件名(不带扩展名)作为参数创建指向页面的链接。例如,如果有 about.htm 页面,您可以使用以下代码生成指向它的链接:
<a href="{{ 'about' | page }}">About Us</a>
请记住,如果您从子目录引用页面,您应该指定子目录名称:
<a href="{{ 'contacts/about' | page }}">About Us</a>
NOTE: 这主题文档 有更多关于子目录使用的细节。
要从 PHP 部分访问指向特定页面的链接,您可以使用$this->pageUrl('page-name-without-extension')
:
==
<?php
function onStart() {
$this['newsPage'] = $this->pageUrl('blog/overview');
}
?>
==
{{ newsPage }}
您可以通过过滤空字符串来创建指向当前页面的链接:
<a href="{{ '' | page }}">Refresh page</a>
要获取 PHP 中当前页面的链接,您可以使用$this->pageUrl('')
用一个空字符串。
==
<?php
function onStart() {
$this['currentUrl'] = $this->pageUrl('');
}
?>
==
{{ currentUrl }}
反向路由
当链接到定义了 URL 参数的页面时, | page
filter 通过将数组作为第一个参数传递来支持反向路由。
url = "/blog/post/:post_id"
==
[...]
鉴于上述内容是在 CMS 页面文件中找到的post.htm 您可以使用以下方式链接到此页面:
<a href="{{ 'post' | page({ post_id: 10 }) }}">
Blog post #10
</a>
如果网站地址是https://example.com 上面的示例将输出以下内容:
<a href="https://example.com/blog/post/10">
Blog post #10
</a>
持久性 URL 参数
如果 URL 参数已经出现在环境中,则 | page
过滤器将自动使用它。
url = "/blog/post/:post_id"
url = "/blog/post/edit/:post_id"
如果有两页,post.htm 和post-edit.htm,定义了上述 URL 后,您可以链接到任一页面而无需定义post_id
范围。
<a href="{{ 'post-edit' | page }}">
Edit this post
</a>
当上面的标记出现在post.htm 页面,它会输出以下内容:
<a href="https://example.com/blog/post/edit/10">
Edit this post
</a>
这post_id
的价值10 是已知的,并且在整个环境中都存在。您可以通过将第二个参数作为false
:
<a href="{{ 'post' | page(false) }}">
Unknown blog post
</a>
或者通过定义不同的值:
<a href="{{ 'post' | page({ post_id: 6 }) }}">
Blog post #6
</a>