Giter Club home page Giter Club logo

presenter's Introduction

This package has been abandoned and is no longer maintained.

Caffeinated Presenter

Laravel 5.1 Laravel 5.2 Source License

Laravel 5 view presenters, originally developed after the Laracasts video tutorial on the same topic: View Presenters From Scratch.

Presenters allow you to manipulate any form of data for display within a view file. A simple example would be if you have a user entity with fields for the first and last names, how would you simply display the full name of the user within your view file? The most common solution would be something like the following:

<h1>Hello, {{ $user->first_name }} {{ $user->last_name }}!</h1>

Which works, but every time you need to display a user's full name, you'd have to type this out. What if instead it was something like this:

<h1>Hello, {{ $user->present()->fullName }}!</h1>

Isn't that much more readable than the previous example? Now you may argue that you could add this type of logic directly to your model class, which you could, but then you'll find that your model classes are riddled with methods that are manipulating view logic. I don't believe model entities should be responsible for this. Their only job is to pull the requested data from the database and hand it over. That's it.

Quick Installation

Begin by installing the package through Composer.

composer require caffeinated/presenter=~2.0

And that's it! With your coffee in reach, start building out some awesome presenters!

Usage

1. Pull in trait

Within your model, simply pull in the Caffeinated\Presenter\Traits\PresentableTrait trait, which will automatically instantiate the Caffeinated Presenter class.

...

use Caffeinated\Presenter\Traits\PresentableTrait;

class Example extends Eloquent
{
	use PresentableTrait;

	...
}

2. Define your presenter class

Define a protected $presenter variable pointing to the namespace of your presenter class.

...

use Caffeinated\Presenter\Traits\PresentableTrait;

class Example extends Eloquent
{
	use PresentableTrait;

	protected $presenter = 'App\Presenters\Page';

	...
}

3. Create your presenter class

Create a new class as defined within your model earlier - in our case we'll create a new directory within the app directory called Presenters, and create a Page file. Presenters should extend the abstract Caffeinated\Presenter\Presenter class.

<?php
namespace App\Presenters;

use Caffeinated\Presenter\Presenter;

class Page extends Presenter
{
	...
}

4. Define your presenter methods

Your model instance is passed through to your presenter class automatically, and is accessible via $this->entity. With that, you may now define any number of presenter methods here as you wish.

<?php
namespace App\Presenters;

use Caffeinated\Presenter\Presenter;

class Page extends Presenter
{
	public function title()
	{
		return ucwords($this->entity->title);
	}
}

The usage for the above would then be {{ $page->present()->title }}.

presenter's People

Contributors

kaidesu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

presenter's Issues

Please set the $presenter property to your presenter path.

Having this error, I am trying a very basic example just to test.

Laravel 5.1
ErrorException in PresentableTrait.php line 24:
Please set the $presenter property to your presenter path. (View: pathtoview/view.php)

Model

namespace App\Models\Main;

use Caffeinated\Presenter\Traits\PresentableTrait;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    use PresentableTrait;

    protected $presenter = 'App\Presenters\Phone';

    protected $table = 'd_m_phone';
}

Presenter

namespace App\Presenters;

use Caffeinated\Presenter\Presenter;

class Phone extends Presenter 
{

    public function phoneDisplay()
    {
        return $this->number;
    }

}

In the view

{{ $phone->present()->phoneDisplay }}

Any ideas why it's showing that error? I checked for typos everywhere.

Composer + Failed to download caffeinated/presenter

- Installing caffeinated/presenter (v1.0.1)
    Downloading: Connecting...    Failed to download caffeinated/presenter from dist: The "https://api.github.com/repos/caffeinated/presenter/zipball/5001cfc47665f57c0c6ecd9d87072d52ea79584f" file could not be downloaded (HTTP/1.1 404 Not Found)
    Now trying to download from source
- Installing caffeinated/presenter (v1.0.1)
  Cloning 5001cfc47665f57c0c6ecd9d87072d52ea79584f

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.