diff --git a/src/BaseListView.php b/src/BaseListView.php index 66998954d..7d5a9ed8d 100644 --- a/src/BaseListView.php +++ b/src/BaseListView.php @@ -115,6 +115,8 @@ abstract class BaseListView extends Widget */ private ?string $containerTag = 'div'; private array $containerAttributes = []; + private string $prepend = ''; + private string $append = ''; /** * @psalm-var non-empty-string|null @@ -248,6 +250,30 @@ final public function containerAttributes(array $attributes): static return $new; } + /** + * Returns a new instance with HTML content to be added after the opening container tag. + * + * @param string|Stringable $prepend The HTML content to be prepended. + */ + final public function prepend(string|Stringable $prepend): static + { + $new = clone $this; + $new->prepend = (string) $prepend; + return $new; + } + + /** + * Returns a new instance with HTML content to be added before the closing container tag. + * + * @param string|Stringable $append The HTML content to be appended. + */ + final public function append(string|Stringable $append): static + { + $new = clone $this; + $new->append = (string) $append; + return $new; + } + /** * Return a new instance with the empty text. * @@ -543,6 +569,13 @@ final public function render(): string ) ); + if ($this->prepend !== '') { + $content = $this->prepend . "\n" . $content; + } + if ($this->append !== '') { + $content .= "\n" . $this->append; + } + return $this->containerTag === null ? $content : Html::tag($this->containerTag, "\n" . $content . "\n", $this->containerAttributes)