Presenters

  • Installation
  • Usage

Installation

Open your composer.json file, and add the new required package.
"pingpong/presenters": "~2.1" 
Next, open a terminal and run.
composer update 
Done.

Usage

First, create your own presenter and make sure that class is extends to Pingpong\Presenters\Presenter class. Like this.
<?php

use Pingpong\Presenters\Presenter;

class UserPresenter extends Presenter
{
    public function email($attributes = array())
    {
        return HTML::mailto($this->resource->email, $this->resource->email, $attributes);
    }
}
?>
Make sure your model/eloquent to extends Pingpong\Presenters\Model and set the presenter property to that model/eloquent.
<?php

use Pingpong\Presenters\Model;

class User extends Model
{
    protected $presenter = 'UserPresenter';
}
?>
That's it! You're done. Now, within your view, you can do:
<h1>Your email is {{ $user->present()->email }}</h1>
Or, call the presenter as method.
<h1>Your email is {{ $user->present()->email(['width' => 140]) }}</h1>
Source:
Pingpong Labs

No comments:

Post a Comment