Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sineld/bladeset
A very simple blade extension which allows variables to be set within blade templates.
https://github.com/sineld/bladeset
Last synced: 3 months ago
JSON representation
A very simple blade extension which allows variables to be set within blade templates.
- Host: GitHub
- URL: https://github.com/sineld/bladeset
- Owner: sineld
- License: mit
- Created: 2016-01-30T00:12:28.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-08-30T02:36:54.000Z (about 2 years ago)
- Last Synced: 2024-07-10T21:19:53.040Z (4 months ago)
- Language: PHP
- Homepage: http://packalyst.com/packages/package/sineld/bladeset
- Size: 35.2 KB
- Stars: 44
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - sineld/bladeset - A very simple blade extension which allows variables to be set within blade templates. (PHP)
README
## Laravel Blade Variable Assign In Template
This package is heavily inspirated from [alexdover/blade-set](https://github.com/alexdover/blade-set) after [this discussion](https://github.com/laravel/framework/issues/4778).
A very simple blade extension which allows variables to be set within blade templates.
This versions is for Laravel v. 5-6-7-8-9 and should work with future versions of Laravel, if don't, please mail me.
### Usage Examples
```php
@set('myVariable', $existing_variable)// or
@set("myVariable", "Hello, World!")
```Then you can use the variable `$myVariable` in the blade template.
```php
{{ $myVariable }}
```You might choose to fetch a bunch of models from your template, for example
```php
@set('myModelList', MyModel::where('something', '=', 1)->paginate(10))
```### Available Operators [set, var, assign]
```php
@set('username', 'sineld')@var("username", "sineld")
@assign('username', 'sineld')
```Tip: You can assign your own operator in config file!
### Why?
Compare
```php
paginate(10); ?>
```to
```php
@set('myModelList', MyModel::where('something', '=', 1)->paginate(10))
```I felt that the use of the `@set` was a more elegant solution in the context of blade templates.
**Another reason (from [github issue page](https://github.com/laravel/framework/issues/4778#issuecomment-126774099)):**
An example where setting and keeping track of a variable inside a template using this sytax would be processing a list of things where each thing has a week and you want to set a week header for each group of weeks:
```php
@set('week', 0);@foreach ($things as $thing)
@if ($week != $thing->week)
WEEK {{ $thing->week }}
@set('week', $thing->week)
@endifTitle: {{ $thing->title }}
@endforeach
```### Installation
Run this command on terminal in your packages root:
```php
composer require sineld/bladeset
```or
Require this package in your `composer.json`:
```php
"sineld/bladeset": "8.0"
```Update composer. This will download the package.
```php
composer update
```Add the BladeSetServiceProvider to the providers array in `config/app.php` if your projects Laravel version is below 5.5. If you Laravel version is higher or equal to 5.5 you do not need to add the line below to `config/app.php` file because package auto discovery is available.
```php
Sineld\BladeSet\BladeSetServiceProvider::class,
```(Optional) Publish package config.
```php
php artisan vendor:publish
```Then edit `app/config/bladeset.php` accoring to your needs.
All done!
### Licence
You can use this package under the [MIT license](http://opensource.org/licenses/MIT)### Feedback
If you have any questions, feature requests or constructive criticism then please get in touch.
Twitter - [@sineld](http://twitter.com/sineld)