PHPackages                             ingenerator/kohana-view - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. ingenerator/kohana-view

ActiveKohana-module[Templating &amp; Views](/categories/templating)

ingenerator/kohana-view
=======================

PHP class based views for the Kohana framework

v4.9.0(9mo ago)430.8k↓34.1%1BSD-3-ClausePHPPHP ~8.2.0 || ~8.3.0 || ~8.4.0CI passing

Since Aug 1Pushed 8mo ago2 watchersCompare

[ Source](https://github.com/ingenerator/kohana-view)[ Packagist](https://packagist.org/packages/ingenerator/kohana-view)[ Docs](https://github.com/ingenerator/kohana-view)[ RSS](/packages/ingenerator-kohana-view/feed)WikiDiscussions 5.x Synced 3d ago

READMEChangelog (10)Dependencies (8)Versions (31)Used By (1)

`kohana-view` provides separation of view logic and templating for PHP applications. It's designed to be used with the Kohana framework - but you should be able to use it with most PHP projects with a little work.

[![License](https://camo.githubusercontent.com/6f4821f677385d3ffcb742afe71c4105601a86e41148e9d76fc5600218796ed4/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6b6f68616e612d766965772f6c6963656e73652e737667)](https://packagist.org/packages/ingenerator/kohana-view)[![Build status](https://github.com/ingenerator/kohana-view/actions/workflows/test.yaml/badge.svg)](https://github.com/ingenerator/kohana-view/actions/workflows/test.yaml)[![Latest Stable Version](https://camo.githubusercontent.com/213d975be4799cdc3ba28568ac25731eefb20fc5f502c3d8036b20bee238cc40/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6b6f68616e612d766965772f762f737461626c652e737667)](https://packagist.org/packages/ingenerator/kohana-view)[![Latest Unstable Version](https://camo.githubusercontent.com/27e3cfcd81a22cf1bfeb6efb0bb0fa97dbe2903c87c9c7bf411a6121768f45ec/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6b6f68616e612d766965772f762f756e737461626c652e737667)](https://packagist.org/packages/ingenerator/kohana-view)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e1879ab36b65537e974adcaa7f9cc5f97c98fae0bb539ec17068a898623d13ec/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696e67656e657261746f722f6b6f68616e612d766965772f6261646765732f7175616c6974792d73636f72652e706e673f623d332e302e78)](https://scrutinizer-ci.com/g/ingenerator/kohana-view/?branch=3.0.x)[![Total Downloads](https://camo.githubusercontent.com/fdaaa644dfe9a1b719b723b19f7388ef2194f998a2a5c648e36309bd5a7da115/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6b6f68616e612d766965772f646f776e6c6f6164732e737667)](https://packagist.org/packages/ingenerator/kohana-view)

For legacy projects, it can coexist with the standard Kohana `View` class, but it is not in any way compatible - each view needs to use either the stock `View` or be updated to work with `kohana-view`. In particular, there are significant differences in how we approach page layout views compared to the stock Kohana `Controller_Template`.

Why you should use it
---------------------

[](#why-you-should-use-it)

- Class-based views keep logic out of your controllers, and out of your templates
- Easier to find and update all the display logic in your application
- Easier to customise display logic for modules, configurable sections of applications, etc
- Make the dependencies of each View more obvious and easier to maintain
- Automatically escape all view variables when output as HTML (by default, can be disabled)
- Clean, well-structured code with no global state is easier to test and less at risk of error
- Fully unit tested

Installation
------------

[](#installation)

Install with composer: `$> composer require ingenerator/kohana-view`

Add to your `application/bootstrap.php`:

```
Kohana::modules([
  'existing'    => 'existing/modules/call/here',
  'kohana-view' => __DIR__.'/../vendor/ingenerator/kohana-view'
]);
```

We also recommend using a dependency injection container / service container to manage all the dependencies in your project. Kohana-view doesn't require one in particular, but comes with configuration for [zeelot/kohana-dependencies](https://github.com/zeelot/kohana-dependencies). Examples in this readme assume you're using that container, so if you're using something else (really, don't try and do it all inline in PHP) then fetch dependencies from your container however required.

### Caching ViewModel schemas

[](#caching-viewmodel-schemas)

The library needs to parse your AbstractViewModel classes to validate the variables passed to display (see below).

While it will work "out of the box", we strongly recommend registering a suitable psr/cache implementation to avoid any performance issues from the use of Reflection at runtime. For example, you could `composer require symfony/cache` and add something like this to your bootstrap:

```
// bootstrap.php
\Ingenerator\KohanaView\ViewModel\DisplaySchema\ViewDisplaySchemaProviderInstance::init(
    cache: match(Kohana::$environment) {
        Kohana::DEVELOPMENT => new \Symfony\Component\Cache\Adapter\ArrayAdapter(),
        default => new \Symfony\Component\Cache\Adapter\ApcuAdapter(),
    }
);
```

Creating your first view
------------------------

[](#creating-your-first-view)

Each view starts with a class implementing the `Ingenerator\KohanaView\ViewModel` interface. You can roll your own, or extend from `Ingenerator\KohanaView\ViewModel\AbstractViewModel` for a base class with some useful common functionality. View classes can be named anything you like, with or without namespaces, whatever.

```
