Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created January 9, 2011 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/771985 to your computer and use it in GitHub Desktop.
Save martinbean/771985 to your computer and use it in GitHub Desktop.
<?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');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
@anandkunal
Copy link

Very neat. Do you know how this differs from something like Smarty?

@martinbean
Copy link
Author

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