- the methodes assign_by_ref is now wrapped to assign, append_by_ref to append

- allow arrays of variables pass in display, fetch and createTemplate calls
  $data = array('foo'=>'bar','foo2'=>'blar');
  $smarty->display('my.tpl',$data);
This commit is contained in:
Uwe.Tews
2009-09-29 22:58:54 +00:00
parent 8387d1ef6f
commit 26949347e2
2 changed files with 30 additions and 34 deletions

View File

@@ -1,3 +1,9 @@
09/30/2009
- the methodes assign_by_ref is now wrapped to assign, append_by_ref to append
- allow arrays of variables pass in display, fetch and createTemplate calls
$data = array('foo'=>'bar','foo2'=>'blar');
$smarty->display('my.tpl',$data);
09/29/2009 09/29/2009
- changed {php} tag handling - changed {php} tag handling
- removed support of Smarty::instance() - removed support of Smarty::instance()

View File

@@ -15,7 +15,7 @@
*/ */
class Smarty_Internal_TemplateBase { class Smarty_Internal_TemplateBase {
// class used for templates // class used for templates
public $template_class = 'Smarty_Internal_Template'; public $template_class = 'Smarty_Internal_Template';
/** /**
* assigns a Smarty variable * assigns a Smarty variable
@@ -57,19 +57,11 @@ class Smarty_Internal_TemplateBase {
} }
/** /**
* assigns values to template variables by reference * assigns values to template variables by reference
* * wrapper to assign
* @param string $tpl_var the template variable name
* @param mixed $ &$value the referenced value to assign
* @param boolean $nocache if true any output of this variable will be not cached
* @param boolean $scope the scope the variable will have (local,parent or root)
*/ */
public function assign_by_ref($tpl_var, &$value, $nocache = false, $scope = SMARTY_LOCAL_SCOPE) public function assign_by_ref($tpl_var, &$value, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
{ {
if ($tpl_var != '') { $this->assign($tpl_var, $value, $nocache, $scope);
$this->check_tplvar($tpl_var);
$this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache, $scope);
$this->tpl_vars[$tpl_var]->value = &$value;
}
} }
/** /**
* appends values to template variables * appends values to template variables
@@ -141,27 +133,11 @@ class Smarty_Internal_TemplateBase {
/** /**
* appends values to template variables by reference * appends values to template variables by reference
* *
* @param string $tpl_var the template variable name * wrapper to append
* @param mixed $ &$value the referenced value to append
* @param boolean $merge flag if array elements shall be merged
*/ */
public function append_by_ref($tpl_var, &$value, $merge = false) public function append_by_ref($tpl_var, $value = null, $merge = false, $nocache = false, $scope = SMARTY_LOCAL_SCOPE)
{ {
if ($tpl_var != '' && isset($value)) { $this->append($tpl_var, $value, $merge, $nocache, $scope);
if (!isset($this->tpl_vars[$tpl_var])) {
$this->tpl_vars[$tpl_var] = new Smarty_variable();
}
if (!@is_array($this->tpl_vars[$tpl_var]->value)) {
settype($this->tpl_vars[$tpl_var]->value, 'array');
}
if ($merge && is_array($value)) {
foreach($value as $_key => $_val) {
$this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
}
} else {
$this->tpl_vars[$tpl_var]->value[] = &$value;
}
}
} }
/** /**
@@ -292,25 +268,39 @@ class Smarty_Internal_TemplateBase {
*/ */
public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null) public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null)
{ {
if (is_object($cache_id)) { if (is_object($cache_id) || is_array($cache_id)) {
$parent = $cache_id; $parent = $cache_id;
$cache_id = null; $cache_id = null;
} }
if (is_array($parent)) {
$data = $parent;
$parent = null;
} else {
$data = null;
}
if (!is_object($template)) { if (!is_object($template)) {
// we got a template resource // we got a template resource
$_templateId = $this->buildTemplateId ($template, $cache_id, $compile_id); $_templateId = $this->buildTemplateId ($template, $cache_id, $compile_id);
// already in template cache? // already in template cache?
if (isset($this->smarty->template_objects[$_templateId])) { if (isset($this->smarty->template_objects[$_templateId])) {
// return cached template object // return cached template object
return $this->smarty->template_objects[$_templateId]; $tpl = $this->smarty->template_objects[$_templateId];
} else { } else {
// create and cache new template object // create and cache new template object
return new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id); $tpl = new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id);
} }
} else { } else {
// just return a copy of template class // just return a copy of template class
return $template; $tpl = $template;
} }
// fill data if present
if (is_array($data)) {
// set up variable values
foreach ($data as $_key => $_val) {
$tpl->tpl_vars[$_key] = new Smarty_variable($_val);
}
}
return $tpl;
} }
/** /**