-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from azamtav/main
Documentation on how to extend the package
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: Extending the package | ||
weight: 2 | ||
--- | ||
|
||
If you want to extend the Html package, you can do the following. | ||
|
||
Create a class that extends Html: | ||
|
||
|
||
```php | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Spatie\Html\Elements\Div; | ||
use Spatie\Html\Html; | ||
|
||
class HtmlExtended extends Html | ||
{ | ||
public function yesNoRadio($name = null, $model = null) | ||
{ | ||
return Div::create()->addChildren( | ||
Div::create()->class('form-check form-check-inline') | ||
->addChildren($this->radio($name)->class('form-check-input')->id($name.'_yes')->value(1)->checked(old($name) === '1' || $model[$name] === 1)) | ||
->addChildren($this->label('Yes')->for($name.'_yes')->class('form-check-label')) | ||
)->addChildren( | ||
Div::create()->class('form-check form-check-inline') | ||
->addChildren($this->radio($name)->class('form-check-input')->id($name.'_no')->value(0)->checked(old($name) === '0' || $model[$name] === 0)) | ||
->addChildren($this->label('No')->for($name.'_no')->class('form-check-label')) | ||
); | ||
} | ||
} | ||
``` | ||
|
||
Re-Register the class in the AppServiceProvider: | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use App\Helpers\HtmlExtended; | ||
use Illuminate\Support\ServiceProvider; | ||
use Spatie\Html\Html; | ||
|
||
class AppServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register any application services. | ||
*/ | ||
public function register(): void | ||
{ | ||
$this->app->singleton(Html::class, HtmlExtended::class); | ||
} | ||
} | ||
``` |