D
D
Dmitry Kim2016-01-24 16:07:02
Yii
Dmitry Kim, 2016-01-24 16:07:02

Is it possible in Yii2 to check that the same widget has already been initialized in a view?

There is a specific widget that, when launched, registers the necessary asset and renders the necessary html code. An example code is:

public function run() {
  Blueimp_BootstrapImageGalleryAsset::register($this->view);
  return $this->renderItems()."\n".$this->renderControls();
}

If there are several such widgets on the page, it turns out that the code will be duplicated. How can you check that this widget has already been on the page? You need something like:
public function run() {
  $html = [];
  if (!$this->view->HAVE_VIDGET($this)){
    Blueimp_BootstrapImageGalleryAsset::register($this->view);
    $html[] = $this->renderControls();
  }
  $html[] = $this->renderItems();
  return implode("\n", $html);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2016-01-24
@bitver

The answer to the question is static properties.
And the fact that you register assets 100500 times is okay, they will eventually be used 1 time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question