Skip to content

Commit

Permalink
Merge pull request #204 from azamtav/main
Browse files Browse the repository at this point in the history
Documentation on how to extend the package
  • Loading branch information
freekmurze authored Jan 5, 2024
2 parents 6093daf + d66cde8 commit 05418a0
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions docs/general-usage/extending.md
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);
}
}
```

0 comments on commit 05418a0

Please sign in to comment.