问题
On Octobercms blogPosts component i want to pass a variable. I want to change postsPerPage value using:
{variable name="blog_postnumber" label="postnumber" tab="postnumber" type="number"}{/variable}
on static layout. So i want to be able to change postsPerPage component value with this field on static Pages.
I use a partial with blogPosts component. On component postsPerPage field i insert the variable.
postsPerPage = "{{ blog_postnumber }}"
Then i try to insert a number in my field on my static page but is not working. Any idea on how can i pass variables on component?
回答1:
Its really tricky that your passed variable/property to component will be used or not
. if that property is fetched in component's onRender
method it will be used. if its used on onRun
then it will not be used. ( its OctoberCMS Design )
for
blogPosts
component its inonRun
so when you pass property like this{% component 'blogPosts' postsPerPage="2" %}
it will not be used, But To handle this we need other work-around
(1) for the component
blogPosts
you included in partial you need to use property from param. check screen shot.
(2) In you Static layout's Code section you need to add this code
public function onStart() {
$statiPage = $this->page->apiBag['staticPage'];
// default posts per page
$defaultBlogPost = 5;
if(isset($statiPage->viewBag['blog_postnumber'])) {
// fetching value from the page field
$defaultBlogPost = intVal($statiPage->viewBag['blog_postnumber']);
$router = $this->getRouter();
// combine with existing params
$router->setParameters(['myBlogPerPage' => $defaultBlogPost] + $router->getParameters());
}
}
And in markup part field
{variable name="blog_postnumber" label="postnumber" tab="postnumber" type="number"}{/variable}
(3) Now you can start using it set variable value we set it by default 5 in code section, to override it specify value in page's postnumber field section
It will start working.
If any doubts please comment.
来源:https://stackoverflow.com/questions/55347814/passing-variable-to-octobercms-blogposts-component