Passing variable to Octobercms blogPosts component

你。 提交于 2020-01-25 08:33:22

问题


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 in onRun 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!