|page 过滤器使用页面文件名(不带扩展名)作为参数,创建一个页面链接。例如,如果存在 about.htm 页面,您可以使用以下代码为其生成链接:
<a href="{{ 'about'|page }}">About Us</a>请记住,如果你引用子目录中的页面,你应该指定子目录的名称:
<a href="{{ 'contacts/about'|page }}">About Us</a>该 主题文档 提供了有关子目录使用的更多详细信息。
要从 PHP 部分访问某个页面的链接,您可以使用 $this->pageUrl('page-name-without-extension')。
<?
function onStart()
{
$this['newsPage'] = $this->pageUrl('blog/overview');
}
?>{{ newsPage }}你可以通过过滤 this 变量来创建指向当前页面的链接。
<a href="{{ this|page }}">Refresh page</a>要获取 PHP 中当前页面的链接,请调用不带任何参数的 $this->pageUrl() 方法。
<?
function onStart()
{
$this['currentUrl'] = $this->pageUrl();
}
?>{{ currentUrl }}当链接到定义了 URL 参数的页面时,|page 过滤器通过将一个数组作为第一个参数传递来支持反向路由。
url = "/blog/post/:post_id"[...]鉴于上述内容位于 CMS 页面文件 post.htm 您可以使用以下方式链接到此页面:
<a href="{{ 'post'|page({ post_id: 10 }) }}">
Blog post #10
</a>如果网站地址是 https://octobercms.com 上述示例将输出以下内容:
<a href="https://octobercms.com/blog/post/10">
Blog post #10
</a>如果 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://octobercms.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>