Download the PHP package eisteeee/sectioned-view without Composer
On this page you can find all versions of the php package eisteeee/sectioned-view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download eisteeee/sectioned-view
More information about eisteeee/sectioned-view
Files in eisteeee/sectioned-view
Package sectioned-view
Short Description layout inheritance and sections in php's native templates
License MIT
Homepage http://github.com/eisteeee/SectionedView
Informations about the package sectioned-view
SectionedView
SectionedView implements layout inheritance and sections in php's native templates. It aims to be simple an easy to integrate
Features
- Integrates seamlessly with php's native templating (à la ``)
- easy to integrate with frameworks such as Slim PHP
Getting Started
Install
I recommend you install SectionedView via Composer:
composer.json:
"require": {
"eisteee/sectioned-view": "~1.0"
}
or using the command line
composer require eisteee/sectioned-view
Requirements
Only PHP >= 5.3.0 is required
Quick Tutorial
Instantiate a SectionedView renderer:
$template_path = __DIR__ . "/templates"; //absolute path to your template directory
$view = new \SectionedView\View($template_path);
$view->render("hello.php", array('name' => 'World'));
templates/hello.php:
Hello, <?= $name ?>!
renders
Hello, World!
in order to return the rendered contents instead of printing them to output directly use:
$template_path = __DIR__ . "/templates"; //absolute path to your template directory
$view = new \SectionedView\View($template_path, array('echo' => 'false'));
$view->render("hello.php", array('name' => 'World'));
Using layouts & sections
considering the same setup as above, now we will use a layout file
templates\layout.php:
<html>
<head>
<title>Hello</title>
</head>
<body>
</body>
</html>
templates\hello.php:
<!-- the moment you specify a layout file any text that is not within a section gets ommited -->
<!-- you can only specify one layout per file -->
Hello, <?= $name ?>!
will render:
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello, World!
</body>
</html>
Slim PHP Views
a view class for use in Slim PHP projects can be achieved as follows:
class SectionedSlimView extends \Slim\View
{
private $sectionedView;
public function __construct($template_base)
{
parent::__construct();
$this->sectionedView = new \SectionedView\View($template_base);
}
public function render($template)
{
$this->sectionedView->render($template, $this->data->all());
}
}
register it as your Slim view:
$template_path = __DIR__ . "/templates"; /* path relative to specified template_path in SectionedView */
$app = new \Slim\Slim(array(
"view" => new SectionedSlimView($template_path);
));
and you are good to go