Skip to content

Latest commit

 

History

History
120 lines (83 loc) · 4.25 KB

Block.md

File metadata and controls

120 lines (83 loc) · 4.25 KB

Block

The <block> HTML element represents a block of content.

You must specify the tag name in the setter tagName().

Basic Usage

Instantiate the Block class using Block::widget().

$block = Block::widget()->tagName('body');

Or, block style instantiation.

<?= Block::widget()->tagName('body')->begin() ?>
    // ... content to be wrapped by `block` element
<?= Block::end() ?>

Setting Attributes

Use the provided methods to set specific attributes for the a element.

// setting class attribute
$block->class('container');

Or, use the attributes method to set multiple attributes at once.

$block->attributes(['class' => 'container', 'style' => 'background-color: #eee;']);

Adding Content

If you want to include content within the block tag, use the content method.

$block->content('MyContent');

Or, use begin() and end() methods to wrap content.

<?= Block::widget()->tagName('body')->begin() ?>
    My content
<?= Block::end() ?>

Rendering

Generate the HTML output using the render method, for simple instantiation.

$html = $block->render();

For block style instantiation, use the end() method, which returns the HTML output.

$html = Block::end();

Or, use the magic __toString method.

$html = (string) $block;

Common Use Cases

Below are examples of common use cases:

// adding multiple attributes
$block->class('external')->content('MyContent');

// using data attributes
$block->dataAttributes(['analytics' => 'trackClick']);

Explore additional methods for setting various attributes such as lang, name, style, title, etc.

Attributes

Refer to the Attribute Tests for comprehensive examples.

The following methods are available for setting attributes:

Method Description
attributes() Set multiple attributes at once.
class() Set the class attribute.
content() Set the content within the block element.
dataAttributes() Set multiple data-attributes at once.
id() Set the id attribute.
lang() Set the lang attribute.
name() Set the name attribute.
style() Set the style attribute.
title() Set the title attribute.

Custom methods

Refer to the Custom Methods Tests for comprehensive examples.

The following methods are available for customizing the HTML output:

Method Description
begin() Start the block element.
end() End the block element, and generate the HTML output.
render() Generates the HTML output.
tagName() Set the tag name for the block element.
widget() Instantiates the Block::class.