9阅网

您现在的位置是:首页 > 知识 > 正文

知识

octobercms - 从主题设置中传递Viewbag变量到组件。

admin2022-10-24知识21

在Octobercms主题设置中,我使用了一个yaml文件。

product_count:
    label: Number
    type: number
    span: left
    tab: Index
    default: 3

在index.htm页面上,我使用了一个局部的。featuredProducts 的组件,别名为 featuredProducts

关于组件 featuredProducts viewBag我使用这个变量。

perPage = "{{ product_count }}"

我试图通过变量 product_count 从主题设置yaml文件到组件viewBag,但没有成功。任何想法,如何能做到这一点?



【回答】:

你需要使用组件的 property 功能和它的 onRender() 方法。

页面的标记部分

{% component 'featuredProducts' perPage=this.theme.product_count %}

组件的 onRender() 方法。一定要用 onRender() 方法,因为那里会有道具。

public function onRender() {
    // with default value of 3, if theme value is not already set
    $perPage = $this->property('perPage', 3); 

    // now use $perPage to fetch records
    $this->page['items'] = SomeModel::limit($perPage)->get();

    // items will be available in markup for use
}

如果有什么疑问请评论。