2017 © Pedro Peláez
 

library kisstpl

View service / template engine for plain PHP templates

image

mindplay/kisstpl

View service / template engine for plain PHP templates

  • Thursday, September 7, 2017
  • by mindplay.dk
  • Repository
  • 1 Watchers
  • 3 Stars
  • 4,295 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 0 Forks
  • 1 Open issues
  • 13 Versions
  • 5 % Grown

The README.md

mindplay/kisstpl

PHP Version Build Status Code Coverage Scrutinizer Code Quality, (*1)

A very simple view-service / template-engine for plain PHP templates., (*2)

I wanted a template engine that uses view-models (objects) rather than view-dictionaries (arrays) as are typical in most PHP template engines., (*3)

Oh, and if you don't like typing htmlspecialchars() all day, maybe try this., (*4)

The view-service is tied to a root folder and a root namespace:, (*5)

$service = new ViewService(new SimpleViewFinder('my/app/views', 'app\view'));

$hello = new \app\view\HelloWorld();

$service->render($hello); // -> "my/app/views/HelloWorld.view.php"

The render() statement in this example will render the template my/app/views/HelloWorld.view.php, passing the view-model object to the rendered template as $view - the SimpleViewFinder is responsible for locating the actual template based on the type of view-model., (*6)

The render() method also accepts a second argument, allowing you to render different templates for the same view-model:, (*7)

$service->render($hello, 'boom'); // -> "my/app/views/HelloWorld.boom.php"
$service->render($hello, 'bang'); // -> "my/app/views/HelloWorld.bang.php"

You can type-hint in the beginning of a template file for IDE support:, (*8)


...

Alternatively, for type-safe template rendering, you can also type-hint statically, by returning a closure:, (*9)

use app\view\HelloWorld;
use mindplay\kisstpl\Renderer;


...
layout->title = 'My Page!';

$this->begin($view->layout->body);

?>


Hello!

Body content goes here..., (*10)

<?php $this->end($view->layout->body); $this->render($view->layout);

Note that begin() and end() take variable references as arguments - the call to end() will apply the captured content to $view->layout->body., (*11)

There is deliberately no view rendering "pipeline", or any concept of layout, and this is "a good thing" - your templates have complete control of the rendering process, you have IDE support all the way,, (*12)

You can also capture rendered content and return it, instead of sending the rendered content to output:, (*13)

$content = $service->capture($hello);

You can use this feature to implement "partials", since it can be called from within a template. Like render(), the capture() method also accepts a second argument allowing you to render different views of the same view-model., (*14)

You can of course also extend ViewService with custom functionality - an interface Renderer defines the four basic methods, render(), capture(), begin() and end() so you can type-hint and swap out implementations as needed., (*15)

You can also replace the ViewFinder implementation if you need custom logic (specific to your project) for locating templates. A few implementations are included:, (*16)

  • SimpleViewFinder for direct 1:1 class-to-file mapping (and zero overhead from calls to file_exists()) with a specified base namespace and root path., (*17)

  • LocalViewFinder for direct 1:1 class-to-file mapping (and zero overhead) not limited to any particular namespace, and assuming local view-files located in the same path as the view-model class file., (*18)

  • DefaultViewFinder which searches a list of root-paths and defaults to the first template found., (*19)

  • MultiViewFinder which allows you to aggregate as many other ViewFinder instances as you need, and try them in order., (*20)

The latter is useful in modular scenarios, e.g. using a "theme" folder for template overrides, allowing you to plug in as many conventions for locating views as necessary., (*21)

The Versions

07/09 2017

dev-master

9999999-dev

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

07/09 2017

dev-bug/error-handling

dev-bug/error-handling

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

07/09 2017

1.3.3

1.3.3.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

14/06 2017

dev-bugfix/output-buffers

dev-bugfix/output-buffers

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

14/06 2017

1.3.2

1.3.2.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

13/01 2016

1.3.1

1.3.1.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

09/12 2015

1.3.0

1.3.0.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

03/09 2015

1.2.3

1.2.3.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

27/08 2015

1.2.2

1.2.2.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

03/06 2015

1.2.1

1.2.1.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

03/06 2015

1.2.0

1.2.0.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

02/06 2015

1.1.0

1.1.0.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires

19/02 2015

1.0.0

1.0.0.0

View service / template engine for plain PHP templates

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3

 

The Development Requires