includes/classes/smarty/sysplugins/smarty_internal_templatebase.php line 232

Open in your IDE?
  1. <?php
  2. /**
  3.  * Smarty Internal Plugin Smarty Template  Base
  4.  * This file contains the basic shared methods for template handling
  5.  *
  6.  * @package    Smarty
  7.  * @subpackage Template
  8.  * @author     Uwe Tews
  9.  */
  10. /**
  11.  * Class with shared smarty/template methods
  12.  *
  13.  * @package    Smarty
  14.  * @subpackage Template
  15.  *
  16.  * @property int $_objType
  17.  *
  18.  * The following methods will be dynamically loaded by the extension handler when they are called.
  19.  * They are located in a corresponding Smarty_Internal_Method_xxxx class
  20.  *
  21.  * @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
  22.  * @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers)
  23.  * @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
  24.  * @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
  25.  * @method array getAutoloadFilters(string $type = null)
  26.  * @method string getDebugTemplate()
  27.  * @method array getDefaultModifier()
  28.  * @method array getLiterals()
  29.  * @method array getTags(mixed $template = null)
  30.  * @method object getRegisteredObject(string $object_name)
  31.  * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
  32.  * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
  33.  * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
  34.  * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
  35.  * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
  36.  * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
  37.  * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
  38.  * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
  39.  * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
  40.  * @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)
  41.  * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
  42.  * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
  43.  * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
  44.  * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
  45.  * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
  46.  * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
  47.  */
  48. abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
  49. {
  50.     /**
  51.      * Set this if you want different sets of cache files for the same
  52.      * templates.
  53.      *
  54.      * @var string
  55.      */
  56.     public $cache_id null;
  57.     /**
  58.      * Set this if you want different sets of compiled files for the same
  59.      * templates.
  60.      *
  61.      * @var string
  62.      */
  63.     public $compile_id null;
  64.     /**
  65.      * caching enabled
  66.      *
  67.      * @var int
  68.      */
  69.     public $caching Smarty::CACHING_OFF;
  70.     /**
  71.      * check template for modifications?
  72.      *
  73.      * @var int
  74.      */
  75.     public $compile_check Smarty::COMPILECHECK_ON;
  76.     /**
  77.      * cache lifetime in seconds
  78.      *
  79.      * @var integer
  80.      */
  81.     public $cache_lifetime 3600;
  82.     /**
  83.      * Array of source information for known template functions
  84.      *
  85.      * @var array
  86.      */
  87.     public $tplFunctions = array();
  88.     /**
  89.      * universal cache
  90.      *
  91.      * @var array()
  92.      */
  93.     public $_cache = array();
  94.     /**
  95.      * fetches a rendered Smarty template
  96.      *
  97.      * @param string $template   the resource handle of the template file or template object
  98.      * @param mixed  $cache_id   cache id to be used with this template
  99.      * @param mixed  $compile_id compile id to be used with this template
  100.      * @param object $parent     next higher level of Smarty variables
  101.      *
  102.      * @throws Exception
  103.      * @throws SmartyException
  104.      * @return string rendered template output
  105.      */
  106.     public function fetch($template null$cache_id null$compile_id null$parent null)
  107.     {
  108.         $result $this->_execute($template$cache_id$compile_id$parent0);
  109.         return $result === null ob_get_clean() : $result;
  110.     }
  111.     /**
  112.      * displays a Smarty template
  113.      *
  114.      * @param string $template   the resource handle of the template file or template object
  115.      * @param mixed  $cache_id   cache id to be used with this template
  116.      * @param mixed  $compile_id compile id to be used with this template
  117.      * @param object $parent     next higher level of Smarty variables
  118.      *
  119.      * @throws \Exception
  120.      * @throws \SmartyException
  121.      */
  122.     public function display($template null$cache_id null$compile_id null$parent null)
  123.     {
  124.         // display template
  125.         $this->_execute($template$cache_id$compile_id$parent1);
  126.     }
  127.     /**
  128.      * test if cache is valid
  129.      *
  130.      * @api  Smarty::isCached()
  131.      * @link http://www.smarty.net/docs/en/api.is.cached.tpl
  132.      *
  133.      * @param null|string|\Smarty_Internal_Template $template   the resource handle of the template file or template
  134.      *                                                          object
  135.      * @param mixed                                 $cache_id   cache id to be used with this template
  136.      * @param mixed                                 $compile_id compile id to be used with this template
  137.      * @param object                                $parent     next higher level of Smarty variables
  138.      *
  139.      * @return bool cache status
  140.      * @throws \Exception
  141.      * @throws \SmartyException
  142.      */
  143.     public function isCached($template null$cache_id null$compile_id null$parent null)
  144.     {
  145.         return $this->_execute($template$cache_id$compile_id$parent2);
  146.     }
  147.     /**
  148.      * fetches a rendered Smarty template
  149.      *
  150.      * @param string $template   the resource handle of the template file or template object
  151.      * @param mixed  $cache_id   cache id to be used with this template
  152.      * @param mixed  $compile_id compile id to be used with this template
  153.      * @param object $parent     next higher level of Smarty variables
  154.      * @param string $function   function type 0 = fetch,  1 = display, 2 = isCache
  155.      *
  156.      * @return mixed
  157.      * @throws \Exception
  158.      * @throws \SmartyException
  159.      */
  160.     private function _execute($template$cache_id$compile_id$parent$function)
  161.     {
  162.         $smarty $this->_getSmartyObj();
  163.         $saveVars true;
  164.         if ($template === null) {
  165.             if (!$this->_isTplObj()) {
  166.                 throw new SmartyException($function '():Missing \'$template\' parameter');
  167.             } else {
  168.                 $template $this;
  169.             }
  170.         } elseif (is_object($template)) {
  171.             /* @var Smarty_Internal_Template $template */
  172.             if (!isset($template->_objType) || !$template->_isTplObj()) {
  173.                 throw new SmartyException($function '():Template object expected');
  174.             }
  175.         } else {
  176.             // get template object
  177.             $saveVars false;
  178.             $template $smarty->createTemplate($template$cache_id$compile_id$parent $parent $thisfalse);
  179.             if ($this->_objType === 1) {
  180.                 // set caching in template object
  181.                 $template->caching $this->caching;
  182.             }
  183.         }
  184.         // make sure we have integer values
  185.         $template->caching = (int)$template->caching;
  186.         // fetch template content
  187.         $level ob_get_level();
  188.         try {
  189.             $_smarty_old_error_level =
  190.                 isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) : null;
  191.             if ($this->_objType === 2) {
  192.                 /* @var Smarty_Internal_Template $this */
  193.                 $template->tplFunctions $this->tplFunctions;
  194.                 $template->inheritance $this->inheritance;
  195.             }
  196.             /* @var Smarty_Internal_Template $parent */
  197.             if (isset($parent->_objType) && ($parent->_objType === 2) && !empty($parent->tplFunctions)) {
  198.                 $template->tplFunctions array_merge($parent->tplFunctions$template->tplFunctions);
  199.             }
  200.             if ($function === 2) {
  201.                 if ($template->caching) {
  202.                     // return cache status of template
  203.                     if (!isset($template->cached)) {
  204.                         $template->loadCached();
  205.                     }
  206.                     $result $template->cached->isCached($template);
  207.                     Smarty_Internal_Template::$isCacheTplObj$template->_getTemplateId() ] = $template;
  208.                 } else {
  209.                     return false;
  210.                 }
  211.             } else {
  212.                 if ($saveVars) {
  213.                     $savedTplVars $template->tpl_vars;
  214.                     $savedConfigVars $template->config_vars;
  215.                 }
  216.                 ob_start();
  217.                 $template->_mergeVars();
  218.                 if (!empty(Smarty::$global_tpl_vars)) {
  219.                     $template->tpl_vars array_merge(Smarty::$global_tpl_vars$template->tpl_vars);
  220.                 }
  221.                 $result $template->render(false$function);
  222.                 $template->_cleanUp();
  223.                 if ($saveVars) {
  224.                     $template->tpl_vars $savedTplVars;
  225.                     $template->config_vars $savedConfigVars;
  226.                 } else {
  227.                     if (!$function && !isset(Smarty_Internal_Template::$tplObjCache$template->templateId ])) {
  228.                         $template->parent null;
  229.                         $template->tpl_vars $template->config_vars = array();
  230.                         Smarty_Internal_Template::$tplObjCache$template->templateId ] = $template;
  231.                     }
  232.                 }
  233.             }
  234.             if (isset($_smarty_old_error_level)) {
  235.                 error_reporting($_smarty_old_error_level);
  236.             }
  237.             return $result;
  238.         } catch (Exception $e) {
  239.             while (ob_get_level() > $level) {
  240.                 ob_end_clean();
  241.             }
  242.             if (isset($_smarty_old_error_level)) {
  243.                 error_reporting($_smarty_old_error_level);
  244.             }
  245.             throw $e;
  246.         }
  247.     }
  248.     /**
  249.      * Registers plugin to be used in templates
  250.      *
  251.      * @api  Smarty::registerPlugin()
  252.      * @link http://www.smarty.net/docs/en/api.register.plugin.tpl
  253.      *
  254.      * @param string   $type       plugin type
  255.      * @param string   $name       name of template tag
  256.      * @param callback $callback   PHP callback to register
  257.      * @param bool     $cacheable  if true (default) this function is cache able
  258.      * @param mixed    $cache_attr caching attributes if any
  259.      *
  260.      * @return \Smarty|\Smarty_Internal_Template
  261.      * @throws \SmartyException
  262.      */
  263.     public function registerPlugin($type$name$callback$cacheable true$cache_attr null)
  264.     {
  265.         return $this->ext->registerPlugin->registerPlugin($this$type$name$callback$cacheable$cache_attr);
  266.     }
  267.     /**
  268.      * load a filter of specified type and name
  269.      *
  270.      * @api  Smarty::loadFilter()
  271.      * @link http://www.smarty.net/docs/en/api.load.filter.tpl
  272.      *
  273.      * @param string $type filter type
  274.      * @param string $name filter name
  275.      *
  276.      * @return bool
  277.      * @throws \SmartyException
  278.      */
  279.     public function loadFilter($type$name)
  280.     {
  281.         return $this->ext->loadFilter->loadFilter($this$type$name);
  282.     }
  283.     /**
  284.      * Registers a filter function
  285.      *
  286.      * @api  Smarty::registerFilter()
  287.      * @link http://www.smarty.net/docs/en/api.register.filter.tpl
  288.      *
  289.      * @param string      $type filter type
  290.      * @param callback    $callback
  291.      * @param string|null $name optional filter name
  292.      *
  293.      * @return \Smarty|\Smarty_Internal_Template
  294.      * @throws \SmartyException
  295.      */
  296.     public function registerFilter($type$callback$name null)
  297.     {
  298.         return $this->ext->registerFilter->registerFilter($this$type$callback$name);
  299.     }
  300.     /**
  301.      * Registers object to be used in templates
  302.      *
  303.      * @api  Smarty::registerObject()
  304.      * @link http://www.smarty.net/docs/en/api.register.object.tpl
  305.      *
  306.      * @param string $object_name
  307.      * @param object $object                     the referenced PHP object to register
  308.      * @param array  $allowed_methods_properties list of allowed methods (empty = all)
  309.      * @param bool   $format                     smarty argument format, else traditional
  310.      * @param array  $block_methods              list of block-methods
  311.      *
  312.      * @return \Smarty|\Smarty_Internal_Template
  313.      * @throws \SmartyException
  314.      */
  315.     public function registerObject(
  316.         $object_name,
  317.         $object,
  318.         $allowed_methods_properties = array(),
  319.         $format true,
  320.         $block_methods = array()
  321.     ) {
  322.         return $this->ext->registerObject->registerObject(
  323.             $this,
  324.             $object_name,
  325.             $object,
  326.             $allowed_methods_properties,
  327.             $format,
  328.             $block_methods
  329.         );
  330.     }
  331.     /**
  332.      * @param int $compile_check
  333.      */
  334.     public function setCompileCheck($compile_check)
  335.     {
  336.         $this->compile_check = (int)$compile_check;
  337.     }
  338.     /**
  339.      * @param int $caching
  340.      */
  341.     public function setCaching($caching)
  342.     {
  343.         $this->caching = (int)$caching;
  344.     }
  345.     /**
  346.      * @param int $cache_lifetime
  347.      */
  348.     public function setCacheLifetime($cache_lifetime)
  349.     {
  350.         $this->cache_lifetime $cache_lifetime;
  351.     }
  352.     /**
  353.      * @param string $compile_id
  354.      */
  355.     public function setCompileId($compile_id)
  356.     {
  357.         $this->compile_id $compile_id;
  358.     }
  359.     /**
  360.      * @param string $cache_id
  361.      */
  362.     public function setCacheId($cache_id)
  363.     {
  364.         $this->cache_id $cache_id;
  365.     }
  366. }