Download the PHP package krak/presenter without Composer
On this page you can find all versions of the php package krak/presenter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package presenter
Krak Presenter
Simple yet powerful implementation of the presenter pattern.
Installation
install via composer.
Design
The Krak Presenters are broken up into 3 main components: Presenters, Decorators, and View Models.
The Presenters and Decorators all implement the Presenter
interface:
A presenter/decorator will accept a view model to present and then return it's contents.
So the call
will always return the content associated with view.
Presenters
The Presenters will take view models and "present" them i.e. render them into a string response. This library comes bundled with 2 different presenters: View and Mock.
The View presenter will take a View model and return the appropriate content from the view file associated with the View model.
The Mock presenter is primarily used for testing, but it's just a simple wrapper around the SplObjectStorage.
ViewPresenter
the last echo statement would ouptut:
<html>
<h1>Header</h1>
</html>
The ViewPresenter constructor takes a FileLocator, an extension (defaults to an empty string), and an alias for the view model (defaults to 'view').
Extension
If there is an extension set, then it will append .{ext}
to the end of each view file name.
Alias
MockPresenter
the output will be
some-data
Decorators
The presenter system is designed around the Presenter
interface which makes the use of decorators very easily.
This library comes with two decorators: Cache and Tree.
Caching
The caching presenter is just a decorator that will try to get the presenter data out of cache before actually going through the process of rendering the view.
the output will be true because the cache presenter added the data to the cache, and the next call to present on the same view model would just return the data from cache instead of delegating the presentation to it's internal presenter.
bool(true)
Now, the CachePresenter will only cache views models that implement the CacheableView
interface.
Tree
The tree presenter is another decorator that allows a hierarchy/tree of views to be presented. A tree presenter will only traverse a tree of views if they implement the TreeView
interface. One important note about the tree view is how it handles the presenting of multiple items at once.
The call $tree_presenter->presenter($view)
will return the output of the $view
object which is the root of the tree. It will then traverse the tree down and use it's internal presenter to get all of the data for each child view. It then injects the content presented back into child views to be used by any of the parents. Look at the following example.
the last echo statement would display
<div>
<span>some-data</span>
</div>
View Models
The view models are the actual object responsible for rendering the views. There are four types of views with the Krak Presenter library: Views, TreeViews, CacheableViews, and AnonymousViews.
View
Very simple interface for retrieving the view file to be loaded. This interface is used by the ViewPresenter. You can use the ViewTrait
to define those methods for you.
TreeView
This interface is designed to be used with TreePresenter, and as you can see it allows traversal of a tree of TreeView models and allows each view to hold store their content. You can use the TreeViewTrait
to define those methods for you.
CacheableView
This interface is designed to be used with the CachePresenter. A cache tuple looks like the following:
where the tuple has a key of 'key' and a ttl of 3600.
Anonymous Views
Sometimes you don't need to build an entire class to just render a file with some data, for that, we have the Anonymous View models.
An Anonymous View implements the View
interface. You can create anonymous view like so:
then in some-view-file
this view will render to:
<div>
val
</div>
Buffer
The buffer is a simple utility that comes in handy when working with TreeViews. A buffer essentially just holds string content, but you can share the same buffer with multiple views, and they will append to the same buffer.
For example, let's say you wanted to let each view define some javascript. And the javascript defined by each view should then be output at the bottom of the page. You could that easily with a buffer.
First, we'll assume that each view has the same instance of a buffer as a public variable with the name, js_buf
.
View Listener
If you use a project that is based off of the Symfony HttpKernel, you can register the view listener so that your controllers can return views and have them converted to responses.
Then in your controllers, you can just return a view model like so, and it will be converted to a response
Service Provider
If you use Pimple in your projects, you can use the ViewPresenterServiceProvider
to register the view presenter as a service.
If you want to use decorators, then you can use the Pimple::extend
method like so: