|
1 | | -# laravel-transclude |
2 | | -Allow to use Angular transclude features within Blade templates. |
| 1 | +# Laravel Transclude |
| 2 | + |
| 3 | +[](https://travis-ci.org/CHStudio/laravel-transclude) |
| 4 | +[](https://coveralls.io/github/CHStudio/laravel-transclude?branch=master) |
| 5 | + |
| 6 | +This package allow to use [transclusion](https://en.wikipedia.org/wiki/Transclusion) from Blade template engines. |
| 7 | +It's very useful when you want to handle views as component. It's inspired by the Angular transclude logic. |
| 8 | + |
| 9 | +Installing |
| 10 | +---------- |
| 11 | +[](https://packagist.org/packages/chstudio/laravel-transclude) |
| 12 | +[](https://packagist.org/packages/chstudio/laravel-transclude) |
| 13 | + |
| 14 | +This project can be installed using Composer. Add the following to your `composer.json`: |
| 15 | + |
| 16 | +```JSON |
| 17 | +{ |
| 18 | + "require": { |
| 19 | + "chstudio/laravel-transclude": "~2.0" |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +or run this command: |
| 25 | + |
| 26 | +```Shell |
| 27 | +composer require chstudio/laravel-transclude |
| 28 | +``` |
| 29 | + |
| 30 | +After updating composer, add the `ServiceProvider` to the providers array in `config/app.php`. |
| 31 | + |
| 32 | +### Laravel 5.x: |
| 33 | + |
| 34 | +```php |
| 35 | +CHStudio\LaravelTransclude\TranscludeServiceProvider::class, |
| 36 | +``` |
| 37 | + |
| 38 | +Then you can use the new blade directives in your views ! |
| 39 | + |
| 40 | +Usage |
| 41 | +----- |
| 42 | + |
| 43 | +This package register three new Blade directives : |
| 44 | + |
| 45 | +* `@transclude` / `@endtransclude` to write inside a transcluded block, |
| 46 | +* `@transcluded` to declare a space where the transclusion will be written. |
| 47 | + |
| 48 | +For example, take the [Bootstrap](http://getbootstrap.com/) form elements, they are all using the same global |
| 49 | +structure. Then in that structure there are different html blocks depending on the form element. |
| 50 | + |
| 51 | +### Create template files |
| 52 | + |
| 53 | +#### `input-group.blade.php` |
| 54 | + |
| 55 | +```twig |
| 56 | +<div class="form-group"> |
| 57 | + <label for="{{ $name }}" class="control-label">{{$label}}</label> |
| 58 | +
|
| 59 | + @transcluded |
| 60 | +</div> |
| 61 | +``` |
| 62 | + |
| 63 | +#### `radio.blade.php` |
| 64 | + |
| 65 | +```twig |
| 66 | +@transclude('input-group') |
| 67 | + @foreach($options as $option) |
| 68 | + <div class="radio"> |
| 69 | + <label> |
| 70 | + <input name="{{$name}}" type="radio" {{$option['value']==$selected?' checked':''}} value="{{$option['value']}}" /> |
| 71 | + {{$option['label']}} |
| 72 | + </label> |
| 73 | + </div> |
| 74 | + @endforeach |
| 75 | +@endtransclude |
| 76 | +``` |
| 77 | + |
| 78 | +### Use the new blocks |
| 79 | + |
| 80 | +Then after writing this 3 files, you can add an element using the `@include` directive : |
| 81 | + |
| 82 | +```twig |
| 83 | +<form> |
| 84 | + @include('radio', [ |
| 85 | + 'options' => [ |
| 86 | + ['value' => '1', 'label' => 'Option 1'] |
| 87 | + ], |
| 88 | + 'selected' => '1', |
| 89 | + 'label' => 'My radio button' |
| 90 | + 'name' => 'my-radio' |
| 91 | + ]) |
| 92 | +</form> |
| 93 | +``` |
| 94 | + |
| 95 | +This code will generate a full radio element with a combination of input-group and radio templates : |
| 96 | + |
| 97 | +```html |
| 98 | +<form> |
| 99 | + <div class="form-group"> |
| 100 | + <label for="my-radio" class="control-label">My radio button</label> |
| 101 | + |
| 102 | + <div class="radio"> |
| 103 | + <label> |
| 104 | + <input name="my-radio" type="radio" checked value="1" /> |
| 105 | + Option 1 |
| 106 | + </label> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | +</form> |
| 110 | +``` |
| 111 | + |
| 112 | +## Contributing |
| 113 | + |
| 114 | +We welcome everyone to contribute to this project. Below are some of the things that you can do to contribute. |
| 115 | + |
| 116 | +- Read [our contributing guide](CONTRIBUTING.md). |
| 117 | +- [Fork us](https://github.com/chstudio/laravel-transclude/fork) and [request a pull](https://github.com/chstudio/laravel-transclude/pulls) to the [master](https://github.com/chstudio/laravel-transclude/tree/master) branch. |
| 118 | +- Submit [bug reports or feature requests](https://github.com/chstudio/laravel-transclude/issues) to GitHub. |
0 commit comments