Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Suraci committed Mar 5, 2008
0 parents commit 76553dd
Show file tree
Hide file tree
Showing 13 changed files with 3,232 additions and 0 deletions.
54 changes: 54 additions & 0 deletions php/Twig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Twig
* ~~~~
*
* A simple cross-language template engine.
*
* Usage
* -----
*
* Using Twig in a application is straightfoward. All you have to do is to
* make sure you have a folder where all your templates go and another one
* where the compiled templates go (which we call the cache)::
*
* require 'Twig.php';
* $loader = new Twig_Loader('path/to/templates', 'path/to/cache');
*
* After that you can load templates using the loader::
*
* $template = $loader->getTemplate('index.html');
*
* You can render templates by using the render and display methods. display
* works like render just that it prints the output whereas render returns
* the generated source as string. Both accept an array as context::
*
* echo $template->render(array('users' => get_list_of_users()));
* $template->display(array('users' => get_list_of_users()));
*
* Custom Loaders
* --------------
*
* For many applications it's a good idea to subclass the loader to add
* support for multiple template locations. For example many applications
* support plugins and you want to allow plugins to ship themes.
*
* The easiest way is subclassing Twig_Loader and override the getFilename
* method which calculates the path to the template on the file system.
*
*
* :copyright: 2008 by Armin Ronacher.
* :license: BSD.
*/


if (!defined('TWIG_BASE'))
define('TWIG_BASE', dirname(__FILE__) . '/Twig');
define('TWIG_VERSION', '0.1-dev');


// the systems we load automatically on initialization. The compiler
// and other stuff is loaded on first request.
require TWIG_BASE . '/exceptions.php';
require TWIG_BASE . '/runtime.php';
require TWIG_BASE . '/api.php';
146 changes: 146 additions & 0 deletions php/Twig/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Twig::API
* ~~~~~~~~~
*
* The High-Level API
*
* :copyright: 2008 by Armin Ronacher.
* :license: BSD.
*/


/**
* Load the compiler system. Call this before you access the
* compiler!
*/
function twig_load_compiler()
{
if (!defined('TWIG_COMPILER_INCLUDED'))
require TWIG_BASE . '/compiler.php';
}


/**
* This class wraps a template instance as returned by the compiler and
* is usually constructed from the `Twig_Loader`.
*/
class Twig_Template
{
private $instance;

public function __construct($instance)
{
$this->instance = $instance;
}

/**
* Render the template with the given context and return it
* as string.
*/
public function render($context=NULL)
{
ob_start();
$this->display($context);
return ob_end_clean();
}

/**
* Works like `render()` but prints the output.
*/
public function display($context=NULL)
{
if (is_null($context))
$context = array();
$this->instance->render($context);
}
}

/**
* Baseclass for custom loaders. Subclasses have to provide a
* getFilename method.
*/
class Twig_BaseLoader
{
public $cache;

public function __construct($cache)
{
$this->cache = $cache;
}

public function getTemplate($name)
{
$cls = $this->requireTemplate($name);
return new Twig_Template(new $cls);
}

public function getCacheFilename($name)
{
return $this->cache . '/twig_' . md5($name) . '.cache';
}

public function requireTemplate($name)
{
$cls = '__TwigTemplate_' . md5($name);
if (!class_exists($cls)) {
$fn = $this->getFilename($name);
if (!file_exists($fn))
throw new Twig_TemplateNotFound($name);
$cache_fn = $this->getCacheFilename($name);
if (!file_exists($cache_fn) ||
filemtime($cache_fn) < filemtime($fn)) {
twig_load_compiler();
$fp = fopen($cache_fn, 'wb');
$compiler = new Twig_FileCompiler($fp);
$this->compileTemplate($name, $compiler, $fn);
fclose($fp);
}
include $cache_fn;
}
return $cls;
}

public function compileTemplate($name, $compiler=null, $fn=null)
{
twig_load_compiler();
if (is_null($compiler)) {
$compiler = new Twig_StringCompiler();
$returnCode = true;
}
else
$returnCode = false;
if (is_null($fn))
$fn = $this->getFilename($name);

$node = twig_parse(file_get_contents($fn, $name), $name);
$node->compile($compiler);
if ($returnCode)
return $compiler->getCode();
}
}


/**
* Helper class that loads templates.
*/
class Twig_Loader extends Twig_BaseLoader
{
public $folder;

public function __construct($folder, $cache)
{
parent::__construct($cache);
$this->folder = $folder;
}

public function getFilename($name)
{
$path = array();
foreach (explode('/', $name) as $part) {
if ($part[0] != '.')
array_push($path, $part);
}
return $this->folder . '/' . implode('/', $path);
}
}
Loading

0 comments on commit 76553dd

Please sign in to comment.