|
1 | | -# custom-html-elements |
2 | | -Allows you to create custom HTML elements to render more complex template parts |
| 1 | +# Custom HTML Elements |
| 2 | +Allows you to create custom HTML elements to render more complex template parts with a simpler HTML representation |
| 3 | + |
| 4 | +## Requirements |
| 5 | + |
| 6 | +* PHP 8.1+ |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```shell |
| 11 | +composer require lordsimal/custom-html-elements |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +This is an example representation of a custom HTML element you want to use: |
| 17 | + |
| 18 | +```html |
| 19 | +<c-youtube src="RLdsCL4RDf8"/> |
| 20 | +``` |
| 21 | + |
| 22 | +So this would appear in a HTML output like this: |
| 23 | + |
| 24 | +```html |
| 25 | +<!DOCTYPE html> |
| 26 | +<html lang="en"> |
| 27 | +<head> |
| 28 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 29 | + <title>Example Page</title> |
| 30 | + <meta name="author" content="Kevin Pfeifer"> |
| 31 | +</head> |
| 32 | +<body> |
| 33 | + <c-youtube src="RLdsCL4RDf8"/> |
| 34 | +</body> |
| 35 | +</html> |
| 36 | +``` |
| 37 | + |
| 38 | +To render this custom HTML element you need to do this: |
| 39 | + |
| 40 | +```php |
| 41 | +$htmlOutput = ''; // This variable represents what is shown above |
| 42 | +$me = new \LordSimal\CustomHtmlElements\TagEngine([] |
| 43 | + 'parse_on_shutdown' => true, |
| 44 | + 'tag_directories' => [ |
| 45 | + __DIR__.DIRECTORY_SEPARATOR.'Tags'.DIRECTORY_SEPARATOR, |
| 46 | + __DIR__.DIRECTORY_SEPARATOR.'OtherTagsFolder'.DIRECTORY_SEPARATOR, |
| 47 | + ], |
| 48 | + 'sniff_for_buried_tags' => true |
| 49 | +]); |
| 50 | +echo $me->parse($htmlOutput); |
| 51 | +``` |
| 52 | + |
| 53 | +The element logic can be placed in e.g. `Tags/Youtube.php` or `OtherTagsFolder/Youtube.php`: |
| 54 | + |
| 55 | +```php |
| 56 | +<?php |
| 57 | +namespace App\Tags; |
| 58 | + |
| 59 | +use LordSimal\CustomHtmlElements\CustomTag; |
| 60 | + |
| 61 | +class Youtube extends CustomTag |
| 62 | +{ |
| 63 | + public static string $tag = 'c-youtube'; |
| 64 | + |
| 65 | + public function render(): string |
| 66 | + { |
| 67 | + return <<< HTML |
| 68 | + <iframe width="560" height="315" |
| 69 | + src="https://www.youtube.com/embed/{$this->src}" |
| 70 | + allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" |
| 71 | + allowfullscreen> |
| 72 | + </iframe> |
| 73 | +HTML; |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +As you can see the main 2 parts are the |
| 79 | + |
| 80 | +```php |
| 81 | +public static string $tag = 'c-youtube'; |
| 82 | +``` |
| 83 | + |
| 84 | +which defines what HTML tag is represented by this class and the `render()` method. |
| 85 | + |
| 86 | +Inside the `render()` method you have access to all HTML attributes which you pass onto your element. |
| 87 | + |
| 88 | +So e.g. |
| 89 | + |
| 90 | +```html |
| 91 | +<c-button type="primary" text="Click me" url="/something/stupid" /> |
| 92 | +``` |
| 93 | + |
| 94 | +would be accessible inside the `Button` class via |
| 95 | + |
| 96 | +```php |
| 97 | +class Button extends CustomTag |
| 98 | +{ |
| 99 | + public static string $tag = 'c-button'; |
| 100 | + |
| 101 | + public function render(): string |
| 102 | + { |
| 103 | + $classes = ['c-button']; |
| 104 | + if ($this->type == 'primary') { |
| 105 | + $classes[] = 'c-button--primary'; |
| 106 | + } |
| 107 | + $classes = implode(' ', $classes); |
| 108 | + return <<< HTML |
| 109 | + <a href="$this->url" class="$classes">$this->text</a> |
| 110 | +HTML; |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Accessing the inner content |
| 116 | + |
| 117 | +You may want to create custom HTML elements like |
| 118 | +```html |
| 119 | +<c-github>Inner Content</c-github> |
| 120 | +``` |
| 121 | + |
| 122 | +To access the `Inner Content` text inside your class you simply have to call `$this->content` like so |
| 123 | + |
| 124 | +```php |
| 125 | +class Github extends CustomTag |
| 126 | +{ |
| 127 | + public static string $tag = 'c-github'; |
| 128 | + |
| 129 | + public function render(): string |
| 130 | + { |
| 131 | + return <<< HTML |
| 132 | + $this->content |
| 133 | +HTML; |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## Self closing elements |
| 139 | + |
| 140 | +By default every custom HTML element can be used either way: |
| 141 | + |
| 142 | +```html |
| 143 | +<c-youtube src="RLdsCL4RDf8"></c-youtube> |
| 144 | +``` |
| 145 | +or |
| 146 | +```html |
| 147 | +<c-youtube src="RLdsCL4RDf8" /> |
| 148 | +``` |
| 149 | + |
| 150 | +both are rendered the same way. |
| 151 | + |
| 152 | +## Rendering nested custom HTML elements |
| 153 | + |
| 154 | +By default this library doesn't render nested custom HTML elements. To enable this feature you have to add this config while creating the TagEngine |
| 155 | + |
| 156 | +```php |
| 157 | +$tagEngine = new TagEngine([ |
| 158 | + 'tag_directories' => [ |
| 159 | + __DIR__.DIRECTORY_SEPARATOR.'Tags'.DIRECTORY_SEPARATOR, |
| 160 | + ], |
| 161 | + 'sniff_for_nested_tags' => true |
| 162 | +]); |
| 163 | +``` |
| 164 | + |
| 165 | +## Acknowledgements |
| 166 | + |
| 167 | +This library is inspired by the following packages |
| 168 | + |
| 169 | +* https://github.com/buggedcom/PHP-Custom-Tags |
| 170 | +* https://github.com/SageITSolutions/MarkupEngine |
0 commit comments