|
| 1 | +# Yii |
| 2 | +Adding this library as a dependency to your Yii application allows you to either use the pre-made templates, and to use the DOM\HTMLDocument and DOM\HTMLElement compliant PHP classes for all HTML5 elements, including immutable attribute Enums for validation. |
| 3 | + |
| 4 | +## Installation |
| 5 | +```bash |
| 6 | +composer require vardumper/extended-htmldocument |
| 7 | +``` |
| 8 | +## Use in PHP DOM\HTMLDocument |
| 9 | + |
| 10 | +```php |
| 11 | +use Html\Delegator\HTMLDocumentDelegator as HTMLDocument; |
| 12 | +use Html\Element\Inline\Anchor; |
| 13 | + |
| 14 | +$dom = HTMLDocument::createEmpty() |
| 15 | +echo (string) Anchor::create($dom) |
| 16 | + ->setClass('secondary') |
| 17 | + ->setRel(RelEnum::NOFOLLOW) |
| 18 | + ->setHref('https://google.com') |
| 19 | + ->setTitle('Google it') |
| 20 | + ->setContent('Click me'); |
| 21 | +// produces: <a class="secondary" href="https://google.com" rel="nofollow" title="Google it">Click me</a> |
| 22 | +``` |
| 23 | + |
| 24 | +## Configure Twig for use of pre-made Twig templates |
| 25 | +To configure the path to this libraries' pre-made Twig templates, you need to add an additional path to the filesystem loader, usually in `config/common/di/view-twig.php`: |
| 26 | + |
| 27 | +```php |
| 28 | +use Twig\Environment; |
| 29 | +use Twig\Loader\FilesystemLoader; |
| 30 | +use Twig\Loader\LoaderInterface; |
| 31 | +use Yiisoft\Aliases\Aliases; |
| 32 | +use Yiisoft\Definitions\Reference; |
| 33 | + |
| 34 | +return [ |
| 35 | + LoaderInterface::class => static function (Aliases $aliases) { |
| 36 | + $loader = new FilesystemLoader($aliases->get('@views')); |
| 37 | + $loader->addPath($aliases->get('@vendor/vardumper/extended-htmldocument/templates/twig'), 'html'); // <-- add another path and alias |
| 38 | + return $loader; |
| 39 | + }, |
| 40 | + Environment::class => [ |
| 41 | + '__construct()' => [ |
| 42 | + 'loader' => Reference::to(LoaderInterface::class), |
| 43 | + 'options' => $params['yiisoft/view-twig']['options'], |
| 44 | + ], |
| 45 | + ], |
| 46 | +]; |
| 47 | +``` |
| 48 | + |
| 49 | +Now that Twig finds the new templates, you can use them in your `.twig` files. |
| 50 | + |
| 51 | +### Include |
| 52 | + |
| 53 | +```twig |
| 54 | +{% include '@html/inline/a/a.twig' with { |
| 55 | + href: 'https://example.com', |
| 56 | + title: 'Some info about the link', |
| 57 | + rel: 'nofollow', |
| 58 | + role: 'button', |
| 59 | + content: '<strong>Click here</strong>' |
| 60 | +} %} |
| 61 | +``` |
| 62 | + |
| 63 | +### Embed |
| 64 | + |
| 65 | +```twig |
| 66 | +{% embed '@html/inline/a/a.twig' with { |
| 67 | + href: 'https://example.com', |
| 68 | + title: 'Some info about the link', |
| 69 | + rel: 'nofollow', |
| 70 | + role: 'button' |
| 71 | +} %} |
| 72 | + {% block content %} |
| 73 | + {% include '@html/inline/strong/strong.twig' with { |
| 74 | + content: 'Overriding content here' |
| 75 | + } %} |
| 76 | + {% endblock %} |
| 77 | +{% endembed %} |
| 78 | +``` |
| 79 | + |
| 80 | +### Use |
| 81 | + |
| 82 | +```twig |
| 83 | +{% use '@html/inline/a/a.twig' %} |
| 84 | +
|
| 85 | +{% block a %} |
| 86 | + {% set href = 'https://example.com' %} |
| 87 | + {% set title = 'Some info about the link' %} |
| 88 | + {% set rel = 'nofollow' %} |
| 89 | + {% set role = 'button' %} |
| 90 | + {% block content %} |
| 91 | + {% include '@html/inline/strong/strong.twig' with { |
| 92 | + content: 'Overriding content here' |
| 93 | + } %} |
| 94 | + {% endblock %} |
| 95 | + {{ parent() }} |
| 96 | +{% endblock %} |
| 97 | +``` |
| 98 | + |
| 99 | +## Notes |
| 100 | + |
| 101 | +Content can be passed in two ways: via the `content` parameter, or via a block override when using `embed` or `use`. Using block overrides allows you to nest other templates inside elements. |
| 102 | +While these examples seem a bit longer than writing a simple `<a>` tag, what they provide is consistency and validation. You cannot produce invalid HTML with them. |
| 103 | + |
| 104 | +### Data data-* and Alpine x-*: attributes |
| 105 | +You can pass arbitrary `data-*` attributes via the `data-attributes` mapping. For example: |
| 106 | + |
| 107 | +```twig |
| 108 | +{% include '@html/block/article/article.twig' with { |
| 109 | + content: 'Article body', |
| 110 | + 'data-attributes': { |
| 111 | + 'test': 'value', |
| 112 | + 'role': 'article' |
| 113 | + } |
| 114 | +} %} |
| 115 | +``` |
0 commit comments