generated from yii-tools/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e866308
commit 699d9f8
Showing
6 changed files
with
521 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# HtmlBuilder helper | ||
|
||
The repository also provides a helper class called `HtmlBuilder` that you can use to generate `HTML` code. | ||
|
||
The `HtmlBuilder` class provides a wide range of methods for creating `HTML` elements, making it easy to generate | ||
`HTML` code programmatically using `PHP`. | ||
|
||
|
||
## Creating a new HTML element | ||
|
||
To create a new `HTML` element, you can use the `HtmlBuilder` with the `create` method. | ||
|
||
Allowed arguments are: | ||
|
||
- `tag` (string) - The tag name. | ||
- `content` (string) - The content of the tag. | ||
- `attributes` (array) - The attributes of the tag. | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Html\HtmlBuilder; | ||
?> | ||
|
||
<?= HtmlBuilder::create('div', 'Hello, World!', ['class' => 'container']) ?> | ||
``` | ||
|
||
Or you can use the `HtmlBuilder` with the `begin` and `end` methods. | ||
|
||
Allowed arguments for `begin` method are: | ||
|
||
- `tag` (string) - The tag name. | ||
- `attributes` (array) - The attributes of the tag. | ||
|
||
Allowed arguments for `end` method are: | ||
|
||
- `tag` (string) - The tag name. | ||
|
||
```php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Html\HtmlBuilder; | ||
?> | ||
|
||
<?= HtmlBuilder::begin('div', ['class' => 'container']) ?> | ||
Hello, World! | ||
<?= HtmlBuilder::end('div') ?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Tag | ||
|
||
The `<tag>` `HTML` element represents a generic tag. | ||
|
||
You must specify the tag name in the setter `tagName()`. | ||
|
||
## Basic Usage | ||
|
||
Instantiate the `Tag` class using `Tag::widget()`. | ||
|
||
```php | ||
$tag = Tag::widget()->tagName('div'); | ||
``` | ||
|
||
> Note: The `Tag` class is a generic class that can be used to create any `HTML` tag. You must specify the tag name | ||
using the `tagName()` method. | ||
|
||
## Setting Attributes | ||
|
||
Use the provided methods to set specific attributes for the a element. | ||
|
||
```php | ||
// setting class attribute | ||
$tag->class('container'); | ||
``` | ||
|
||
Or, use the `attributes` method to set multiple attributes at once. | ||
|
||
```php | ||
$tag->attributes(['class' => 'container', 'style' => 'background-color: #eee;']); | ||
``` | ||
|
||
## Adding Content | ||
|
||
If you want to include content within the `div` tag, use the `content` method. | ||
|
||
```php | ||
$tag->content('My content'); | ||
``` | ||
|
||
## Rendering | ||
|
||
Generate the `HTML` output using the `render` method, for simple instantiation. | ||
|
||
```php | ||
$html = $tag->render(); | ||
``` | ||
|
||
Or, use the magic `__toString` method. | ||
|
||
```php | ||
$html = (string) $tag; | ||
``` | ||
|
||
## Common Use Cases | ||
|
||
Below are examples of common use cases: | ||
|
||
```php | ||
// adding multiple attributes | ||
$tag->class('external')->content('My content'); | ||
|
||
// using data attributes | ||
$tag->dataAttributes(['analytics' => 'trackClick']); | ||
``` | ||
|
||
Explore additional methods for setting various attributes such as `lang`, `name`, `style`, `title`, etc. | ||
|
||
## Attributes | ||
|
||
Refer to the [Attribute Tests](https://github.com/php-forge/html/blob/main/tests/Tag/AttributeTest.php) 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 `div` 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. | | ||
| `tabIndex()` | Set the `tabindex` attribute. | | ||
| `title()` | Set the `title` attribute. | | ||
| `type()` | Set the `type` attribute. | | ||
| `value()` | Set the `value` attribute. | | ||
|
||
## Custom methods | ||
|
||
Refer to the [Custom Methods Tests](https://github.com/php-forge/html/blob/main/tests/Tag/CustomMethodTest.php) for | ||
comprehensive examples. | ||
|
||
The following methods are available for customizing the `HTML` output: | ||
|
||
| Method | Description | | ||
| -------------- | --------------------------------------------------------------------------------------------------- | | ||
| `render()` | Generates the `HTML` output. | | ||
| `tagName()` | Set the tag name for the `HTML` output. | | ||
| `template()` | Set the template for the `HTML` output. | | ||
| `tokenValues()`| Set the token values for the `HTML` output. | | ||
| `widget()` | Instantiates the `Body::class`. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Tests\Tag; | ||
|
||
use PHPForge\Html\Tag; | ||
use PHPForge\Support\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
*/ | ||
final class AttributeTest extends TestCase | ||
{ | ||
public function testAttributes(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span id="value"></span> | ||
HTML, | ||
Tag::widget()->attributes(['id' => 'value'])->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testClass(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span class="value"></span> | ||
HTML, | ||
Tag::widget()->class('value')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testContent(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span>content</span> | ||
HTML, | ||
Tag::widget()->content('content')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testDataAttributes(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span data-value="value"></span> | ||
HTML, | ||
Tag::widget()->dataAttributes(['value' => 'value'])->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testId(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span id="value"></span> | ||
HTML, | ||
Tag::widget()->id('value')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testLang(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span lang="value"></span> | ||
HTML, | ||
Tag::widget()->lang('value')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testName(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span name="value"></span> | ||
HTML, | ||
Tag::widget()->name('value')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testStyle(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span style="value"></span> | ||
HTML, | ||
Tag::widget()->style('value')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testTabindex(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span tabindex="1"></span> | ||
HTML, | ||
Tag::widget()->tabindex(1)->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testTitle(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<span title="value"></span> | ||
HTML, | ||
Tag::widget()->title('value')->tagName('span')->render() | ||
); | ||
} | ||
|
||
public function testType(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<button type="button"></button> | ||
HTML, | ||
Tag::widget()->tagName('button')->type('button')->render() | ||
); | ||
} | ||
|
||
public function testValue(): void | ||
{ | ||
Assert::equalsWithoutLE( | ||
<<<HTML | ||
<input value="value"> | ||
HTML, | ||
Tag::widget()->tagName('input')->value('value')->render() | ||
); | ||
} | ||
} |
Oops, something went wrong.