Skip to content

jamierumbelow/presenters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

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:

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

Run composer update:

$ php composer.phar update

...and autoload:

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.

Instantiate a new presenter object and pass through the raw object:

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

You can then access the data inside the presenter:

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:

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

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

About

Clean up your views with PHP presenters

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages