shishima / dynamic-view-value-render
This package supports displaying values in views files according to predefined configurations.
Requires
- php: ^8.0
This package is auto-updated.
Last update: 2024-11-15 04:17:36 UTC
README
This package supports displaying values in views files according to predefined configurations. You don't need to directly modify the blade file, just adjust the config, and this package will handle the conversion for you
Installation
You can install the package via composer:
composer require shishima/dynamic-view-value-render
Usage
-
Create a configuration file in the
config
directoryE.g: Create a configuration file for home blade
return [ 'delivery_date' => [ 'type' => [\Shishima\ConvertExport\Pipeline\DB::class], 'value' => 'location.name' ] ]; ...
-
In the controllers or similar, before returning the view, use the
dynamic_render_set_data
function to assign the input dataE.g: app/Controllers/HomeController.php
$data = [ 'estimate' => $estimate, 'company' => $company ] dynamic_render_set_data(data: $data); // <-- Here return view($view, ...));
You can use the compact function to collect data into an array and then pass it to the
dynamic_render_set_data
function$data = compact('estimate', 'company'); dynamic_render_set_data(data: $data); // <-- Here return view($view, ...));
-
In the blade file, at the positions where values need to show, use the
dynamic_render_value
function to output values based on the initial configuration<div> <p>{{ dynamic_render_value(config('transform.delivery_date')) }}</p> </div>
Config
In the config file, there are two configurable values: type
and value
Type
The configured parameters will be an array. Within the array will be classes for transforming output data
There are 2 classes available:
-
\Shishima\ConvertExport\Pipeline\DB::class
This class is used to retrieve data from within a variable. Nested data at multiple levels can be accessed through the dot
.
notationUse the value config to specify the key to retrieve the value
E.g:
'delivery_date' => [ 'type' => [\Shishima\ConvertExport\Pipeline\DB::class], 'value' => 'location.city.name' ] ];
-
\Shishima\ConvertExport\Pipeline\Fixed::class
This class is used to print out predefined values configured in the
value
E.g:
'delivery_date' => [ 'type' => [\Shishima\ConvertExport\Pipeline\Fixed::class], 'value' => 'Hello world!' ] ];
Custom Transform
In practical cases, data conversion may involve special cases. In such scenarios, to handle these cases, we can create a dedicated processing class and pass this class into type for the package to handle automatically
Since type
is an array, multiple processing classes can be passed here. The data will be processed in the order from left to right
The output data of this class will be the input data for the next class
E.g: Create UpperCase class
namespace App\Transform; use Illuminate\Support\Arr; use Shishima\ConvertExport\Pipeline\ConvertExportBase; class UpperCase extends ConvertExportBase { public function __invoke($payload) { $value = Arr::get($payload, 'value', ''); $dataInput = Arr::get($payload, 'dataInput'); $config = Arr::get($payload, 'config'); // transform value return strtoupper($value); } }
IMPORTANT! If this class only performs a only task, you must use the __invoke
method
Custom class must extend the ConvertExportBase
class
The parameter $payload
is an array consisting of 3 values:
- value: Input data of previous processing steps
- dataInput: All the data assigned when using the function
dynamic_render_set_data
- config: The configuration passed through the function
dynamic_render_value
After creating the custom class, configure this class in the config file
'delivery_date' => [ 'type' => [ \Shishima\ConvertExport\Pipeline\Fixed::class, \App\Transform\UpperCase::class, ], 'value' => 'Hello world!' ] ];
Custom Class contains multiple processing methods
The package also supports writing multiple processing functions within one class
To do this, methods must start with the prefix convert
E.g:
namespace App\Transform; use Illuminate\Support\Arr; use Shishima\ConvertExport\Pipeline\ConvertExportBase; class Transform extends ConvertExportBase { public function convertUpper($payload) { // ... } public function convertLower($payload) { // ... } }
To use these functions in the config file, they will be configured under the type. No need for the convert
prefix
'delivery_date' => [ 'type' => [ '\App\Transform\Transform:Upper,Lower', ], 'value' => '' ] ];
Value
Used for DB::class
and Fixed::class
to retrieve data
If not using the above 2 classes, it can be left blank
Changelog
Please see CHANGELOG for more information on what has changed recently
Contributing
Please see CONTRIBUTING for details
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities
Credits
License
The MIT License (MIT). Please see License File for more information