将变量传递给Octobercms blogPosts组件 [英] Passing variable to Octobercms blogPosts component

查看:89
本文介绍了将变量传递给Octobercms blogPosts组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Octobercms blogPosts组件上,我想传递一个变量.我想使用以下方式更改postsPerPage值:

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}

关于静态布局.因此,我希望能够使用静态页面上的此字段来更改postsPerPage组件值.

on static layout. So i want to be able to change postsPerPage component value with this field on static Pages.

我在BlogPosts组件中使用了部分.在组件postsPerPage字段上,我插入变量.

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?

推荐答案

将变量/属性传递给组件will be used or not确实很棘手.如果在组件的onRender方法中获取了该属性,则将使用它.如果在onRun上使用了它,则将不使用它. (其OctoberCMS设计)

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 )

用于blogPosts组件,位于onRun中,因此当您传递类似{% component 'blogPosts' postsPerPage="2" %}的属性时,将不会使用它,但是要处理此问题,我们需要其他解决方法

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


对于部分中包含的组件blogPosts

(1),您需要使用param中的属性.检查屏幕截图.

(1) for the component blogPosts you included in partial you need to use property from param. check screen shot.

(2)在您的静态布局的代码部分中,您需要添加此代码

(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());
    }               
}

在标记部分字段中

{variable name="blog_postnumber" label="postnumber" tab="postnumber" type="number"}{/variable}

(3)现在,您可以开始使用它设置变量值了,我们默认在代码部分设置 5 ,以覆盖它,在页面的postnumber字段部分指定值

(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

它将开始工作.

如有任何疑问,请发表评论.

If any doubts please comment.

这篇关于将变量传递给Octobercms blogPosts组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆