Homepage
Main navigation
Main content
Additional information
October Tricks
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
October CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
There are many ways to display a model's data on a cms page. This trick describes two: ### Via page execution lifecycle ```twig url = "/blog/:slug" == use Acme\Blog\Models\Post; function onStart() { $this['post'] = Post::where('slug', $this->param('slug'))->first(); } == <h2>{{ post.title }}</h2> <div>{{ post.content }}</div> ``` ### Via a custom component Register your custom component in your Plugin.php. ```php // Plugin.php public function registerComponents() { return [ Acme\Blog\Components\BlogDetail::class => 'blogDetail', ]; } ``` Create the `blogDetail` component as `components/BlogDetail.php` ```php namespace Acme\Blog\Components; use Cms\Classes\ComponentBase; class BlogDetail extends ComponentBase { public $blog; public function defineProperties() { return [ 'slug' => [ 'type' => 'string', 'title' => 'Slug', ] ]; } public function onRun() { $this->blog = $this->page['blog'] = Post::where('slug', $this->property('slug'))->first(); } } ``` Finally, place the component on your CMS page. ```twig url = "/blog/:slug" [blogDetail] slug = "{{ :slug }}" == <h2>{{ blogDetail.post.title }}</h2> <div>{{ blogDetail.post.content }}</div> {# Since we set $this->page['blog'] the post is also available via the {{ post }} variable directly. @see https://octobertricks.com/tricks/access-component-properties-page-or-partial #} ```
References
Add additional online resources to your trick
×
Name
URL
×
Name
URL
×
Name
URL
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline