洞察探索open banking如何通过小程序容器技术助力金融企业实现数据安全和数字化转型
754
2022-10-17
Laravel Tag Helpers:向Laravel应用程序添加功能强大的HTML标记帮助程序
Laravel Tag Helpers
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:
You can use custom tag helpers to turn this code into this:
Installation
You can install the package via composer:
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:
To use and apply this tag helper, you need to register it. Typically you would do this in the AppServiceProvider boot() method or a service provider of your own.
TagHelper::helper(CustomTagHelper::class);
Since you only register the class name of the custom tag helper, you can use dependency injection inside of your custom helper class.
Binding your helper to HTML elements and attributes
In your custom tag helper, you can use the $targetAttribute and $targetElement properties to specify which HTML element (div, form, a, etc.) and which attributes (
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:
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
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.
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.
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:
You can access this data, using the getAttribute method inside your helper:
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:
Home
setAttribute('href', "{{ route(" . $element->getAttributeForBlade('route') . ") }}"); $element->removeAttribute('route'); }}
This will output:
Home
But if you pass a dynamic parameter like this:
Home
This will output:
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.
Will become:
Caveats
csrf needs to be in a line with another attribute.
// this works
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:
Homeid()]">Home
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.
Credits
Marcel PociotAll Contributors
License
The MIT License (MIT). Please see License File for more information.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~