2017 © Pedro Peláez
 

library presenters

Clean up your views with PHP presenters

image

jamierumbelow/presenters

Clean up your views with PHP presenters

  • Tuesday, December 4, 2012
  • by jamierumbelow
  • Repository
  • 2 Watchers
  • 6 Stars
  • 19,339 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

PHP Presenters

It's very easy to end up with complicated, brittle views full of messy logic and presentation code that really should be somewhere else. In my book, The CodeIgniter Handbook - Volume One - Who Needs Ruby? I discuss a great technique for cleaning up your views., (*1)

This small library is a lightweight, tested solution for using presenters within PHP (and CodeIgniter apps). This library is tailored for CI, but works with any PHP application., (*2)

Synopsis

class Book_Presenter extends Presenter
{
    public function price()
    {
        return number_format($this->book->price, 2);
    }
}

$book = $this->db->where('id', 1)->get('books')->row();
$book = new Book_Presenter($book);

echo $book->title() . ' costs ' . $book->price();

Installation

Add it to your composer.json:, (*3)

{
    "require": {
        "jamierumbelow/presenters": "*"
    }
}

Run composer update:, (*4)

$ php composer.phar update

...and autoload:, (*5)

require_once 'vendor/autoload.php';

Usage

Create a new class with the suffix _Presenter. Book_presenter will create a $this->book variable, Game_Type_Presenter will create a $this->game_type variable., (*6)

Instantiate a new presenter object and pass through the raw object:, (*7)

$book = $this->db->where('id', 1)->get('books')->row();
$book = new Book_Presenter($book);

You can then access the data inside the presenter:, (*8)

class Book_Presenter extends Presenter
{
    public function title()
    {
        return $this->book->title . ' - ' . $this->book->subtitle;
    }
}

If you'd like to customise the object name, pass it through as the second parameter:, (*9)

$book = new Book_Presenter($book, 'bookObject');

class Book_Presenter extends Presenter
{
    public function title()
    {
        return $this->bookObject->title . ' - ' . $this->bookObject->subtitle;
    }
}

The Versions

04/12 2012

dev-master

9999999-dev https://github.com/jamierumbelow/presenters

Clean up your views with PHP presenters

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

php codeigniter views presenters presenting handbook

04/12 2012

1.0.0

1.0.0.0 https://github.com/jamierumbelow/presenters

Clean up your views with PHP presenters

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

The Development Requires

php codeigniter views presenters presenting handbook