A simple layout and view rendering engine built on PHP.
LwV emulates Rails and other popular framework view systems. Your markup and content is stored in the views/
folder and layouts reside in views/layouts
, though both of these values can be changed in config/config.php
.
Since the rendering engine is PHP, you can utilize any control structures and operators you like.
By default LwV expects to be installed in the root of whatever URL it is being accessed from. If the install directory is in the root (ie. www.domain.com/layout), set the base_dir
configuration to 'layout/'.
Let's say you want to reference an About page. Create views/about.php
, navigation to yourdomain.com/about
, and that view will be retrieved. The default view you see when visiting the root of LwS is views/index.php
. This is customizable in config/config.php
.
The default layout is views/layouts/default.php
and can be changed in config/config.php
. You can also override the default layout by setting $this->layout('example')
in a view, where example
will reference views/layouts/example.php
.
The main content of your view can be retrieved and displayed in the layout by calling $this->getContent();
.
Assets (images, javascript, stylesheets, etc) can be accessed by calling $this->asset('images/example.jpg')
which then retrieves the given asset relative to the assets
folder. And of course you can always add your own absolute or relative paths to the layouts, views, etc. For example:
<img src="<?php $this->asset('images/example.jpg'); ?>">
You can render partials within views. Partials are stored in the views/shared
and can be referenced from and saved in any view and layout. To render a view, use $this->render('shared/file')
. All partials are referenced relative to the views/
folder. You can also have unlimited nested folders, so $this->render('shared/some/nested/partial');
You can pass variables to nested views and a layout by assigning an array as a second parameter. For example $this->render('shared/file', array('title' => 'New Page Title'))
, that view can now access the value of title
in the sent array as $title
in that nested view. The syntax for passing a variable to be used in a layout (which can be subsequently sent to another partial) $this->layout('default', array('title' => 'New Page Title'))
(Set the first parameter to false
to load the view without a layout).
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
<header>
<?php $this->render('shared/header'); ?>
</header>
<div class="content">
<?php $this->getContent(); ?>
</div>
<footer>
<?php $this->render('shared/footer'); ?>
</footer>
</body>
</html>
<?php $this->layout('custom'); ?>
<h2>About Page</h2>
<p>Some page content here</p>
<?php $this->layout(false, array('page' => 'About'); ?>
<p>Some page content here</p>
<h2>About Page</h2>
<p>Some page content here</p>
<?php $this->render('shared/extra_content', array('content' => 'Some custom contest here.')); ?>