/** * Execute a template macro. * Should be used only from within generated template code! * * @param $path string Template macro path */ public function executeMacro($path) { // extract macro source file from macro name, if not source file // found in $path, then the macro is assumed to be local if (preg_match('/^(.*?)\\/([a-z0-9_]*)$/i', $path, $m)) { list(, $file, $macroName) = $m; // TODO: stores a list of already prepared macro to avoid this // preparation on each call $tpl = new PHPTAL($file); $tpl->setConfigurationFrom($this); $tpl->prepare(); // save current file $currentFile = $this->_context->__file; $this->_context->__file = $tpl->__file; // require PHP generated code and execute macro function require_once $tpl->getCodePath(); $fun = $tpl->getFunctionName() . '_' . $macroName; if (!function_exists($fun)) { throw new PHPTAL_Exception("Macro '{$macroName}' is not defined in {$file}", $this->_source->getRealPath()); } $fun($this, $this->_context); // restore current file $this->_context->__file = $currentFile; } else { // call local macro $fun = $this->getFunctionName() . '_' . trim($path); if (!function_exists($fun)) { throw new PHPTAL_Exception("Macro '{$macroName}' is not defined", $this->_source->getRealPath()); } $fun($this, $this->_context); } }
/** * Execute a template macro. * Should be used only from within generated template code! * * @param $path string Template macro path */ public function executeMacro($path) { // extract macro source file from macro name, if not source file // found in $path, then the macro is assumed to be local if (preg_match('/^(.*?)\\/([a-z0-9_]*)$/i', $path, $m)) { list(, $file, $macroName) = $m; if (isset($this->externalMacroTempaltesCache[$file])) { $tpl = $this->externalMacroTempaltesCache[$file]; } else { $tpl = new PHPTAL($file); $tpl->setConfigurationFrom($this); $tpl->prepare(); // require PHP generated code require_once $tpl->getCodePath(); $this->externalMacroTempaltesCache[$file] = $tpl; if (count($this->externalMacroTempaltesCache) > 10) { $this->externalMacroTempaltesCache = array(); } // keep it small (typically only 1 or 2 external files are used) } // save current file $currentFile = $this->_context->__file; $this->_context->__file = $tpl->__file; $fun = $tpl->getFunctionName() . '_' . $macroName; if (!function_exists($fun)) { throw new PHPTAL_Exception("Macro '{$macroName}' is not defined in {$file}", $this->_source->getRealPath()); } $fun($this, $this->_context); // restore current file $this->_context->__file = $currentFile; } else { // call local macro $fun = $this->getFunctionName() . '_' . trim($path); if (!function_exists($fun)) { throw new PHPTAL_Exception("Macro '{$path}' is not defined", $this->_source->getRealPath()); } $fun($this, $this->_context); } }