/** * This is PHPTAL's internal function that handles * execution of macros from templates. * * $this is caller's context (the file where execution had originally started) * @param $local_tpl is PHPTAL instance of the file in which macro is defined (it will be different from $this if it's external macro call) * @access private */ public final function _executeMacroOfTempalte($path, PHPTAL $local_tpl) { // extract macro source file from macro name, if macro path does not // contain filename, 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(); // keep it small (typically only 1 or 2 external files are used) if (count($this->externalMacroTempaltesCache) > 10) { $this->externalMacroTempaltesCache = array(); } $this->externalMacroTempaltesCache[$file] = $tpl; } // save current file $currentFile = $this->_context->_file; $this->_context->_file = $tpl->_file; $fun = $tpl->getFunctionName() . '_' . strtr($macroName, "-", "_"); if (!function_exists($fun)) { throw new PHPTAL_MacroMissingException("Macro '{$macroName}' is not defined in {$file}", $this->_source->getRealPath()); } try { $fun($tpl, $this); } catch (PHPTAL_TemplateException $e) { $e->hintSrcPosition($tpl->_context->_file . '/' . $macroName, $tpl->_context->_line); $this->_context->_file = $currentFile; throw $e; } // restore current file $this->_context->_file = $currentFile; } else { // call local macro $fun = $local_tpl->getFunctionName() . '_' . strtr($path, "-", "_"); if (!function_exists($fun)) { throw new PHPTAL_MacroMissingException("Macro '{$path}' is not defined", $local_tpl->_source->getRealPath()); } $fun($local_tpl, $this); } }
/** * 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); } }