x:template - Getting started
What is x:template?
x:template is a PHP based template engine. It's designed to be used in an object oriented MVC environment where it acts as the view part of the architecture. The primary aim of the system is to have a real separation between the logic of a template and the actual HTML template. This ensures a higher reusability of the code and a better workflow between developers and designers.
Install x:template
System requirements
The following components must be installed on your system to use x:template:
- PHP > 5.3.x
- PHP DOM Extension (This usually is boundled with PHP)
- PHP CLI 5.3 (This is needed to run the test suite from the shell)
Download and Install
Use your shell to download and test x:template:
$ cd [The directory you want to install x:template to] $ wget http://xtemplate.net/xtemplate-{version}.tar.gz $ tar -zxvf xtemplate-{$version}.tar.gz
Now there is a subfolder named "build/xtemplate-{version}" in your directory. Since x:template doesn't require any further installation operations, you're done installing right now.
Test
To make sure the engine works on your system, you should run the built in automated test suite. Open your shell and type:
$ cd build/xtemplate-{version} $ bash test.sh
If some or all test fail but your system matches the requirements, please send me your php_info() output.
Web test suite
There is also a test suite which can be accessed via web browser. To do so, open the following URI in your web browser:
http://[your host]/[path to x:template]/Test
This URI could look like this:
http://xtemplate.net/XTemplate/Test
Hello world!
Now we've done all the preparation in order to use x:template and are able to start the obligatory "Hello World!" example. First we need to create a HTML template file.
<!DOCTYPE html> <html> <head> </head> <body> <div class="hello-world">Here will be the string "Hello World!"</div> </body> </html>
We want to insert the string "Hello World!" to the DIV-tag with the class="hello-world". To do so, we need a view class:
<?php /** * x:template - PHP based template engine * Copyright (c) 2011 - 2012 by Tobias Pohlen <tobias.pohlen@xtemplate.net> * * Released under the GPL License. * * @author Tobias Pohlen * @ignore */ // Load the x:template engine require_once "../../../Engine.php"; /** * This is the view class for the getting started tutorial. * * @author Tobias Pohlen * @version $Id$ * @package xtemplate * @ignore */ class View extends XTemplate\ComfortView { const TEMPLATE = "template.html"; /** * Render the template */ protected function _render() { // Access the div element via CSS selector ".hello-world" $this[".hello-world"] = "Hello World!"; } } ?>
Now we've set up our view and we're able to render the file in our controller. To keep things easy, we just use a normal PHP file:
<?php /** * x:template - PHP based template engine * Copyright (c) 2011 - 2012 by Tobias Pohlen <tobias.pohlen@xtemplate.net> * * Released under the GPL License. * * @author Tobias Pohlen * @ignore */ // Load the view class require_once "view.php"; // Create a new instance of the view class $objView = new View; // Render the view try { $objView->render(); } catch (XTemplate\Exceptions\RenderException $objException) { die ("The view could not be rendered: ". $objException->getMessage()); } // Create the output echo $objView; ?>
The result should look like this: