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

问题描述 投票:0回答:1

在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,但没有成功。任何想法,如何能做到这一点?

octobercms octobercms-plugins octobercms-backend
1个回答
1
投票

你需要使用组件的 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
}

如果有什么疑问请评论。

© www.soinside.com 2019 - 2024. All rights reserved.