0

在 Octobercms 主题设置中,我使用 yaml 文件:

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

在 index.htm 页面上,我使用了部分featuredProducts带有别名的组件featuredProducts

在组件featuredProductsviewBag 我使用这个变量:

perPage = "{{ product_count }}"

我尝试将product_count主题设置 yaml 文件中的变量传递给组件 viewBag 但没有成功。关于如何做到这一点的任何想法?

4

1 回答 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
}

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

于 2020-05-28T13:21:59.580 回答