|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 是 已 知 并 已 持久化 跨 各 环境。 您 可 禁用 此 功能 通过 传递 第 2个 参数 为 false:
<a href="{{ 'post'|page(false) }}">
Unknown blog post
</a>或者通过定义一个不同的值:
<a href="{{ 'post'|page({ post_id: 6 }) }}">
Blog post #6
</a>