Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/beyondcode/laravel-tag-helper
Add powerful HTML tag helpers to your Laravel application
https://github.com/beyondcode/laravel-tag-helper
laravel view
Last synced: 3 months ago
JSON representation
Add powerful HTML tag helpers to your Laravel application
- Host: GitHub
- URL: https://github.com/beyondcode/laravel-tag-helper
- Owner: beyondcode
- License: mit
- Archived: true
- Created: 2018-10-05T14:43:22.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-29T13:22:14.000Z (about 6 years ago)
- Last Synced: 2024-05-09T14:09:53.385Z (6 months ago)
- Topics: laravel, view
- Language: PHP
- Size: 77.1 KB
- Stars: 239
- Watchers: 8
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Tag Helpers
[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-tag-helper.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-tag-helper)
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-tag-helper/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-tag-helper)
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-tag-helper.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-tag-helper)
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-tag-helper.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-tag-helper)This package allows you to register custom "tag helpers" in your Laravel application. These helpers can modify the HTML code.
For example, instead of this:
```html
```
You can use custom tag helpers to turn this code into this:
```html
```## Installation
You can install the package via composer:
```bash
composer require beyondcode/laravel-tag-helper
```The package will automatically register itself.
## Usage
You can create your own Tag Helper, by creating a new class and extend from the `BeyondCode\TagHelper\Helper` class.
Within this class you can define on which HTML elements and attributes your helper should be triggered:```php
`, ``, etc.) you want to bind this helper to.If you do not provide a `targetElement` on your own, this package will use a `*` as a wildcard in order to target **all** elements with a specific attribute, like this:
```php
class CustomTagHelper extends Helper
{
protected $targetAttribute = 'my-attribute';
// ...
}
```This tag helper would be called for every HTML element that has a `my-attribute` attribute.
### Manipulating DOM Elements
Once your tag helper successfully matches one or multiple HTML elements, the `process` method of your tag helper will be called.
Inside of this method, you can manipulate the HTML element.
Available features:
#### Changing the HTML element tag
In this example, we are binding our helper to HTML elements ``. In the process method, we can then change the tag internally to `a` to render this as a link.
```php
setTag('a');
}
}
```#### Manipulating Attributes
You can also add, edit or delete HTML element attributes.
In this example, we are binding our helper to all link tags that have a custom `route` attribute.
We then update the `href` attribute of our link, remove the `route` attribute and add a new `title` attribute.```php
setAttribute('href', route($element->getAttribute('route')));
$element->removeAttribute('route');
$element->setAttribute('title', 'This is a link.');
}
}
```#### Manipulating Outer / Inner Text
Your custom tag helpers can you manipulate the HTML that is inside or outside of the current element.
```php
removeAttribute('add-hidden-field');
$element->appendInnerText('');
// $element->prependInnerText('');
// $element->setInnerText('');
}
}
```### Passing variables to your tag helpers
You can pass attribute values to your tag helpers as you would usually pass attributes to HTML elements.
Since the modifications of your tag helpers get cached, you should always return valid Blade template output in your modified attribute values.You can **not** directly access the variable content inside of your tag helper, but only get the attribute string representation.
For example, to get the attribute value of the `method` attribute:
```html
```
You can access this data, using the `getAttribute` method inside your helper:
```php
getAttribute('method');
}
}
```If you want to write Blade output, you sometimes need to know if the user passed a variable or function call, or a string value.
To tell the difference, users can pass variable data by prefixing the attribute using a colon.If you want to output this attribute into a blade template, you can then use the `getAttributeForBlade` method and it will
either give you an escaped string representation of the attribute - or the unescaped representation, in case it got prefixed by a colon.For example:
```html
Home
``````php
setAttribute('href', "{{ route(" . $element->getAttributeForBlade('route') . ") }}");
$element->removeAttribute('route');
}
}
```This will output:
```html
Home
```But if you pass a dynamic parameter like this:
```html
Home
```This will output:
```html
Home
```This way you do not need to manually care about escaping and detecting dynamic variables.
## Built-In Helpers
This package ships with a couple useful tag helpers out of the box.
### CSRF Helper
Just add a `csrf` attribute to any `form` element to automatically add the Laravel CSRF field to it.
```html
```
Will become:
```html
```
#### Caveats
`csrf` needs to be in a line with another attribute.
```php
// this works```
Will become:
```html
```
### Link
When your `a` tags contains a `route` attribute, this helper will change the href to the appropriate route.
You can also provide a `route-parameters` attribute, to pass additional parameters to the route generation.Examples:
```html
Home```
### Testing
``` bash
composer test
```### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
### Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
## Credits
- [Marcel Pociot](https://github.com/:author_username)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.