0 follower

Final Class Yiisoft\Html\Tag\CustomTag

InheritanceYiisoft\Html\Tag\CustomTag » Yiisoft\Html\Tag\Base\Tag
ImplementsYiisoft\Html\NoEncodeStringableInterface
Uses TraitsYiisoft\Html\Tag\Base\TagContentTrait

Custom HTML tag.

Protected Properties

Hide inherited properties

Property Type Description Defined By
$attributes array Yiisoft\Html\Tag\Base\Tag

Constants

Hide inherited constants

Constant Value Description Defined By
TYPE_AUTO 0 Yiisoft\Html\Tag\CustomTag
TYPE_NORMAL 1 Yiisoft\Html\Tag\CustomTag
TYPE_VOID 2 Yiisoft\Html\Tag\CustomTag
VOID_ELEMENTS [ 'area' => 1, 'base' => 1, 'br' => 1, 'col' => 1, 'command' => 1, 'embed' => 1, 'hr' => 1, 'img' => 1, 'input' => 1, 'keygen' => 1, 'link' => 1, 'meta' => 1, 'param' => 1, 'source' => 1, 'track' => 1, 'wbr' => 1, ] List of void elements. These only have a start tag; end tags must not be specified. {@see https://www.w3.org/TR/html-markup/syntax.html#void-element} Yiisoft\Html\Tag\CustomTag

Method Details

Hide inherited methods

__toString() public method
public string __toString ( )

                final public function __toString(): string
{
    return $this->render();
}

            
addAttributes() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::addAttributes()

Add a set of attributes to existing tag attributes.

Same named attributes are replaced.

public Yiisoft\Html\Tag\CustomTag addAttributes ( array $attributes )
$attributes array

Name-value set of attributes.

                final public function addAttributes(array $attributes): static
{
    $new = clone $this;
    $new->attributes = array_merge($new->attributes, $attributes);
    return $new;
}

            
addClass() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::addClass()

Add one or more CSS classes to the tag.

public Yiisoft\Html\Tag\CustomTag addClass ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or many CSS classes.

                final public function addClass(BackedEnum|string|null ...$class): static
{
    $new = clone $this;
    Html::addCssClass($new->attributes, $class);
    return $new;
}

            
addContent() public method
public Yiisoft\Html\Tag\CustomTag addContent ( string|\Stringable $content )
$content string|\Stringable

Tag content.

                final public function addContent(string|Stringable ...$content): static
{
    $new = clone $this;
    $new->content = array_merge($new->content, array_values($content));
    return $new;
}

            
addStyle() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::addStyle()

Add CSS styles to the tag.

See also Yiisoft\Html\Html::addCssStyle().

public Yiisoft\Html\Tag\CustomTag addStyle ( string|string[] $style, boolean $overwrite true )
$style string|string[]

The new style string (e.g. 'width: 100px; height: 200px') or array (e.g. ['width' => '100px', 'height' => '200px']).

$overwrite boolean

Whether to overwrite existing CSS properties if the new style contain them too.

                final public function addStyle(array|string $style, bool $overwrite = true): static
{
    $new = clone $this;
    Html::addCssStyle($new->attributes, $style, $overwrite);
    return $new;
}

            
after() protected method
protected string after ( )

                protected function after(): string
{
    return '';
}

            
attribute() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::attribute()

Set attribute value.

public Yiisoft\Html\Tag\CustomTag attribute ( string $name, mixed $value )
$name string

Name of the attribute.

$value mixed

Value of the attribute.

                final public function attribute(string $name, mixed $value): static
{
    $new = clone $this;
    $new->attributes[$name] = $value;
    return $new;
}

            
attributes() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::attributes()

Replace attributes with a new set.

public Yiisoft\Html\Tag\CustomTag attributes ( array $attributes )
$attributes array

Name-value set of attributes.

                final public function attributes(array $attributes): static
{
    $new = clone $this;
    $new->attributes = $attributes;
    return $new;
}

            
before() protected method
protected string before ( )

                protected function before(): string
{
    return '';
}

            
class() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::class()

Replace current tag CSS classes with a new set of classes.

public Yiisoft\Html\Tag\CustomTag class ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or many CSS classes.

                final public function class(BackedEnum|string|null ...$class): static
{
    $new = clone $this;
    unset($new->attributes['class']);
    Html::addCssClass($new->attributes, $class);
    return $new;
}

            
close() public method

public string close ( )
return string

Closing tag.

                public function close(): string
{
    return '</' . $this->getName() . '>';
}

            
content() public method
public Yiisoft\Html\Tag\CustomTag content ( string|\Stringable $content )
$content string|\Stringable

Tag content.

                final public function content(string|Stringable ...$content): static
{
    $new = clone $this;
    $new->content = array_values($content);
    return $new;
}

            
doubleEncode() public method
public Yiisoft\Html\Tag\CustomTag doubleEncode ( boolean $doubleEncode )
$doubleEncode boolean

Whether already encoded HTML entities in tag content should be encoded. Defaults to true.

                final public function doubleEncode(bool $doubleEncode): static
{
    $new = clone $this;
    $new->doubleEncode = $doubleEncode;
    return $new;
}

            
encode() public method
public Yiisoft\Html\Tag\CustomTag encode ( boolean|null $encode )
$encode boolean|null

Whether to encode tag content. Supported values:

  • null: stringable objects that implement interface {@see \Yiisoft\Html\NoEncodeStringableInterface} are not encoded, everything else is encoded;
  • true: any content is encoded;
  • false: nothing is encoded. Defaults to null.

                final public function encode(?bool $encode): static
{
    $new = clone $this;
    $new->encode = $encode;
    return $new;
}

            
generateContent() protected method
protected string generateContent ( )
return string

Obtain tag content considering encoding options {@see \Yiisoft\Html\Tag\Base\encode()}.

                final protected function generateContent(): string
{
    $content = '';
    foreach ($this->content as $item) {
        if ($this->encode || ($this->encode === null && !($item instanceof NoEncodeStringableInterface))) {
            $item = Html::encode($item, $this->doubleEncode);
        }
        $content .= $item;
    }
    return $content;
}

            
getName() protected method

protected string getName ( )

                protected function getName(): string
{
    return $this->name;
}

            
id() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::id()

Set tag ID.

public Yiisoft\Html\Tag\CustomTag id ( string|null $id )
$id string|null

Tag ID.

                final public function id(?string $id): static
{
    $new = clone $this;
    $new->attributes['id'] = $id;
    return $new;
}

            
name() public static method

Create a tag instance with the name provided.

public static self name ( string $name )
$name string

Name of the tag.

                public static function name(string $name): self
{
    return new self($name);
}

            
normal() public method

Set type of the tag as normal.

Normal tags have both open and close parts.

public self normal ( )

                public function normal(): self
{
    $new = clone $this;
    $new->type = self::TYPE_NORMAL;
    return $new;
}

            
open() public method

public string open ( )
return string

Opening tag.

                public function open(): string
{
    return '<' . $this->getName() . $this->renderAttributes() . '>';
}

            
prepareAttributes() protected method
protected void prepareAttributes ( )

                protected function prepareAttributes(): void {}

            
removeStyle() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::removeStyle()

Remove CSS styles from the tag.

See also Yiisoft\Html\Html::removeCssStyle().

public Yiisoft\Html\Tag\CustomTag removeStyle ( string|string[] $properties )
$properties string|string[]

The CSS properties to be removed. You may use a string if you are removing a single property.

                final public function removeStyle(string|array $properties): static
{
    $new = clone $this;
    Html::removeCssStyle($new->attributes, $properties);
    return $new;
}

            
render() public method
public string render ( )

                final public function render(): string
{
    return $this->before() . $this->renderTag() . $this->after();
}

            
renderAttributes() protected method

Defined in: Yiisoft\Html\Tag\Base\Tag::renderAttributes()

Render the current tag attributes.

See also Yiisoft\Html\Html::renderTagAttributes().

protected string renderAttributes ( )

                final protected function renderAttributes(): string
{
    $this->prepareAttributes();
    return Html::renderTagAttributes($this->attributes);
}

            
renderTag() protected method

protected string renderTag ( )

                protected function renderTag(): string
{
    $isVoid = $this->type === self::TYPE_VOID
        || ($this->type === self::TYPE_AUTO && isset(self::VOID_ELEMENTS[strtolower($this->name)]));
    return $isVoid ? $this->open() : ($this->open() . $this->generateContent() . $this->close());
}

            
unionAttributes() public method

Defined in: Yiisoft\Html\Tag\Base\Tag::unionAttributes()

Union attributes with a new set.

public Yiisoft\Html\Tag\CustomTag unionAttributes ( array $attributes )
$attributes array

Name-value set of attributes.

                final public function unionAttributes(array $attributes): static
{
    $new = clone $this;
    $new->attributes += $attributes;
    return $new;
}

            
void() public method

Set type of the tag as void.

Void tags should be self-closed right away.

public self void ( )

                public function void(): self
{
    $new = clone $this;
    $new->type = self::TYPE_VOID;
    return $new;
}