Package Data | |
---|---|
Maintainer Username: | RedSquareWeb |
Maintainer Contact: | christian@redsquareweb.com (Christian Bricka) |
Package Create Date: | 2016-05-30 |
Package Last Update: | 2016-06-01 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-29 15:01:44 |
Package Statistics | |
---|---|
Total Downloads: | 9 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Forked from Jeffry Way's view presenter.
Fixed some compatibility issues with Laravel 5.1 and 5.2.
So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) is displayed from the view.
Instead, leverage view presenters. That's what they're for! This package provides one such implementation.
Install this package through Composer.
{
"require": {
"redsquareweb/presenter": "dev-master"
}
}
The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required.
Here's an example of a presenter.
use RedSquareWeb\Presenter\Presenter;
class UserPresenter extends Presenter {
public function fullName()
{
return $this->first . ' ' . $this->last;
}
public function accountAge()
{
return $this->created_at->diffForHumans();
}
}
Next, on your entity, pull in the RedSquareWeb\Presenter\PresentableTrait
trait, which will automatically instantiate your presenter class.
Here's an example - maybe a Laravel User
model.
<?php
use RedSquareWeb\Presenter\PresentableTrait;
class User extends \Eloquent {
use PresentableTrait;
protected $presenter = 'UserPresenter';
}
That's it! You're done. Now, within your view, you can do:
<h1>Hello, {{ $user->present()->fullName }}</h1>
Notice how the call to the present()
method (which will return your new or cached presenter object) also provides the benefit of making it perfectly clear where you must go, should you need to modify how a full name is displayed on the page.
Have fun!
Christian @ https://redsquareweb.com