Skip to content

Instantly share code, notes, and snippets.

@chucktrukk
Created September 26, 2011 20:00
Show Gist options
  • Save chucktrukk/1243242 to your computer and use it in GitHub Desktop.
Save chucktrukk/1243242 to your computer and use it in GitHub Desktop.
Revo Snippet
<?php
if( !defined('VENDOR_PATH')) {
define('VENDOR_PATH', MODX_BASE_PATH.'/vendors');
}
class RevoSnippet
{
public function __construct()
{
$this->defaults = array(
'load_twig' => true,
'twig_path' => array(
MODX_BASE_PATH,
),
'twig_cache' => MODX_BASE_PATH.'assets/cache/twig/',
'twig_loader' => 'Twig_Loader_Filesystem',
);
$this->pre_init();
$this->init();
$this->post_init();
}
public function pre_init() {}
public function init()
{
global $modx;
$this->config = array_merge($this->defaults, (array) $modx->event->params);
$this->modx = &$modx;
$this->request = new HTTPRequest;
if($this->config['load_twig']) {
$this->load_twig();
}
$this->set_page();
}
protected function load_twig()
{
$folder = VENDOR_PATH.'twig/lib/Twig/';
Autoload::add_directory(VENDOR_PATH.'twig/lib/');
Twig_Autoloader::register();
$this->twig = $this->instantiate_twig($this->config);
}
public function instantiate_twig($config)
{
$path = $config['twig_path'];
$cache = $config['twig_cache'];
$loader_class = $config['twig_loader'];
$loader = new $loader_class($path);
$twig = new Twig_Environment($loader, array(
'debug' => true,
'cache' => $cache,
'auto_reload' => true,
));
return $twig;
}
public function post_init() {}
public function render($template, $twigInstance = false)
{
if($twigInstance) {
$twig = $twigInstance;
} else {
$twig = $this->twig;
}
foreach(get_object_vars($this) as $property => $value) {
$params[$property] = $value;
}
$params['this'] = $this;
try {
$template = $twig->loadTemplate($template);
} catch(Exception $e) {
return "Unable to find template '{$template}'";
return $e->getMessage();
}
try {
return $template->render($params);
} catch(Exception $e) {
return $e->getMessage();
}
}
public function set_page()
{
if(!$this->modx->isFrontend()) {
return;
}
$this->page = $this->modx->resource->toArray();
}
public function execute()
{
}
}
@chucktrukk
Copy link
Author

modx->isFrontend()) { return; } $this->page = $this->modx->resource->toArray(); } ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment