- added new {template} tag

This commit is contained in:
Uwe.Tews
2009-04-24 19:59:51 +00:00
parent 1ee0747801
commit e315782819
10 changed files with 186 additions and 17 deletions

View File

@@ -1,3 +1,6 @@
04/24/2009
- added new {template} tag
04/23/2009
- fixed access of special smarty variables from included template

View File

@@ -1,6 +1,6 @@
<?php
/**
* Smarty Internal Plugin Compile Capture
* Smarty Internal Plugin Compile Block
*
* Compiles the {block} tag
*
@@ -17,7 +17,7 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @return string compiled code
* @return boolean true
*/
public function compile($args, $compiler)
{

View File

@@ -2,7 +2,7 @@
/**
* Smarty Internal Plugin Compile Block Close
*
* Compiles the {/capture} tag
* Compiles the {/block} tag
*
* @package Smarty
* @subpackage Compiler

View File

@@ -32,7 +32,7 @@ class Smarty_Internal_Compile_Extend extends Smarty_Internal_CompileBase {
// create template object
$_template = new Smarty_Template ($include_file, $compiler->template);
// save file dependency
$compiler->template->file_dependency['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
$compiler->template->properties['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
// $_old_source = preg_replace ('/' . $this->smarty->left_delimiter . 'extend\s+(?:file=)?\s*(\S+?|(["\']).+?\2)' . $this->smarty->right_delimiter . '/i', '' , $compiler->template->template_source, 1);
$_old_source = $compiler->template->template_source;
$_old_source = preg_replace_callback('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')((?:\r?\n?)(.*?)(?:\r?\n?))(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/is', array('Smarty_Internal_Compile_Extend', 'saveBlockData'), $_old_source);

View File

@@ -68,7 +68,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase {
// if ($this->compiler->tag_nocache == false) {
// save file dependency
// $compiler->template->file_dependency['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
// $compiler->template->properties['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
// unset ($_template);
// }
// create template object

View File

@@ -0,0 +1,42 @@
<?php
/**
* Smarty Internal Plugin Compile Template
*
* Compiles the {template} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Template Class
*/
class Smarty_Internal_Compile_Template extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {template} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @return boolean true
*/
public function compile($args, $compiler)
{
$this->compiler = $compiler;
$this->required_attributes = array('name');
$this->optional_attributes = array('_any');
// check and get attributes
$_attr = $this->_get_attributes($args);
$save = array($_attr, $compiler->template->extracted_compiled_code, $compiler->template->extract_code);
$this->_open_tag('template', $save);
$_name = trim($_attr['name'], "'");
foreach ($_attr as $_key => $_data) {
$compiler->template->properties['template'][$_name]['parameter'][$_key] = $_data;
}
$compiler->template->extract_code = true;
$compiler->template->extracted_compiled_code = '';
$compiler->template->has_code = false;
return true;
}
}
?>

View File

@@ -0,0 +1,72 @@
<?php
/**
* Smarty Internal Plugin Compile TemplateCall
*
* Compiles the {templatecall} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile TemplateCall Class
*/
class Smarty_Internal_Compile_TemplateCall extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {templateall} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @return string compiled code
*/
public function compile($args, $compiler)
{
$this->compiler = $compiler;
$this->required_attributes = array('name');
$this->optional_attributes = array('_any');
// check and get attributes
$_attr = $this->_get_attributes($args);
// save posible attributes
if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of beind displayed
$_assign = $_attr['assign'];
}
$_name = trim( $_attr['name'],"'");
// create template object
$_output = "<?php \$_template = new Smarty_Template ('string:', \$_smarty_tpl);";
// assign default paramter
if (isset($compiler->template->properties['template'][$_name]['parameter'])) {
foreach ($compiler->template->properties['template'][$_name]['parameter'] as $_key => $_value) {
if (!isset($_attr[$_key])) {
$_output .= "\$_template->assign('$_key',$_value);";
}
}
}
// delete {include} standard attributes
unset($_attr['name'], $_attr['assign']);
// remaining attributes must be assigned as smarty variable
if (!empty($_attr)) {
// create variables
foreach ($_attr as $_key => $_value) {
$_output .= "\$_template->assign('$_key',$_value);";
}
}
if (isset($compiler->template->properties['template'][$_name]['compiled'])) {
$_compiled = str_replace(array('_%n',"'"), array('',"\'"), $compiler->template->properties['template'][$_name]['compiled']);
$_output .= "\$_template->compiled_template = '$_compiled'; \$_template->mustCompile = false;";
} else {
// for recursion
$_output .= "\$_template->compiled_template = \$_smarty_tpl->compiled_template; \$_template->mustCompile = false;";
}
// was there an assign attribute
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign($_assign,\$_smarty_tpl->smarty->fetch(\$_template)); ?>";
} else {
$_output .= "echo \$_smarty_tpl->smarty->fetch(\$_template); ?>";
}
return $_output;
}
}
?>

View File

@@ -0,0 +1,44 @@
<?php
/**
* Smarty Internal Plugin Compile Template Close
*
* Compiles the {/template} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile TemplateClose Class
*/
class Smarty_Internal_Compile_TemplateClose extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {/template} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @return boolean true
*/
public function compile($args, $compiler)
{
$this->compiler = $compiler;
$this->compiler->has_code = false;
// turn off block code extraction
$compiler->template->extract_code = false;
// check and get attributes
$this->optional_attributes = array('name');
$_attr = $this->_get_attributes($args);
$saved_data = $this->_close_tag(array('template'));
// if name does match to opening tag
if (isset($_attr['name']) && $saved_data[0]['name'] != $_attr['name']) {
$this->compiler->trigger_template_error('mismatching name attributes "' . $saved_data[0]['name'] . '" and "' . $_attr['name'] . '"');
}
$_name = trim($saved_data[0]['name'], "'");
$compiler->template->properties['template'][$_name]['compiled'] = str_replace("\n",'_%n',$compiler->template->extracted_compiled_code);
$compiler->template->extracted_compiled_code = $saved_data[1];
$compiler->template->extract_code = $saved_data[2];
return true;
}
}
?>

View File

@@ -59,7 +59,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// storage for plugin
public $plugin_data = array();
// files template is depending from
public $file_dependency = array();
public $properties = array();
// storage for block data
public $block_data = array();
@@ -89,7 +89,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
$this->caching_type = $this->smarty->default_caching_type;
$this->security = $this->smarty->security;
$this->cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($this->caching_type);
$this->parent = $_parent;
$this->parent = $_parent;
// Template resource
$this->template_resource = $template_resource;
// parse resource name
@@ -194,15 +194,18 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
public function mustCompile ()
{
if ($this->mustCompile === null) {
$this->mustCompile = ($this->usesCompiler() && ($this->force_compile || $this->isEvaluated() || ($this->smarty->compile_check && $this->getCompiledTimestamp () !== $this->getTemplateTimestamp ())));
$this->mustCompile = ($this->usesCompiler() && ($this->force_compile || $this->isEvaluated() || ($this->smarty->compile_check && $this->getCompiledTimestamp () !== $this->getTemplateTimestamp ())));
if ($this->mustCompile) {
return true;
}
// read compiled template
if ($this->compiled_template !== true && file_exists($this->getCompiledFilepath())) {
$this->compiled_template = !$this->isEvaluated() ? file_get_contents($this->getCompiledFilepath()):'';
$found = preg_match('~\<\?php /\*(.*)\*/ \?\>~', $this->compiled_template, $matches);
if ($found) {
$this->file_dependency = unserialize($matches[1]);
if (!empty($this->file_dependency)) {
foreach ($this->file_dependency['file_dependency'] as $file_to_check) {
$_properties = unserialize($matches[1]);
if (!empty($_properties['file_dependency'])) {
foreach ($_properties['file_dependency'] as $file_to_check) {
If (filemtime($file_to_check[0]) != $file_to_check[1]) {
$this->mustCompile = true;
return $this->mustCompile;
@@ -284,9 +287,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
// compiling succeded
if (!$this->isEvaluated()) {
// build file dependency string
$this->file_dependency_string = '<?php /*' . serialize($this->file_dependency) . "*/ ?>\n";
$this->properties_string = '<?php /*' . serialize($this->properties) . "*/ ?>\n";
// write compiled template
$this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->file_dependency_string . $this->dir_acc_sec_string . $this->getCompiledTemplate());
$this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->properties_string . $this->dir_acc_sec_string . $this->getCompiledTemplate());
// make template and compiled file timestamp match
touch($this->getCompiledFilepath(), $this->getTemplateTimestamp());
}
@@ -344,8 +347,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
public function writeCachedContent ()
{
// build file dependency string
$this->file_dependency_string = '<?php /*' . serialize($this->file_dependency) . "*/ ?>\n";
$this->rendered_content = $this->file_dependency_string . $this->dir_acc_sec_string . $this->rendered_content;
$this->properties_string = '<?php /*' . serialize($this->properties) . "*/ ?>\n";
$this->rendered_content = $this->properties_string . $this->dir_acc_sec_string . $this->rendered_content;
return ($this->isEvaluated() || !$this->caching) ? false : $this->cacher_object->writeCachedContent($this);
}

View File

@@ -98,8 +98,8 @@ class Smarty_Internal_TemplateCompilerBase extends Smarty_Internal_Base {
/**
* Compile Tag
*
* This is a call back from the lexer/parser
* It executes the required compile plugin for the Smarty tag
* This is a call back from the lexer/parser
* It executes the required compile plugin for the Smarty tag
*
* @param string $tag tag name
* @param array $args array with tag attributes
@@ -112,6 +112,11 @@ class Smarty_Internal_TemplateCompilerBase extends Smarty_Internal_Base {
$this->has_code = true;
$this->has_output = false;
// compile the smarty tag (required compile classes to compile the tag are autoloaded)
if (isset($this->template->properties['template'][$tag])) {
// template defined by {template} tag
$args['name'] = $tag;
$tag = 'templatecall';
}
if (!($_output = $this->$tag($args, $this)) === false) {
if ($_output !== true) {
// did we get compiled code