Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 7de7ff1

Browse files
fix: handle templates being loaded in multiple scopes
1 parent fd7ca15 commit 7de7ff1

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

lib/Dwoo/Compiler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,11 @@ public function getUsedPlugins()
533533
* @param string $uuid unique id of the function
534534
* @param string $body function php code
535535
*/
536-
public function addTemplatePlugin($name, array $params, $uuid, $body = null)
536+
public function addTemplatePlugin($name, array $params, $uuid, $body = null, Dwoo_ITemplate $sourceTpl = null)
537537
{
538-
$this->templatePlugins[$name] = array('params' => $params, 'body' => $body, 'uuid' => $uuid);
538+
if (!array_key_exists($name, $this->templatePlugins) || $this->templatePlugins[$name]['uuid'] == $uuid) {
539+
$this->templatePlugins[$name] = array('params'=> $params, 'body' => $body, 'uuid' => $uuid, 'sourceTpl' => $sourceTpl);
540+
}
539541
}
540542

541543
/**
@@ -2031,7 +2033,7 @@ protected function parseFunction($in, $from, $to, $parsingParams = false, $curBl
20312033
} elseif ($pluginType & Dwoo_Core::TEMPLATE_PLUGIN) {
20322034
array_unshift($params, '$this');
20332035
$params = self::implode_r($params);
2034-
$output = 'Dwoo_Plugin_'.$func.'_'.$this->templatePlugins[$func]['uuid'].'('.$params.')';
2036+
$output = 'Dwoo_Plugin_'.$func.'('.$params.')';
20352037
$this->templatePlugins[$func]['called'] = true;
20362038
}
20372039

lib/plugins/builtin/blocks/template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function postProcessing(Dwoo_Compiler $compiler, array $params, $p
7171
}
7272
$init .= '/* -- template start output */';
7373

74-
$funcName = 'Dwoo_Plugin_'.$params['name'].'_'.$params['uuid'];
74+
$funcName = 'Dwoo_Plugin_'.$params['name'];
7575

7676
$search = array(
7777
'$this->charset',

lib/plugins/builtin/functions/load_templates.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,46 @@ function Dwoo_Plugin_load_templates_compile(Dwoo_Compiler $compiler, $file)
4747

4848
$cmp = clone $compiler;
4949
$cmp->compile($compiler->getDwoo(), $tpl);
50-
foreach ($cmp->getTemplatePlugins() as $template => $args) {
51-
$compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body']);
50+
$usedTemplates = array($tpl);
51+
foreach ($cmp->getTemplatePlugins() as $template=>$args) {
52+
if (isset($args['sourceTpl'])) {
53+
$sourceTpl = $args['sourceTpl'];
54+
} else {
55+
$sourceTpl = $tpl;
56+
}
57+
58+
$compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body'], $sourceTpl);
59+
60+
if (!in_array($sourceTpl, $usedTemplates, true)) {
61+
$usedTemplates[] = $sourceTpl;
62+
}
5263
}
53-
foreach ($cmp->getUsedPlugins() as $plugin => $type) {
64+
foreach ($cmp->getUsedPlugins() as $plugin=>$type) {
5465
$compiler->addUsedPlugin($plugin, $type);
5566
}
5667

5768
$out = '\'\';// checking for modification in '.$resource.':'.$identifier."\r\n";
5869

59-
$modCheck = $tpl->getIsModifiedCode();
70+
foreach ($usedTemplates AS $usedTemplate) {
71+
$modCheck = $usedTemplate->getIsModifiedCode();
6072

61-
if ($modCheck) {
62-
$out .= 'if (!('.$modCheck.')) { ob_end_clean(); return false; }';
63-
} else {
64-
$out .= 'try {
65-
$tpl = $this->templateFactory("'.$resource.'", "'.$identifier.'");
73+
if ($modCheck) {
74+
$out .= 'if (!('.$modCheck.')) { ob_end_clean(); return false; }';
75+
} else {
76+
$usedTemplateResourceName = $usedTemplate->getResourceName();
77+
$usedTemplateResourceIdentifier = $usedTemplate->getResourceIdentifier();
78+
$out .= '
79+
try {
80+
$tpl = $this->templateFactory("'.$usedTemplateResourceName.'", "'.$usedTemplateResourceIdentifier.'");
6681
} catch (Dwoo_Exception $e) {
67-
$this->triggerError(\'Load Templates : Resource <em>'.$resource.'</em> was not added to Dwoo, can not extend <em>'.$identifier.'</em>\', E_USER_WARNING);
82+
$this->triggerError(\'Load Templates : Resource <em>'.$usedTemplateResourceName.'</em> was not added to Dwoo, can not extend <em>'.$usedTemplateResourceIdentifier.'</em>\', E_USER_WARNING);
6883
}
6984
if ($tpl === null)
70-
$this->triggerError(\'Load Templates : Resource "'.$resource.':'.$identifier.'" was not found.\', E_USER_WARNING);
85+
$this->triggerError(\'Load Templates : Resource "'.$usedTemplateResourceName.':'.$usedTemplateResourceIdentifier.'" was not found.\', E_USER_WARNING);
7186
elseif ($tpl === false)
72-
$this->triggerError(\'Load Templates : Resource "'.$resource.'" does not support extends.\', E_USER_WARNING);
73-
if ($tpl->getUid() != "'.$tpl->getUid().'") { ob_end_clean(); return false; }';
87+
$this->triggerError(\'Load Templates : Resource "'.$usedTemplateResourceName.'" does not support extends.\', E_USER_WARNING);
88+
if ($tpl->getUid() != "'.$usedTemplate->getUid().'") { ob_end_clean(); return false; }';
89+
}
7490
}
7591

7692
return $out;

0 commit comments

Comments
 (0)