-
Notifications
You must be signed in to change notification settings - Fork 7.6k
PHXView View Engine
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.
- Download File:PHXView.zip zip and unpack.
- Copy PHXView.php to your application/libraries folder.
- Create the following folders: application/components, application/layouts, application/views/components, application/views/layouts
- Add PHXView to your autoloads.
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>
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><?=$layoutData ?></p>
<p><?=$view ?></p>
</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');
}
}
?>
When visiting /test you should receive the following output.
<html>
<head></head>
<body>
<p>Hi, I am data added from the layout class!</p>
<p>Hi, I'm the rendered view file.</p>
</body>
</html>