Generate a link to a CMS page
2
If you've got the following cms page called blog-post.htm
in your theme...
title = "Blog post"
url = "/blog/:slug"
layout = "default"
is_hidden = 0
[blogPost]
==
{% component 'blogPost' %}
... you can use the following methods to generate a link to it:
In Twig
Pass the name of the cms page file to the page
filter. Optionally pass along any URL parameters.
{# Example with parameters #}
{{ 'blog-post' | page({slug: 'slug-of-the-post'}) }}
{# Example without parameters #}
{{ 'blog' | page }}
{# Note: If you are linking to a Winter.Pages page, use the staticPage filter #}
{{ 'static-blog' | staticPage }}
In PHP
Use the \Cms\Classes\Controller
class to generate the URL.
// In a page or component context you can access the
// controller via $this->controller
$this->controller->pageUrl('blog-post', ['slug' => 'slug-of-the-post']);
// If you don't have access to a controller you can
// easily create a new instance on the fly.
$url = (new \Cms\Classes\Controller)->pageUrl('blog-post', ['slug' => 'slug-of-the-post']);
// Or use the Page class helper
\Cms\Classes\Page::url('blog-post', ['slug' => 'slug-of-the-post'])
There are no comments yet
Be the first one to comment