Refactored Smarty display logic.

This commit is contained in:
Josh Sherman 2010-03-15 23:57:36 -04:00
parent a2073f71c4
commit 8c1ecea92b
3 changed files with 95 additions and 149 deletions

View file

@ -58,46 +58,38 @@ class Controller extends Object
');
}
// Loads the default module information (if any)
$basename = $this->config->module['default'];
if ($basename != false)
{
$module_class = strtr($basename, '/', '_');
$module_filename = '../modules/' . $basename . '.php';
$css_class = strtr($basename, '_', '-');
$js_filename = $basename;
unset($basename);
}
// Attempts to override the defaults with passed information (if any)
// Loads the requested module's information
if (isset($_REQUEST['module']) && trim($_REQUEST['module']) != '')
{
$new_basename = strtr($_REQUEST['module'], '-', '_');
$new_module_class = strtr($new_basename, '/', '_');
$new_module_filename = '../modules/' . $new_basename . '.php';
$new_css_class = strtr($new_basename, '_', '-');
$new_js_filename = $new_basename;
// File exists, proceed with override
if (file_exists($new_module_filename))
{
$module_class = $new_module_class;
$module_filename = $new_module_filename;
$css_class = $new_css_class;
$js_filename = $new_js_filename;
}
unset($new_basename, $new_module_class, $new_module_filename, $new_css_class, $new_js_filename);
$basename = strtr($_REQUEST['module'], '-', '_');
$module_class = strtr($basename, '/', '_');
$module_filename = MODULE_PATH . $basename . '.php';
$template_basename = $basename;
$css_class = strtr($basename, '_', '-');
$js_filename = $basename;
}
// Loads the default module information (if any)
else
{
$basename = $this->config->module['default'];
$module_class = strtr($basename, '/', '_');
$module_filename = MODULE_PATH . $basename . '.php';
$template_basename = $basename;
$css_class = strtr($basename, '_', '-');
$js_filename = $basename;
}
unset($basename);
$module_exists = (isset($module_filename) && $module_filename != null && file_exists($module_filename));
// Instantiates an instance of the module
if (isset($module_filename) && $module_filename != null && file_exists($module_filename))
if ($module_exists)
{
require_once $module_filename;
// Checks that our class exists
// @todo Probably should throw an error here?
if (class_exists($module_class))
{
$module = new $module_class;
@ -122,8 +114,14 @@ class Controller extends Object
// Starts up the display engine
$display_class = 'Display_' . $module->engine;
$template = $module->template;
$display = new $display_class($template);
$display = new $display_class($module->template, $template_basename);
// If there's no valid module or template redirect
// @todo can cause infinite loop...
if (!$module_exists && !$display->templateExists())
{
header('Location: /', 404);
}
// Attempts to execute the default method
if (method_exists($module, '__default'))

View file

@ -24,12 +24,12 @@
abstract class Display_Common extends Object
{
/**
* Template
* Templates
*
* @access protected
* @var string
*/
protected $template = null;
protected $templates = null;
/**
* Template Extension
@ -52,7 +52,7 @@ abstract class Display_Common extends Object
*
* Runs the parent's constructor and adds the module to the object.
*/
public function __construct($template)
public function __construct()
{
parent::__construct();
@ -72,19 +72,28 @@ abstract class Display_Common extends Object
header('Content-type: text/html; charset=UTF-8');
$template = TEMPLATE_PATH . $template . ($this->extension != false ? '.' . $this->extension : '');
// Loops through each passed template and variables it
foreach (func_get_args() as $template)
{
$template = TEMPLATE_PATH . $template . ($this->extension != false ? '.' . $this->extension : '');
if (file_exists($template) && is_file($template) && is_readable($template))
{
$this->template = $template;
}
else
{
// @todo Add in pretty error page.
exit('barf.');
if (file_exists($template) && is_file($template) && is_readable($template))
{
$this->templates[] = $template;
}
}
}
}
/**
* Template Exists
*
* @return boolean whether or not we have any templates defined
*/
public function templateExists()
{
return (boolean)count($this->templates);
}
/**
* Preparation Method

View file

@ -24,42 +24,53 @@
*/
class Display_Smarty extends Display_Common
{
/**
* Instance of Smarty
*
* @access private
* @var object, defaults to null
*/
private $smarty = null;
/**
* Template Extension
*
* @access protected
* @var string
*/
protected $extension = 'tpl';
public function __construct(Config $config, Error $error) {
parent::__construct($config, $error);
/**
* Render the Smarty generated pages
*/
public function render()
{
$this->smarty = new Smarty();
// Establishes our paths
$this->smarty->template_dir = SITE_PATH . '../templates/';
$this->smarty->template_dir = TEMPLATE_PATH;
$cache_dir = SMARTY_PATH . 'cache';
$compile_dir = SMARTY_PATH . 'compile';
if (!file_exists($cache_dir)) { mkdir($cache_dir, 0777, true); }
if (!file_exists($compile_dir)) { mkdir($compile_dir, 0777, true); }
if (!file_exists($cache_dir))
{
mkdir($cache_dir, 0777, true);
}
if (!file_exists($compile_dir))
{
mkdir($compile_dir, 0777, true);
}
$this->smarty->cache_dir = $cache_dir ;
$this->smarty->compile_dir = $compile_dir;
}
public function prepare() {
// Enables caching
if ($this->caching === true) {
$this->smarty->caching = 1;
$this->smarty->compile_check = true;
if (is_numeric($this->caching)) {
$this->smarty->cache_lifetime = $this->caching;
}
}
// Loads the trim whitespace filter
$this->smarty->load_filter('output', 'trimwhitespace');
/*
// @todo No functions to load currently
// Includes the PICKLES custom Smarty functions
$directory = PICKLES_PATH . 'functions/smarty/';
@ -75,95 +86,23 @@ class Display_Smarty extends Display_Common
closedir($handle);
}
}
}
*/
/**
* Render the Smarty generated pages
*/
public function render() {
// Assigns the variables and loads the template
if (is_array($this->templates))
{
$this->smarty->assign('config', $this->config);
$this->smarty->assign('module', $this->module_return);
//$this->smarty->assign('template', strtr($this->template, '-', '_'));
// Establishes the template names
$template = SITE_PATH . '../templates/' . $this->module_filename . '.tpl';
if (!file_exists($template)) {
$shared_template = PICKLES_PATH . 'common/templates/' . ($this->shared_module_filename == false || $this->template_override == true ? $this->module_filename : $this->shared_module_filename) . '.tpl';
if (file_exists($shared_template)) {
$template = $shared_template;
// Assigns the template variable if there's more than one template
if (isset($this->templates[1]))
{
$this->smarty->assign('template', $this->templates[1]);
}
$this->smarty->display($this->templates[0]);
}
$this->template = $template;
$cache_id = isset($this->cache_id) ? $this->cache_id : $this->module_filename;
$template_found = false;
// Checks that the passed in main template is for real
if (isset($this->config->templates->main)) {
// If there's no .tpl at the end, appends it
if (strstr('\.tpl', $this->config->templates['main'])) {
$this->config->templates->main .= '.tpl';
}
// Checks that the template exists
if ($this->smarty->template_exists($this->config->templates->main)) {
$template = $this->config->templates->main;
$template_found = true;
}
else {
$this->error->addError('The specified main template file (' . $this->config->templates->main . ') could not be found');
}
}
// If no main template was found, try to load the module template
if ($template_found == false) {
if ($this->smarty->template_exists($this->template) == true) {
$template = $this->template;
$template_found = true;
}
}
// If no module template is found, error out.
if ($template_found == false) {
$this->error->addError('No valid template file could be found');
}
else {
if (!$this->smarty->is_cached($template, $cache_id)) {
// Build the combined module name array and assign it
$this->module_name = str_replace('-', '_', $this->module_name);
$module_name = split('/', $this->module_name);
array_unshift($module_name, $this->module_name);
$this->smarty->assign('module_name', $module_name);
// Only assign the template if it's not the index, this avoids an infinite loop.
if ($this->template != 'index.tpl') {
$this->smarty->assign('template', strtr($this->template, '-', '_'));
}
// Loads the data from the config
$data = $this->config->getPublicData();
if (isset($data) && is_array($data)) {
$this->smarty->assign('config', $data);
}
// Loads the module's public data
// @todo For refactoring, need to change the name from data
if (isset($this->data) && is_array($this->data)) {
$this->smarty->assign('module', $this->data);
}
}
$this->smarty->display($template, $cache_id);
}
}
public function getSmartyObject() {
return $this->smarty;
}
}