analogue/presenter

Easy view presenters for Analogue ORM

5.4 2017-02-22 20:32 UTC

This package is not auto-updated.

Last update: 2024-12-21 20:14:17 UTC


README

Based heavily on Jeffrey Way's Easy View Presenters for Laravel

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.

  • Should that logic be hard-coded into the view? No.
  • Should we instead store the logic in the model? No again!

Instead, leverage view presenters. That's what they're for! This package provides one such implementation.

Install

Pull this package in through Composer.

{
    "require": {
        "analogue/presenter": "0.2.*"
    }
}

Usage

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 Analogue\Presenter\Presenter;

class UserPresenter extends Presenter {

    public function fullName()
    {
        return $this->entity->first . ' ' . $this->entity->last;
    }

    public function accountAge()
    {
        return $this->entity->created_at->diffForHumans();
    }

}

Next, on your entity, pull in the Analogue\Presenter\Presentable trait.

Here's an example - maybe an Analogue User entity.

<?php

use Analogue\ORM\Entity;
use Analogue\Presenter\Presentable;

class User extends Entity {

    use Presentable;
}

Then, add a public $presentable property to the relevant entity map:

<?php

use Analogue\ORM\EntityMap;

// the UserPresenter class we created above
use App\Http\Presenters\UserPresenter;

class UserMap extends EntityMap
{
    public $presenter = UserPresenter::class;

    // ...
}

That's it! You're done. Now, within your controller/view, you can do:

// in some controller
return view("user", ["user" => $user->present()]);
<h1>Hello, {{ $user->fullName() }}</h1>

The Presenter will also pass through any calls to entity properties: e.g. $user->present()->first() would return the $user->first property - this is useful if you pass the presenter, rather than the entity, into your template.

Laravel Integration

There is also a PresentBladeServiceProvider included for use with Laravel and the Blade templating language.

This adds a @presenteach and @endpresenteach directive, which allows you to easily iterate over the presenters for each entity in a collection:

// config/app.php
'providers' => [
    // ...
    Analogue\Presenter\PresentBladeServiceProvider::class,
]
<ul>
@presenteach ($users as $user)
    <li>{{ $user->fullName() }}</li>
@endpresenteach
</ul>