3 * Smarty Internal Plugin Resource Extends
6 * @subpackage TemplateResources
12 * Smarty Internal Plugin Resource Extends
14 * Implements the file system as resource for Smarty which {extend}s a chain of template files templates
17 * @subpackage TemplateResources
19 class Smarty_Internal_Resource_Extends extends Smarty_Resource {
22 * mbstring.overload flag
26 public $mbstring_overload = 0;
29 * populate Source Object with meta data from Resource
31 * @param Smarty_Template_Source $source source object
32 * @param Smarty_Internal_Template $_template template object
34 public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template=null)
38 $components = explode('|', $source->name);
40 foreach ($components as $component) {
41 $s = Smarty_Resource::source(null, $source->smarty, $component);
42 if ($s->type == 'php') {
43 throw new SmartyException("Resource type {$s->type} cannot be used with the extends resource type");
45 $sources[$s->uid] = $s;
47 if ($_template && $_template->smarty->compile_check) {
48 $exists = $exists && $s->exists;
51 $source->components = $sources;
52 $source->filepath = $s->filepath;
53 $source->uid = sha1($uid);
54 if ($_template && $_template->smarty->compile_check) {
55 $source->timestamp = $s->timestamp;
56 $source->exists = $exists;
58 // need the template at getContent()
59 $source->template = $_template;
63 * populate Source Object with timestamp and exists from Resource
65 * @param Smarty_Template_Source $source source object
67 public function populateTimestamp(Smarty_Template_Source $source)
69 $source->exists = true;
70 foreach ($source->components as $s) {
71 $source->exists = $source->exists && $s->exists;
73 $source->timestamp = $s->timestamp;
77 * Load template's source from files into current template object
79 * @param Smarty_Template_Source $source source object
80 * @return string template source
81 * @throws SmartyException if source cannot be loaded
83 public function getContent(Smarty_Template_Source $source)
85 if (!$source->exists) {
86 throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
89 $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
90 $_rdl = preg_quote($source->smarty->right_delimiter);
91 $_ldl = preg_quote($source->smarty->left_delimiter);
92 $_components = array_reverse($source->components);
93 $_first = reset($_components);
94 $_last = end($_components);
96 foreach ($_components as $_component) {
97 // register dependency
98 if ($_component != $_first) {
99 $source->template->properties['file_dependency'][$_component->uid] = array($_component->filepath, $_component->timestamp, $_component->type);
103 $source->filepath = $_component->filepath;
104 $_content = $_component->content;
107 if ($_component != $_last) {
108 if (preg_match_all("!({$_ldl}block\s(.+?){$_rdl})!", $_content, $_open) !=
109 preg_match_all("!({$_ldl}/block{$_rdl})!", $_content, $_close)) {
110 throw new SmartyException("unmatched {block} {/block} pairs in template {$_component->type} '{$_component->name}'");
112 preg_match_all("!{$_ldl}block\s(.+?){$_rdl}|{$_ldl}/block{$_rdl}|{$_ldl}\*([\S\s]*?)\*{$_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
113 $_result_count = count($_result[0]);
115 while ($_start+1 < $_result_count) {
118 if (($this->mbstring_overload ? mb_substr($_result[0][$_start][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
122 while ($_level != 0) {
124 if (($this->mbstring_overload ? mb_substr($_result[0][$_start + $_end][0],0,mb_strlen($source->smarty->left_delimiter,'latin1')+1, 'latin1') : substr($_result[0][$_start + $_end][0],0,strlen($source->smarty->left_delimiter)+1)) == $source->smarty->left_delimiter.'*') {
127 if (!strpos($_result[0][$_start + $_end][0], '/')) {
133 $_block_content = str_replace($source->smarty->left_delimiter . '$smarty.block.parent' . $source->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
134 ($this->mbstring_overload ? mb_substr($_content, $_result[0][$_start][1] + mb_strlen($_result[0][$_start][0], 'latin1'), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + mb_strlen($_result[0][$_start][0], 'latin1'), 'latin1') : substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0]))));
135 Smarty_Internal_Compile_Block::saveBlockData($_block_content, $_result[0][$_start][0], $source->template, $_component->filepath);
136 $_start = $_start + $_end + 1;
145 * Determine basename for compiled filename
147 * @param Smarty_Template_Source $source source object
148 * @return string resource's basename
150 public function getBasename(Smarty_Template_Source $source)
152 return str_replace(':', '.', basename($source->filepath));