Created
January 9, 2011 20:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// include both our template library and the Toro framework | |
require_once('lib/php-template.php'); | |
require_once('lib/toro.php'); | |
// set our template directory and a new instance | |
$tpl_path = dirname(__FILE__) . '/templates/'; | |
$tpl = new Template($tpl_path); | |
// set up an array for our routes | |
$routes = array( | |
array('/', 'MainHandler') | |
); | |
// we also extend the core ToroHandler class to work with our template class | |
// this is essentially creating a $tpl property we can use in extending classes | |
class MyToroHandler extends ToroHandler { | |
public function __construct() { | |
global $tpl; | |
$this->tpl = $tpl; | |
} | |
// we can also override the default 404 handler to work with our templates | |
public function __call($name, $arguments) { | |
header('HTTP/1.1 404 Not Found'); | |
$this->tpl->set('title', 'Page Not Found'); | |
$this->tpl->set('content', '<p>The page you were looking for could not be found.</p>'); | |
} | |
} | |
// set some template variables in our home page handler | |
class MainHandler extends MyToroHandler { | |
public function get() { | |
$this->tpl->set('title', 'Home'); | |
$this->tpl->set('content', '<p>Welcome to my home page!</p>'); | |
} | |
} | |
$site = new ToroApplication($routes); | |
$site->serve(); | |
// finally, print our our rendered template | |
print $tpl->fetch('index.tpl.php'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title><?php echo $title; ?></title> | |
</head> | |
<body> | |
<?php echo $content; ?> | |
</body> | |
</html> |
A lot less files ;-)
I did plan to use Smarty when I started about an hour ago, but realise Smarty had two directories of dependency files as well; whereas the template solution I've used is only the one file, so is lighter-weight, which I feel fits better with what Toro's all about: light-weightedness.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very neat. Do you know how this differs from something like Smarty?