Skip to content

Instantly share code, notes, and snippets.

@mramsden
Created December 27, 2009 09:53
Show Gist options
  • Save mramsden/264227 to your computer and use it in GitHub Desktop.
Save mramsden/264227 to your computer and use it in GitHub Desktop.
This gist shows an approach to using Twig with the Zend Framework.
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/* Other functions may have been omitted */
protected function _initTwigView()
{
$twigView = new ZExt_Twig(APPLICATION_PATH, APPLICATION_PATH."/modules");
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper("ViewRenderer");
$viewRenderer->setView($twigView);
return $twigView;
}
}
<?php
class ZExt_Twig_Loader extends Twig_Loader
{
/**
* @var string
*/
private $_applicationRoot = null;
/**
* @var string
*/
private $_modulesDir = null;
/**
* @var string
*/
private $_viewsDir = null;
/**
* Constructor.
*
* @param string $applicationRoot The application root.
* @param string $modulesDir The directory where all modules are found.
* @param string $viewsDir The path where views should be located in both the application root and
* modules directory.
* @param string $cache The compiler cache directory.
* @param boolean $autoReload Whether to reload the template if the orginal source changed.
*
* @see Twig_Loader
*/
public function __construct($applicationRoot, $modulesDir, $viewsDir = "/views/scripts",
$cache = null, $autoReload = true)
{
$this->setApplicationRoot($applicationRoot);
$this->setModulesDir($modulesDir);
$this->setViewsDir($viewsDir);
parent::__construct($cache, $autoReload);
}
/**
* Returns the source of the next view to load based on the supplied name.
* The name in this instance should take the form 'module/controller/action',
* 'controller/action', or just 'action'.
*
* @param string $name The view to load.
*
* @return array An array consisting of the source code as the first element, and
* the last modification time as the second one or false if it's not relevant.
*/
public function getSource($name)
{
$name = $this->_parseName($name);
$templateFilePath = $this->getApplicationRoot();
if (!is_null($name['module']))
{
$templateFilePath .= DIRECTORY_SEPARATOR.$name['module'];
}
$templateFilePath .= DIRECTORY_SEPARATOR.$this->getViewsDir();
$templateFilePath .= DIRECTORY_SEPARATOR.$name['controller'].DIRECTORY_SEPARATOR.$name['action'].$name['extension'];
if (!file_exists($templateFilePath))
{
throw new Zend_Exception("The template file $templateFilePath does not exist.");
}
error_log($templateFilePath);
return array(file_get_contents($templateFilePath), filemtime($templateFilePath));
}
public function getViewsDir()
{
return $this->_viewsDir;
}
public function setViewsDir($viewsDir)
{
$this->_viewsDir = $viewsDir;
}
public function getApplicationRoot()
{
return $this->_applicationRoot;
}
public function setApplicationRoot($applicationRoot)
{
$this->_applicationRoot = $applicationRoot;
}
public function getModulesDir()
{
return $this->_modulesDir;
}
public function setModulesDir($modulesDir)
{
$this->_modulesDir = $modulesDir;
}
private function _parseName($name)
{
$matches = array();
preg_match("/(\..*)$/", $name, $matches);
$name = substr($name, 0, strpos($name, $matches[1]));
$parsedName = array('module' => null, 'controller' => 'index', 'action' => 'index', 'extension' => $matches[1]);
$parts = split("/", $name);
switch (sizeof($parts))
{
case 3:
$parsedName['module'] = array_shift($parts);
case 2:
$parsedName['controller'] = array_shift($parts);
case 1:
$parsedName['action'] = array_shift($parts);
}
return $parsedName;
}
}
<?php
class ZExt_Twig extends Zend_View_Abstract
{
private $_twig = null;
public function __construct($applicationRoot, $modulesDir = "")
{
$twigLoader = new ZExt_Twig_Loader($applicationRoot, $modulesDir);
$this->_twig = new Twig_Environment($twigLoader);
}
protected function _run()
{
$template = func_get_arg(0);
echo $template->render($this->getVars());
}
protected function _script($name)
{
return $this->_twig->loadTemplate($name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment