0

On Octobercms theme settings i use a yaml file:

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

On index.htm page i use a partial featuredProducts of component with alias featuredProducts

On component featuredProducts viewBag i use this variable:

perPage = "{{ product_count }}"

I try to pass variable product_count from theme settings yaml file to components viewBag but without success. Any idea on how can do it?

Charis
  • 117
  • 2
  • 4
  • 12

1 Answers1

1

You need to use component's property functionality and it's onRender() method.

Page's markup section

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

Component's onRender() method: make sure to use onRender() method as props will be available there.

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
}

If any doubts please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • I managed to make it work using onInit method using $perPage = $this->theme->product_count; $this->page->components['featuredProducts']->setProperty('perPage', $perPage); What is the difference with your method? – Charis Jun 04 '20 at 17:40
  • my method is little general and standard means you can see its more simple and readable. also if you need to change theme variable name etc like `product_count ` you can do it from backend you do not need to open component file. also my method is more readable.. means next time you can see and get idea what is going on .. in your method its hard to understand where from this `perPage` is coming from ... ether way it depends what you prefer :) – Hardik Satasiya Jun 05 '20 at 07:11