Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit ec8db35

Browse files
committed
wip
1 parent fa40c18 commit ec8db35

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,97 @@ This tag helper would be called for every HTML element that has a `my-attribute`
8989

9090
### Manipulating DOM Elements
9191

92+
Once your tag helper successfully matches one or multiple HTML elements, the `process` method of your tag helper will be called.
93+
94+
Inside of this method, you can manipulate the HTML element.
95+
96+
Available features:
97+
98+
#### Changing the HTML element tag
99+
100+
In this example, we are binding our helper to HTML elements `<my-custom-link href="/"></my-custom-link>`. In the process method, we can then change the tag internally to `a` to render this as a link.
101+
102+
```php
103+
<?php
104+
105+
namespace BeyondCode\TagHelper\Helpers;
106+
107+
use BeyondCode\TagHelper\Helper;
108+
use BeyondCode\TagHelper\Html\HtmlElement;
109+
110+
class CustomLink extends Helper
111+
{
112+
protected $targetElement = 'my-custom-link';
113+
114+
public function process(HtmlElement $element)
115+
{
116+
$element->setTag('a');
117+
}
118+
}
119+
```
120+
121+
#### Manipulating Attributes
122+
123+
You can also add, edit or delete HTML element attributes.
124+
125+
In this example, we are binding our helper to all link tags that have a custom `route` attribute.
126+
We then update the `href` attribute of our link, remove the `route` attribute and add a new `title` attribute.
127+
128+
```php
129+
<?php
130+
131+
namespace BeyondCode\TagHelper\Helpers;
132+
133+
use BeyondCode\TagHelper\Helper;
134+
use BeyondCode\TagHelper\Html\HtmlElement;
135+
136+
class CustomLink extends Helper
137+
{
138+
protected $targetAttribute = 'route';
139+
140+
protected $targetElement = 'a';
141+
142+
public function process(HtmlElement $element)
143+
{
144+
$element->setAttribute('href', route($element->getAttribute('route')));
145+
146+
$element->removeAttribute('route');
147+
148+
$element->setAttribute('title', 'This is a link.');
149+
}
150+
}
151+
```
152+
153+
#### Manipulating Outer / Inner Text
154+
155+
Your custom tag helpers can you manipulate the HTML that is inside or outside of the current element.
156+
157+
```php
158+
<?php
159+
160+
namespace BeyondCode\TagHelper\Helpers;
161+
162+
use BeyondCode\TagHelper\Helper;
163+
use BeyondCode\TagHelper\Html\HtmlElement;
164+
165+
class CustomLink extends Helper
166+
{
167+
protected $targetAttribute = 'add-hidden-field';
168+
169+
protected $targetElement = 'form';
170+
171+
public function process(HtmlElement $element)
172+
{
173+
$element->removeAttribute('add-hidden-field');
174+
175+
$element->appendInnerText('<input type="hidden" name="hidden" />');
176+
177+
// $element->prependInnerText('');
178+
// $element->setInnerText('');
179+
}
180+
}
181+
```
182+
92183
## Built-In Helpers
93184

94185

0 commit comments

Comments
 (0)