- added registerDefaultTemplateHandler methode and functionallity

- added registerDefaultPluginHandler methode and functionallity
- added {append} tag to extend Smarty array variabled
This commit is contained in:
Uwe.Tews
2009-04-03 15:59:40 +00:00
parent b46b973b8e
commit 3c5f0d56d3
9 changed files with 714 additions and 588 deletions

View File

@@ -1,3 +1,8 @@
04/03/2006
- added registerDefaultTemplateHandler methode and functionallity
- added registerDefaultPluginHandler methode and functionallity
- added {append} tag to extend Smarty array variabled
04/02/2009
- added setter/getter methodes
- added $foo@first and $foo@last properties at {for} tag

View File

@@ -56,7 +56,9 @@ class Smarty extends Smarty_Internal_TemplateBase {
// display error on not assigned variabled
static $error_unassigned = false;
// template directory
public $template_dir = null;
public $template_dir = null;
// default template handler
public $default_template_handler_func = null;
// compile directory
public $compile_dir = null;
// plugins directory
@@ -139,6 +141,8 @@ class Smarty extends Smarty_Internal_TemplateBase {
public $plugin_search_order = array('function', 'block', 'compiler', 'class');
// plugin handler object
public $plugin_handler = null;
// default plugin handler
public $default_plugin_handler_func = null;
// registered objects
public $registered_objects = array();
// registered filters

View File

@@ -24,10 +24,10 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase {
{
$this->compiler = $compiler;
$this->required_attributes = array('var', 'value');
$this->optional_attributes = array('global','nocache');
$this->optional_attributes = array('global', 'nocache', 'index');
$_nocache = 'null';
$_global = 'null';
$_global = 'null';
// check for nocache attribute before _get_attributes because
// it shall not controll caching of the compiled code, but is a parameter
if (isset($args['nocache'])) {
@@ -44,8 +44,19 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase {
$_global = 'true';
$_global_boolean = true;
}
if (isset($_attr['index'])) {
$_index = $_attr['index'];
}
// compiled output
return "<?php \$_smarty_tpl->assign($_attr[var],$_attr[value],$_nocache,$_global);?>";
if (isset($_attr['index'])) {
if ($_attr['index'] == '') {
return "<?php \$_smarty_tpl->append($_attr[var],$_attr[value],false,$_nocache,$_global);?>";
} else {
return "<?php \$_smarty_tpl->append($_attr[var],array($_attr[index] => $_attr[value]),true,$_nocache,$_global);?>";
}
} else {
return "<?php \$_smarty_tpl->assign($_attr[var],$_attr[value],$_nocache,$_global);?>";
}
}
}

View File

@@ -56,6 +56,13 @@ class Smarty_Internal_Plugin_Handler extends Smarty_Internal_Base {
}
}
}
if (!empty($this->smarty->default_plugin_handler_func)) {
if (!is_callable($this->smarty->default_plugin_handler_func)) {
throw new Exception("Default template handler not callable");
} else {
return call_user_func_array($this->smarty->default_plugin_handler_func, array($name, $type, &$this));
}
}
return false;
}
}

View File

@@ -43,7 +43,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
public $compile_time = 0;
public $mustCompile = null;
public $suppressHeader = false;
public $extract_code = false;
public $extract_code = false;
public $extracted_compiled_code = '';
// Rendered content
public $rendered_content = null;
@@ -158,7 +158,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
if ($this->template_source === null) {
$this->resource_objects[$this->resource_type]->getTemplateSource($this);
}
return $this->template_source;
return $this->template_source;
}
/**
@@ -387,7 +387,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// disable caching for evaluated code
if ($this->isEvaluated()) {
$this->caching = false;
}
}
// checks if template exists
$this->getTemplateFilepath();
// read from cache or render
if ($this->rendered_content === null && !$this->isCached()) {
// render template (not loaded and not in cache)
@@ -488,7 +490,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// load resource handler if required
if (!isset($this->resource_objects[$this->resource_type])) {
// is this an internal or custom resource?
if (in_array($this->resource_type, array('file', 'php', 'string','extend'))) {
if (in_array($this->resource_type, array('file', 'php', 'string', 'extend'))) {
// internal, get from sysplugins dir
$_resource_class = "Smarty_Internal_Resource_{$this->resource_type}";
} else {
@@ -513,12 +515,12 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
public function buildTemplateFilepath ($file = null)
{
if ($file == null) {
$file = $this->resource_name;
}
foreach((array)$this->smarty->template_dir as $_template_dir) {
if (substr($_template_dir, -1) != DIRECTORY_SEPARATOR) {
$_template_dir .= DIRECTORY_SEPARATOR;
$file = $this->resource_name;
}
foreach((array)$this->smarty->template_dir as $_template_dir) {
if (substr($_template_dir, -1) != DIRECTORY_SEPARATOR) {
$_template_dir .= DIRECTORY_SEPARATOR;
}
$_filepath = $_template_dir . $file;
if (file_exists($_filepath))
@@ -526,6 +528,18 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
}
if (file_exists($file)) return $file;
// no tpl file found
if (!empty($this->smarty->default_template_handler_func)) {
if (!is_callable($this->smarty->default_template_handler_func)) {
throw new Exception("Default template handler not callable");
} else {
$_return = call_user_func_array($this->smarty->default_template_handler_func,
array($this->resource_type, $this->resource_name, &$this->template_source, &$this));
if ($_return == true) {
$this->isEvaluated = true;
return $_filepath;
}
}
}
throw new Exception("Unable to load template \"{$file}\"");
return false;
}

View File

@@ -76,7 +76,7 @@ class Smarty_Internal_TemplateBase {
* @param mixed $value the value to append
* @param boolean $merge flag if array elements shall be merged
*/
public function append($tpl_var, $value = null, $merge = false)
public function append($tpl_var, $value = null, $merge = false, $nocache = false, $global = false)
{
if (is_array($tpl_var)) {
// $tpl_var is an array, ignore $value
@@ -84,7 +84,7 @@ class Smarty_Internal_TemplateBase {
if ($_key != '') {
if (!isset($this->tpl_vars[$_key])) {
$this->check_tplvar($_key);
$this->tpl_vars[$_key] = new Smarty_variable();
$this->tpl_vars[$_key] = new Smarty_variable(null, $nocache, $global);
}
if (!is_array($this->tpl_vars[$_key]->value)) {
settype($this->tpl_vars[$_key]->value, 'array');
@@ -102,7 +102,7 @@ class Smarty_Internal_TemplateBase {
if ($tpl_var != '' && isset($value)) {
if (!isset($this->tpl_vars[$tpl_var])) {
$this->check_tplvar($tpl_var);
$this->tpl_vars[$tpl_var] = new Smarty_variable();
$this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $global);
}
if (!is_array($this->tpl_vars[$tpl_var]->value)) {
settype($this->tpl_vars[$tpl_var]->value, 'array');

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,35 @@
<?php
/**
* Smarty method registerDefaultPluginhandlerHandler
*
* Registers a default plugin handler
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class registerDefaultPluginHandler
*
* Registers a default plugin handler
*/
class Smarty_Method_registerDefaultPluginhandlerHandler extends Smarty_Internal_Base {
/**
* Registers a default plugin handler
*
* @param string $ |array $plugin class/methode name
*/
public function execute($plugin)
{
if (is_callable($plugin)) {
$this->smarty->default_plugin_handler_func = $plugin;
} else {
throw new Exception("Default plugin handler not callable");
}
}
}
?>

View File

@@ -0,0 +1,35 @@
<?php
/**
* Smarty method registerDefaultTemplateHandler
*
* Registers a default template handler
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class registerDefaultTemplateHandler
*
* Registers a default template handler
*/
class Smarty_Method_registerDefaultTemplateHandler extends Smarty_Internal_Base {
/**
* Registers a default template handler
*
* @param string $ |array $plugin class/methode name
*/
public function execute($plugin)
{
if (is_callable($plugin)) {
$this->smarty->default_template_handler_func = $plugin;
} else {
throw new Exception("Default template handler not callable");
}
}
}
?>