Skip to content

PHXView View Engine

Derek Jones edited this page Jul 5, 2012 · 19 revisions

Category:Library::Template Engine

This is a View engine that wraps the built in CI engine and allows the use of components and layouts. Components can be used to create pieces of the site that are common to many areas of your site/application. Layouts allow you to define a single file as a shell for your site to work inside of.

Installation:

  1. Download File:PHXView.zip zip and unpack.
  2. Copy PHXView.php to your application/libraries folder.
  3. Create the following folders: application/components, application/layouts, application/views/components, application/views/layouts
  4. Add PHXView to your autoloads.

Usage:

Simple Usage:

Create a layout file:

File name: application/views/layouts/default.php

<html>
<head></head>
<body>

<?=$view ?>

</body>
</html>

Create a view file:

File name: application/views/testphxview.php

Hi, I'm the rendered view file.

Create a controller:

File name: application/controller/test.php

<?php

class Test extends Controller
{
    function index()
    {
        $this->phxview->RenderView('testphxview');
        $this->phxview->RenderLayout('default');
    }
}

?>

What you should receive as output when you visit /test is:

<html>
<head></head>
<body>

Hi, I'm the rendered view file.

</body>
</html>

More Complicated Usage:

Create a layout class: Notes: Layout and component classes have one requirement. They must implement a Render method that accepts a data array as the first parameter and an engine array as the second parameter by reference. The engine array is used to reference back to the phxview engine for the final rendering. Also the file name and class name should be the same.

File name: application/layouts/default.php

<?php

class default
{
    function Render($data, &$engine)
    {
        $data['layoutData'] = 'Hi, I am data added from the layout class!';
        
        $engine->RenderLayoutView('default', $data);
    }
}

?>

Create a layout file:

File name: application/views/layouts/default.php

<html>
<head></head>
<body>

<p>&lt;?=$layoutData ?&gt;</p>

<p>&lt;?=$view ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;

Create a view file:

File name: application/views/testphxview.php

Hi, I'm the rendered view file.

Create a controller:

File name: application/controller/test.php

&lt;?php

class Test extends Controller
{
    function index()
    {
        $this->phxview->RenderView('testphxview');
        $this->phxview->RenderLayout('default');
    }
}

?&gt;

When visiting /test you should receive the following output.

&lt;html&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;

<p>Hi, I am data added from the layout class!</p>

<p>Hi, I'm the rendered view file.</p>

&lt;/body&gt;
&lt;/html&gt; 
Clone this wiki locally