wongyip / laravel-renderable
此包的最新版本(v1.2.5)没有可用的许可证信息。
轻松渲染Eloquent模型。
v1.2.5
2024-09-30 06:27 UTC
Requires
- php: >=8.1
- ezyang/htmlpurifier: ^4.17
- laravel/framework: *
- rcrowe/twigbridge: *
- wongyip/html-beautify: ^1.0
- wongyip/html-tags: *
- wongyip/phphelpers: *
- dev-master
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.12.0
- v0.11.1
- v0.11.0
- v0.10.0
- v0.9.2
- v0.9.1
- 0.9.0
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.1
- v0.6.0
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
This package is auto-updated.
Last update: 2024-09-30 06:28:13 UTC
README
以HTML表格视图呈现Eloquent模型或关联数组。
安装
将作为包安装到Laravel项目中
composer require wongyip/laravel-rendetable
用法
use Wongyip\Laravel\Renderable\Renderable; // Attributes to be rendered, can be an Eloquent model. $user = [ 'id' => 1999, 'surname' => 'SOME', 'last_name' => 'Body', 'roles' => ['Operator', 'Editor', 'Supervisor'], 'gender' => 'Male', 'birthday' => '29th Feb', 'active' => false ]; // Render all attributes except 'gender' and 'birthday'. $included = true; $excluded = ['gender', 'birthday']; // Custom Labels $labels = [ 'surname' => 'First Name', 'active' => 'Status' ]; // Make $r = Renderable::table($user, $included, $excluded); // Render as <ul>, expected array value. $r->typeUL('roles'); // Print 'Active' and 'Blocked' when attribute 'active' is TRUE and FALSE respectively. $r->typeBool('active', 'Active', 'Blocked'); // Overwrite auto-generated labels. $r->labels($labels); // To HTML. echo $r->render();
输出
<div id="renderable-12345678-container" class="renderable-container"> <table id="renderable-12345678" class="table table-bordered table-strip renderable-table"> <thead class="thead-light"> <tr> <th class="renderable-field-header">Field</th> <th class="renderable-value-header">Value</th> </tr> </thead> <tbody class="renderable-body"> <tr class="field-id"> <th class="renderable-label">ID</th> <td class="renderable-value">1999</td> </tr> <tr class="field-surname"> <th class="renderable-label">First Name</th> <td class="renderable-value">SOME</td> </tr> <tr class="field-last_name"> <th class="renderable-label">Last Name</th> <td class="renderable-value">Body</td> </tr> <tr class="field-roles"> <th class="renderable-label">Roles</th> <td class="renderable-value"> <ul> <li>Operator</li> <li>Editor</li> <li>Supervisor</li> </ul> </td> </tr> <tr class="field-active"> <th class="renderable-label">Status</th> <td class="renderable-value">Blocked</td> </tr> </tbody> </table> </div>
输出说明
- 主要输出是一个包裹在容器标签
<div>
中的<table>
标签。 Renderable
对象在实例化时会随机生成自己的Renderable.id
(例如12345678
),这可以通过Renderable.id()
方法进行更改。- 主要标签将具有从
Renderable.id
派生的id
属性,默认情况下以renderable-
为前缀,可通过/config/renderable.php
配置,并可通过更新Renderable.options.idPrefix
属性在运行时更改。 - 容器标签的ID默认进一步附加
-container
后缀,可通过/config/renderable.php
配置,并可通过更新Renderable.options.containerIdSuffix
属性在运行时更改。 - 字段标签和值根据设置进行渲染。
注意
- 输出使用HTML Beautify格式化。
- 输出使用HTML Purifier进行清理。
Renderable
对象设计为只渲染一次,使用clone
关键字创建多个Renderable
对象可能会陷入变量引用噩梦,如果确实需要多个实例,请使用Deep Copy。