sectionsOpen)) { return false; } View::start(); $this->sectionsOpen[$id] = $render; return true; } /** * Закрываем секцию * @return bool */ public function sectionStop(): bool { if (empty($this->sectionsOpen)) { return false; } $content = View::stop(); $render = end($this->sectionsOpen); $id = key($this->sectionsOpen); if ($render) { $this->sectionRender($id, $content); } else { $this->sectionSet($id, $content); } array_pop($this->sectionsOpen); return true; } /** * Закрываем секцию * @see sectionStop * @return bool */ public function sectionEnd(): bool { return $this->sectionStop(); } /** * Расширяем содержимое секции * @param string $id ID секции * @param string|callable $extension содержимое * @param bool $after добавить после имеющегося содержимого * @param int|null $priority приоритет вызова * @return Hook|bool */ public function sectionExtend(string $id, $extension, bool $after = true, ?int $priority = null) { return bff::hooks()->onBlockSection(static::class, $id, function ($content, $block) use ($extension, $after) { if (is_callable($extension)) { return call_user_func($extension, $content, $block); } if ($after) { $content .= $extension; } else { $content = $extension . $content; } return $content; }, $priority); } /** * Устанавливаем содержимое секции * @param string $id ID секции * @param string $content содержимое секции * @return void */ public function sectionSet(string $id, $content) { $this->sections[$id] = $content; } /** * Получаем содержимое секции * @param string $id ID секции * @param string|Closure $default содержимое секции по умолчанию * @return mixed */ public function section(string $id, $default = '') { if (array_key_exists($id, $this->sections)) { $content = $this->sections[$id]; } elseif ($default instanceof Closure) { $content = call_user_func($default, $this); } else { $content = $default; } return $this->sectionRender( $id, $content, true ); } /** * Отрисовываем секцию * @param string $id ID секции * @param string $content содержимое секции * @param bool $return возвращать содержимое * @return mixed|void */ public function sectionRender(string $id, string $content = '', bool $return = false) { $content = bff::filter('view.section.' . $id, $content, $this); if ($return) { return $content; } echo $content; } }