- added support of nested {bock} tags

This commit is contained in:
Uwe.Tews
2010-02-12 01:14:34 +00:00
parent cb0ba1908a
commit 99ab6e96a5
3 changed files with 81 additions and 64 deletions

View File

@@ -1,3 +1,6 @@
12/02/2010
- added support of nested {bock} tags
10/02/2010 10/02/2010
- avoid possible notice on $smarty->cache->clear(...), $smarty->clear_cache(....) - avoid possible notice on $smarty->cache->clear(...), $smarty->clear_cache(....)
- allow Smarty tags inside <? ... ?> tags in SMARTY_PHP_QUOTE and SMARTY_PHP_PASSTHRU mode - allow Smarty tags inside <? ... ?> tags in SMARTY_PHP_QUOTE and SMARTY_PHP_PASSTHRU mode

View File

@@ -1,25 +1,25 @@
<?php <?php
/** /**
* Smarty Internal Plugin Compile extend * Smarty Internal Plugin Compile extend
* *
* Compiles the {extends} tag * Compiles the {extends} tag
* *
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Plugin Compile extend Class * Smarty Internal Plugin Compile extend Class
*/ */
class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase { class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
/** /**
* Compiles code for the {extends} tag * Compiles code for the {extends} tag
* *
* @param array $args array with attributes from parser * @param array $args array with attributes from parser
* @param object $compiler compiler object * @param object $compiler compiler object
* @return string compiled code * @return string compiled code
*/ */
public function compile($args, $compiler) public function compile($args, $compiler)
{ {
$this->compiler = $compiler; $this->compiler = $compiler;
@@ -41,11 +41,18 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_old_source, $c, PREG_OFFSET_CAPTURE)) { preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_old_source, $c, PREG_OFFSET_CAPTURE)) {
$this->compiler->trigger_template_error('unmatched {block} {/block} pairs'); $this->compiler->trigger_template_error('unmatched {block} {/block} pairs');
} }
$block_count = count($s[0]); preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_old_source, $_result, PREG_OFFSET_CAPTURE);
for ($i = 0; $i < $block_count; $i++) { $_result_count = count($_result[0]);
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%', $_i = 0;
substr($_old_source, $s[0][$i][1] + strlen($s[0][$i][0]), $c[0][$i][1] - $s[0][$i][1] - strlen($s[0][$i][0]))); while ($_i < $_result_count) {
$this->saveBlockData($block_content, $s[0][$i][0], $compiler->template); $_ii = 1;
while (!strpos($_result[0][$_i + $_ii][0], '/')) {
$_ii++;
}
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
substr($_old_source, $_result[0][$_i][1] + strlen($_result[0][$_i][0]), $_result[0][$_i-1 + 2 * $_ii][1] - $_result[0][$_i][1] - + strlen($_result[0][$_i][0])));
$this->saveBlockData($_block_content, $_result[0][$_i][0], $compiler->template);
$_i = $_i + 2 * $_ii;
} }
$compiler->template->template_source = $_template->getTemplateSource(); $compiler->template->template_source = $_template->getTemplateSource();
$compiler->template->template_filepath = $_template->getTemplateFilepath(); $compiler->template->template_filepath = $_template->getTemplateFilepath();

View File

@@ -1,17 +1,17 @@
<?php <?php
/** /**
* Smarty Internal Plugin Resource Extends * Smarty Internal Plugin Resource Extends
* *
* Implements the file system as resource for Smarty which does extend a chain of template files templates * Implements the file system as resource for Smarty which does extend a chain of template files templates
* *
* @package Smarty * @package Smarty
* @subpackage TemplateResources * @subpackage TemplateResources
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Plugin Resource Extends * Smarty Internal Plugin Resource Extends
*/ */
class Smarty_Internal_Resource_Extends { class Smarty_Internal_Resource_Extends {
public function __construct($smarty) public function __construct($smarty)
{ {
@@ -29,11 +29,11 @@ class Smarty_Internal_Resource_Extends {
public $allFilepaths = array(); public $allFilepaths = array();
/** /**
* Return flag if template source is existing * Return flag if template source is existing
* *
* @param object $_template template object * @param object $_template template object
* @return boolean result * @return boolean result
*/ */
public function isExisting($_template) public function isExisting($_template)
{ {
if ($_template->getTemplateFilepath() === false) { if ($_template->getTemplateFilepath() === false) {
@@ -43,11 +43,11 @@ class Smarty_Internal_Resource_Extends {
} }
} }
/** /**
* Get filepath to template source * Get filepath to template source
* *
* @param object $_template template object * @param object $_template template object
* @return string filepath to template source file * @return string filepath to template source file
*/ */
public function getTemplateFilepath($_template) public function getTemplateFilepath($_template)
{ {
$sha1String = ''; $sha1String = '';
@@ -67,22 +67,22 @@ class Smarty_Internal_Resource_Extends {
} }
/** /**
* Get timestamp to template source * Get timestamp to template source
* *
* @param object $_template template object * @param object $_template template object
* @return integer timestamp of template source file * @return integer timestamp of template source file
*/ */
public function getTemplateTimestamp($_template) public function getTemplateTimestamp($_template)
{ {
return filemtime($_template->getTemplateFilepath()); return filemtime($_template->getTemplateFilepath());
} }
/** /**
* Read template source from file * Read template source from file
* *
* @param object $_template template object * @param object $_template template object
* @return string content of template source file * @return string content of template source file
*/ */
public function getTemplateSource($_template) public function getTemplateSource($_template)
{ {
$this->template = $_template; $this->template = $_template;
@@ -100,13 +100,20 @@ class Smarty_Internal_Resource_Extends {
if ($_filepath != $_files[count($_files)-1]) { if ($_filepath != $_files[count($_files)-1]) {
if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $_open, PREG_OFFSET_CAPTURE) != if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $_open, PREG_OFFSET_CAPTURE) !=
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $_close, PREG_OFFSET_CAPTURE)) { preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $_close, PREG_OFFSET_CAPTURE)) {
$this->smarty->trigger_error('unmatched {block} {/block} pairs'); $this->smarty->trigger_error("unmatched {block} {/block} pairs in file '$_filepath'");
} }
$_block_count = count($_open[0]); preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
for ($_i = 0; $_i < $_block_count; $_i++) { $_result_count = count($_result[0]);
$_i = 0;
while ($_i < $_result_count) {
$_ii = 1;
while (!strpos($_result[0][$_i + $_ii][0], '/')) {
$_ii++;
}
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%', $_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
substr($_content, $_open[0][$_i][1] + strlen($_open[0][$_i][0]), $_close[0][$_i][1] - $_open[0][$_i][1] - strlen($_open[0][$_i][0]))); substr($_content, $_result[0][$_i][1]+strlen($_result[0][$_i][0]), $_result[0][$_i-1 + 2 * $_ii][1] - $_result[0][$_i][1] - +strlen($_result[0][$_i][0])));
$this->saveBlockData($_block_content, $_open[0][$_i][0], $_filepath); $this->saveBlockData($_block_content, $_result[0][$_i][0], $_filepath);
$_i = $_i + 2 * $_ii;
} }
} else { } else {
$_template->template_source = $_content; $_template->template_source = $_content;
@@ -118,7 +125,7 @@ class Smarty_Internal_Resource_Extends {
protected function saveBlockData($block_content, $block_tag, $_filepath) protected function saveBlockData($block_content, $block_tag, $_filepath)
{ {
if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) { if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) {
$this->smarty->trigger_error("'{$block_tag}' missing name attribute"); $this->smarty->trigger_error("'{$block_tag}' missing name attribute in file '$_filepath'");
} else { } else {
$_name = trim($_match[3], '\'"'); $_name = trim($_match[3], '\'"');
if (isset($this->smarty->block_data[$_name])) { if (isset($this->smarty->block_data[$_name])) {
@@ -145,11 +152,11 @@ class Smarty_Internal_Resource_Extends {
} }
/** /**
* Get filepath to compiled template * Get filepath to compiled template
* *
* @param object $_template template object * @param object $_template template object
* @return string return path to compiled template * @return string return path to compiled template
*/ */
public function getCompiledFilepath($_template) public function getCompiledFilepath($_template)
{ {
$_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null; $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;