Created
June 20, 2012 08:44
-
-
Save xianyunwuxin/2958851 to your computer and use it in GitHub Desktop.
Twig and Kohana3.2
This file contains 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 | |
// application/classes/controller.php | |
class TwigView { | |
protected static $_instance = null; | |
protected $_env; | |
public static function getInstance(){ | |
if(self::$_instance === null){ | |
self::$_instance = new TwigView(); | |
} | |
return self::$_instance; | |
} | |
public static function proxy(){ | |
$args = func_get_args(); | |
$class = ucfirst(array_shift($args)); | |
$method = array_shift($args); | |
return call_user_func_array("{$class}::{$method}",$args); | |
} | |
protected function __construct(){ | |
if (!class_exists('Twig_Autoloader')) { | |
//application/vendor/Twig | |
require APPPATH.'vendor'.DIRECTORY_SEPARATOR.'Twig'.DIRECTORY_SEPARATOR.'Autoloader.php'; | |
} | |
Twig_Autoloader::register(); | |
$loader = new Twig_Loader_Filesystem(APPPATH.'views'); | |
$this->_env = new Twig_Environment( | |
$loader, | |
array('auto_reload'=>true, 'cache'=>APPPATH.'cache'.DIRECTORY_SEPARATOR.'twig') | |
); | |
$this->_env->addFunction('kohana_*_*', new Twig_Function_Function('TwigView::proxy', array('is_safe' => array('html')))); | |
} | |
public function get_env(){ | |
return $this->_env; | |
} | |
public function render($filename, $context = array()){ | |
$response = $this->get_env()->loadTemplate($filename)->render($context); | |
return $response; | |
} | |
} |
This file contains 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 | |
// application/classes/twigview.php | |
class Controller extends Kohana_Controller { | |
protected function error_404(){ | |
} | |
protected function render_to_response($filename, $context=array()){ | |
$response = TwigView::getInstance()->render($filename, $context); | |
$this->response->body($response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment