October CMS resources and help articles

Simple and to the point. Optimized by the community.

Component helper methods to set variables

3
by Samuell, last modified on August 12th, 2019

Creating helper class that we can use to extend other components.

BaseComponent.php

use Cms\Classes\ComponentBase;

abstract class BaseComponent extends ComponentBase
{
    protected function setVar($name, $value)
    {
        return $this->$name = $this->page[$name] = $value;
    }

    protected function setPageVar($name, $value)
    {
        return $this->page[$name] = $value;
    }
}

Usage of methods:

  • setVar - if we want to set component variable and page variable
  • setPageVar - or if we want to set only page variable

Then we can extend one of our Components like this.

class ProductsList extends BaseComponent
{
    public $myVariable;

    public function init()
    {
        $this->setVar('myVariable', 'value');
        $this->setPageVar('myPageVariable', 'value2');
    }
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?