本文实例讲述了php封装的smarty类。分享给大家供大家参考,具体如下:

<"<" tags in templates.
   */
  const PHP_PASSTHRU = 0; //-> print tags as plain text
  const PHP_QUOTE = 1; //-> escape tags as entities
  const PHP_REMOVE = 2; //-> escape tags as entities
  const PHP_ALLOW = 3; //-> escape tags as entities
  /**
   * filter types
   */
  const FILTER_POST = 'post';
  const FILTER_PRE = 'pre';
  const FILTER_OUTPUT = 'output';
  const FILTER_VARIABLE = 'variable';
  /**
   * plugin types
   */
  const PLUGIN_FUNCTION = 'function';
  const PLUGIN_BLOCK = 'block';
  const PLUGIN_COMPILER = 'compiler';
  const PLUGIN_MODIFIER = 'modifier';
  const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
  /**#@-*/
  /**
   * assigned global tpl vars
   */
  public static $global_tpl_vars = array();
  /**
   * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
   */
  public static $_previous_error_handler = null;
  /**
   * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
   */
  public static $_muted_directories = array();
  /**
   * Flag denoting if Multibyte String functions are available
   */
  public static $_MBSTRING = SMARTY_MBSTRING;
  /**
   * The character set to adhere to (e.g. "UTF-8")
   */
  public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
  /**
   * The date format to be used internally
   * (accepts date() and strftime())
   */
  public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
  /**
   * Flag denoting if PCRE should run in UTF-8 mode
   */
  public static $_UTF8_MODIFIER = 'u';
  /**
   * Flag denoting if operating system is windows
   */
  public static $_IS_WINDOWS = false;
  /**#@+
   * variables
   */
  /**
   * auto literal on delimiters with whitspace
   *
   * @var boolean
   */
  public $auto_literal = true;
  /**
   * display error on not assigned variables
   *
   * @var boolean
   */
  public $error_unassigned = false;
  /**
   * look up relative filepaths in include_path
   *
   * @var boolean
   */
  public $use_include_path = false;
  /**
   * template directory
   *
   * @var array
   */
  private $template_dir = array();
  /**
   * joined template directory string used in cache keys
   *
   * @var string
   */
  public $joined_template_dir = null;
  /**
   * joined config directory string used in cache keys
   *
   * @var string
   */
  public $joined_config_dir = null;
  /**
   * default template handler
   *
   * @var callable
   */
  public $default_template_handler_func = null;
  /**
   * default config handler
   *
   * @var callable
   */
  public $default_config_handler_func = null;
  /**
   * default plugin handler
   *
   * @var callable
   */
  public $default_plugin_handler_func = null;
  /**
   * compile directory
   *
   * @var string
   */
  private $compile_dir = null;
  /**
   * plugins directory
   *
   * @var array
   */
  private $plugins_dir = array();
  /**
   * cache directory
   *
   * @var string
   */
  private $cache_dir = null;
  /**
   * config directory
   *
   * @var array
   */
  private $config_dir = array();
  /**
   * force template compiling"{";
  /**
   * template right-delimiter
   *
   * @var string
   */
  public $right_delimiter = "}";
  /**#@+
   * security
   */
  /**
   * class name
   * This should be instance of Smarty_Security.
   *
   * @var string
   * @see Smarty_Security
   */
  public $security_class = 'Smarty_Security';
  /**
   * implementation of security class
   *
   * @var Smarty_Security
   */
  public $security_policy = null;
  /**
   * controls handling of PHP-blocks
   *
   * @var integer
   */
  public $php_handling = self::PHP_PASSTHRU;
  /**
   * controls if the php template file resource is allowed
   *
   * @var bool
   */
  public $allow_php_templates = false;
  /**
   * Should compiled-templates be prevented from being called directly"Class '" . get_class($security_class) . "' must extend Smarty_Security.");
    }
    if ($security_class == null) {
      $security_class = $this->security_class;
    }
    if (!class_exists($security_class)) {
      throw new SmartyException("Security class '$security_class' is not defined");
    } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
      throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
    } else {
      $this->security_policy = new $security_class($this);
    }
    return $this;
  }
  /**
   * Disable security
   *
   * @return Smarty current Smarty instance for chaining
   */
  public function disableSecurity()
  {
    $this->security_policy = null;
    return $this;
  }
  /**
   * Set template directory
   *
   * @param string|array $template_dir directory(s) of template sources
   *
   * @return Smarty    current Smarty instance for chaining
   */
  public function setTemplateDir($template_dir)
  {
    $this->template_dir = array();
    foreach ((array) $template_dir as $k => $v) {
      $this->template_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
    }
    $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
    return $this;
  }
  /**
   * Add template directory(s)
   *
   * @param string|array $template_dir directory(s) of template sources
   * @param string    $key     of the array element to assign the template dir to
   *
   * @return Smarty     current Smarty instance for chaining
   * @throws SmartyException when the given template directory is not valid
   */
  public function addTemplateDir($template_dir, $key = null)
  {
    // make sure we're dealing with an array
    $this->template_dir = (array) $this->template_dir;
    if (is_array($template_dir)) {
      foreach ($template_dir as $k => $v) {
        $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
        if (is_int($k)) {
          // indexes are not merged but appended
          $this->template_dir[] = $v;
        } else {
          // string indexes are overridden
          $this->template_dir[$k] = $v;
        }
      }
    } else {
      $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($template_dir, '/\\')) . DS;
      if ($key !== null) {
        // override directory at specified index
        $this->template_dir[$key] = $v;
      } else {
        // append new directory
        $this->template_dir[] = $v;
      }
    }
    $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
    return $this;
  }
  /**
   * Get template directories
   *
   * @param mixed $index index of directory to get, null to get all
   *
   * @return array|string list of template directories, or directory of $index
   */
  public function getTemplateDir($index = null)
  {
    if ($index !== null) {
      return isset($this->template_dir[$index]) "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
   *
   * @return Smarty current Smarty instance for chaining
   */
  public function setAutoloadFilters($filters, $type = null)
  {
    if ($type !== null) {
      $this->autoload_filters[$type] = (array) $filters;
    } else {
      $this->autoload_filters = (array) $filters;
    }
    return $this;
  }
  /**
   * Add autoload filters
   *
   * @param array $filters filters to load automatically
   * @param string $type  "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
   *
   * @return Smarty current Smarty instance for chaining
   */
  public function addAutoloadFilters($filters, $type = null)
  {
    if ($type !== null) {
      if (!empty($this->autoload_filters[$type])) {
        $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters);
      } else {
        $this->autoload_filters[$type] = (array) $filters;
      }
    } else {
      foreach ((array) $filters as $key => $value) {
        if (!empty($this->autoload_filters[$key])) {
          $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value);
        } else {
          $this->autoload_filters[$key] = (array) $value;
        }
      }
    }
    return $this;
  }
  /**
   * Get autoload filters
   *
   * @param string $type type of filter to get autoloads for. Defaults to all autoload filters
   *
   * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified
   */
  public function getAutoloadFilters($type = null)
  {
    if ($type !== null) {
      return isset($this->autoload_filters[$type]) "Unknown file '{$tpl_name}'");
    }
    $this->debug_tpl = $tpl_name;
    return $this;
  }
  /**
   * creates a template object
   *
   * @param string $template  the resource handle of the template file
   * @param mixed  $cache_id  cache id to be used with this template
   * @param mixed  $compile_id compile id to be used with this template
   * @param object $parent   next higher level of Smarty variables
   * @param boolean $do_clone  flag is Smarty object shall be cloned
   *
   * @return object template object
   */
  public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
  {
    if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) {
      $parent = $cache_id;
      $cache_id = null;
    }
    if ($parent !== null && is_array($parent)) {
      $data = $parent;
      $parent = null;
    } else {
      $data = null;
    }
    // default to cache_id and compile_id of Smarty object
    $cache_id = $cache_id === null "plugin {$plugin_name} is not a valid name format");
    }
    // if type is "internal", get plugin from sysplugins
    if (strtolower($_name_parts[1]) == 'internal') {
      $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
      if (file_exists($file)) {
        require_once($file);
        return $file;
      } else {
        return false;
      }
    }
    // plugin filename is expected to be: [type].[name].php
    $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
    $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
    // loop through plugin dirs and find the plugin
    foreach ($this->getPluginsDir() as $_plugin_dir) {
      $names = array(
        $_plugin_dir . $_plugin_filename,
        $_plugin_dir . strtolower($_plugin_filename),
      );
      foreach ($names as $file) {
        if (file_exists($file)) {
          require_once($file);
          return $file;
        }
        if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
          // try PHP include_path
          if ($_stream_resolve_include_path) {
            $file = stream_resolve_include_path($file);
          } else {
            $file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
          }
          if ($file !== false) {
            require_once($file);
            return $file;
          }
        }
      }
    }
    // no plugin loaded
    return false;
  }
  /**
   * Compile all template files
   *
   * @param string $extension   file extension
   * @param bool  $force_compile force all to recompile
   * @param int  $time_limit
   * @param int  $max_errors
   *
   * @return integer number of template files recompiled
   */
  public function compileAllTemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  {
    return Smarty_Internal_Utility::compileAllTemplates($extension, $force_compile, $time_limit, $max_errors, $this);
  }
  /**
   * Compile all config files
   *
   * @param string $extension   file extension
   * @param bool  $force_compile force all to recompile
   * @param int  $time_limit
   * @param int  $max_errors
   *
   * @return integer number of template files recompiled
   */
  public function compileAllConfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
  {
    return Smarty_Internal_Utility::compileAllConfig($extension, $force_compile, $time_limit, $max_errors, $this);
  }
  /**
   * Delete compiled template file
   *
   * @param string $resource_name template name
   * @param string $compile_id  compile id
   * @param integer $exp_time   expiration time
   *
   * @return integer number of template files deleted
   */
  public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  {
    return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
  }
  /**
   * Return array of tag/attributes of all tags used by an template
   *
   * @param Smarty_Internal_Template $template
   *
   * @return array of tag/attributes
   */
  public function getTags(Smarty_Internal_Template $template)
  {
    return Smarty_Internal_Utility::getTags($template);
  }
  /**
   * Run installation test
   *
   * @param array $errors Array to write errors into, rather than outputting them
   *
   * @return boolean true if setup is fine, false if something is wrong
   */
  public function testInstall(&$errors = null)
  {
    return Smarty_Internal_Utility::testInstall($this, $errors);
  }
  /**
   * Error Handler to mute expected messages
   *
   * @link http://php.net/set_error_handler
   *
   * @param integer $errno Error level
   * @param     $errstr
   * @param     $errfile
   * @param     $errline
   * @param     $errcontext
   *
   * @return boolean
   */
  public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  {
    $_is_muted_directory = false;
    // add the SMARTY_DIR to the list of muted directories
    if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) {
      $smarty_dir = realpath(SMARTY_DIR);
      if ($smarty_dir !== false) {
        Smarty::$_muted_directories[SMARTY_DIR] = array(
          'file'  => $smarty_dir,
          'length' => strlen($smarty_dir),
        );
      }
    }
    // walk the muted directories and test against $errfile
    foreach (Smarty::$_muted_directories as $key => &$dir) {
      if (!$dir) {
        // resolve directory and length for speedy comparisons
        $file = realpath($key);
        if ($file === false) {
          // this directory does not exist, remove and skip it
          unset(Smarty::$_muted_directories[$key]);
          continue;
        }
        $dir = array(
          'file'  => $file,
          'length' => strlen($file),
        );
      }
      if (!strncmp($errfile, $dir['file'], $dir['length'])) {
        $_is_muted_directory = true;
        break;
      }
    }
    // pass to next error handler if this error did not occur inside SMARTY_DIR
    // or the error was within smarty but masked to be ignored
    if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
      if (Smarty::$_previous_error_handler) {
        return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
      } else {
        return false;
      }
    }
  }
  /**
   * Enable error handler to mute expected messages
   *
   * @return void
   */
  public static function muteExpectedErrors()
  {
    /*
      error muting is done because some people implemented custom error_handlers using
      http://php.net/set_error_handler and for some reason did not understand the following paragraph:
        It is important to remember that the standard PHP error handler is completely bypassed for the
        error types specified by error_types unless the callback function returns FALSE.
        error_reporting() settings will have no effect and your error handler will be called regardless -
        however you are still able to read the current value of error_reporting and act appropriately.
        Of particular note is that this value will be 0 if the statement that caused the error was
        prepended by the @ error-control operator.
      Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
        - @filemtime() is almost twice as fast as using an additional file_exists()
        - between file_exists() and filemtime() a possible race condition is opened,
         which does not exist using the simple @filemtime() approach.
    */
    $error_handler = array('Smarty', 'mutingErrorHandler');
    $previous = set_error_handler($error_handler);
    // avoid dead loops
    if ($previous !== $error_handler) {
      Smarty::$_previous_error_handler = $previous;
    }
  }
  /**
   * Disable error handler muting expected messages
   *
   * @return void
   */
  public static function unmuteExpectedErrors()
  {
    restore_error_handler();
  }
}
// Check if we're running on windows
Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
if (Smarty::$_CHARSET !== 'UTF-8') {
  Smarty::$_UTF8_MODIFIER = '';
}
/**
 * Smarty exception class
 *
 * @package Smarty
 */
class SmartyException extends Exception
{
  public static $escape = false;
  public function __toString()
  {
    return ' --> Smarty: ' . (self::$escape "<" tags in templates.
   */
  const PHP_PASSTHRU = 0; //-> print tags as plain text
  const PHP_QUOTE = 1; //-> escape tags as entities
  const PHP_REMOVE = 2; //-> escape tags as entities
  const PHP_ALLOW = 3; //-> escape tags as entities
  /**
   * filter types
   */
  const FILTER_POST = 'post';
  const FILTER_PRE = 'pre';
  const FILTER_OUTPUT = 'output';
  const FILTER_VARIABLE = 'variable';
  /**
   * plugin types
   */
  const PLUGIN_FUNCTION = 'function';
  const PLUGIN_BLOCK = 'block';
  const PLUGIN_COMPILER = 'compiler';
  const PLUGIN_MODIFIER = 'modifier';
  const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
  /**#@-*/
  /**
   * assigned global tpl vars
   */
  public static $global_tpl_vars = array();
  /**
   * error handler returned by set_error_hanlder() in Smarty::muteExpectedErrors()
   */
  public static $_previous_error_handler = null;
  /**
   * contains directories outside of SMARTY_DIR that are to be muted by muteExpectedErrors()
   */
  public static $_muted_directories = array();
  /**
   * Flag denoting if Multibyte String functions are available
   */
  public static $_MBSTRING = SMARTY_MBSTRING;
  /**
   * The character set to adhere to (e.g. "UTF-8")
   */
  public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET;
  /**
   * The date format to be used internally
   * (accepts date() and strftime())
   */
  public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT;
  /**
   * Flag denoting if PCRE should run in UTF-8 mode
   */
  public static $_UTF8_MODIFIER = 'u';
  /**
   * Flag denoting if operating system is windows
   */
  public static $_IS_WINDOWS = false;
  /**#@+
   * variables
   */
  /**
   * auto literal on delimiters with whitspace
   *
   * @var boolean
   */
  public $auto_literal = true;
  /**
   * display error on not assigned variables
   *
   * @var boolean
   */
  public $error_unassigned = false;
  /**
   * look up relative filepaths in include_path
   *
   * @var boolean
   */
  public $use_include_path = false;
  /**
   * template directory
   *
   * @var array
   */
  private $template_dir = array();
  /**
   * joined template directory string used in cache keys
   *
   * @var string
   */
  public $joined_template_dir = null;
  /**
   * joined config directory string used in cache keys
   *
   * @var string
   */
  public $joined_config_dir = null;
  /**
   * default template handler
   *
   * @var callable
   */
  public $default_template_handler_func = null;
  /**
   * default config handler
   *
   * @var callable
   */
  public $default_config_handler_func = null;
  /**
   * default plugin handler
   *
   * @var callable
   */
  public $default_plugin_handler_func = null;
  /**
   * compile directory
   *
   * @var string
   */
  private $compile_dir = null;
  /**
   * plugins directory
   *
   * @var array
   */
  private $plugins_dir = array();
  /**
   * cache directory
   *
   * @var string
   */
  private $cache_dir = null;
  /**
   * config directory
   *
   * @var array
   */
  private $config_dir = array();
  /**
   * force template compiling"{";
  /**
   * template right-delimiter
   *
   * @var string
   */
  public $right_delimiter = "}";
  /**#@+
   * security
   */
  /**
   * class name
   * This should be instance of Smarty_Security.
   *
   * @var string
   * @see Smarty_Security
   */
  public $security_class = 'Smarty_Security';
  /**
   * implementation of security class
   *
   * @var Smarty_Security
   */
  public $security_policy = null;
  /**
   * controls handling of PHP-blocks
   *
   * @var integer
   */
  public $php_handling = self::PHP_PASSTHRU;
  /**
   * controls if the php template file resource is allowed
   *
   * @var bool
   */
  public $allow_php_templates = false;
  /**
   * Should compiled-templates be prevented from being called directly"Class '" . get_class($security_class) . "' must extend Smarty_Security.");
    }
    if ($security_class == null) {
      $security_class = $this->security_class;
    }
    if (!class_exists($security_class)) {
      throw new SmartyException("Security class '$security_class' is not defined");
    } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
      throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
    } else {
      $this->security_policy = new $security_class($this);
    }
    return $this;
  }
  /**
   * Disable security
   *
   * @return Smarty current Smarty instance for chaining
   */
  public function disableSecurity()
  {
    $this->security_policy = null;
    return $this;
  }
  /**
   * Set template directory
   *
   * @param string|array $template_dir directory(s) of template sources
   *
   * @return Smarty    current Smarty instance for chaining
   */
  public function setTemplateDir($template_dir)
  {
    $this->template_dir = array();
    foreach ((array) $template_dir as $k => $v) {
      $this->template_dir[$k] = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
    }
    $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
    return $this;
  }
  /**
   * Add template directory(s)
   *
   * @param string|array $template_dir directory(s) of template sources
   * @param string    $key     of the array element to assign the template dir to
   *
   * @return Smarty     current Smarty instance for chaining
   * @throws SmartyException when the given template directory is not valid
   */
  public function addTemplateDir($template_dir, $key = null)
  {
    // make sure we're dealing with an array
    $this->template_dir = (array) $this->template_dir;
    if (is_array($template_dir)) {
      foreach ($template_dir as $k => $v) {
        $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($v, '/\\')) . DS;
        if (is_int($k)) {
          // indexes are not merged but appended
          $this->template_dir[] = $v;
        } else {
          // string indexes are overridden
          $this->template_dir[$k] = $v;
        }
      }
    } else {
      $v = preg_replace('#(\w+)(/|\\\\){1,}#', '$1$2', rtrim($template_dir, '/\\')) . DS;
      if ($key !== null) {
        // override directory at specified index
        $this->template_dir[$key] = $v;
      } else {
        // append new directory
        $this->template_dir[] = $v;
      }
    }
    $this->joined_template_dir = join(DIRECTORY_SEPARATOR, $this->template_dir);
    return $this;
  }
  /**
   * Get template directories
   *
   * @param mixed $index index of directory to get, null to get all
   *
   * @return array|string list of template directories, or directory of $index
   */
  public function getTemplateDir($index = null)
  {
    if ($index !== null) {
      return isset($this->template_dir[$index]) "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
   *
   * @return Smarty current Smarty instance for chaining
   */
  public function setAutoloadFilters($filters, $type = null)
  {
    if ($type !== null) {
      $this->autoload_filters[$type] = (array) $filters;
    } else {
      $this->autoload_filters = (array) $filters;
    }
    return $this;
  }
  /**
   * Add autoload filters
   *
   * @param array $filters filters to load automatically
   * @param string $type  "pre", "output", … specify the filter type to set. Defaults to none treating $filters' keys as the appropriate types
   *
   * @return Smarty current Smarty instance for chaining
   */
  public function addAutoloadFilters($filters, $type = null)
  {
    if ($type !== null) {
      if (!empty($this->autoload_filters[$type])) {
        $this->autoload_filters[$type] = array_merge($this->autoload_filters[$type], (array) $filters);
      } else {
        $this->autoload_filters[$type] = (array) $filters;
      }
    } else {
      foreach ((array) $filters as $key => $value) {
        if (!empty($this->autoload_filters[$key])) {
          $this->autoload_filters[$key] = array_merge($this->autoload_filters[$key], (array) $value);
        } else {
          $this->autoload_filters[$key] = (array) $value;
        }
      }
    }
    return $this;
  }
  /**
   * Get autoload filters
   *
   * @param string $type type of filter to get autoloads for. Defaults to all autoload filters
   *
   * @return array array( 'type1' => array( 'filter1', 'filter2', … ) ) or array( 'filter1', 'filter2', …) if $type was specified
   */
  public function getAutoloadFilters($type = null)
  {
    if ($type !== null) {
      return isset($this->autoload_filters[$type]) "Unknown file '{$tpl_name}'");
    }
    $this->debug_tpl = $tpl_name;
    return $this;
  }
  /**
   * creates a template object
   *
   * @param string $template  the resource handle of the template file
   * @param mixed  $cache_id  cache id to be used with this template
   * @param mixed  $compile_id compile id to be used with this template
   * @param object $parent   next higher level of Smarty variables
   * @param boolean $do_clone  flag is Smarty object shall be cloned
   *
   * @return object template object
   */
  public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
  {
    if ($cache_id !== null && (is_object($cache_id) || is_array($cache_id))) {
      $parent = $cache_id;
      $cache_id = null;
    }
    if ($parent !== null && is_array($parent)) {
      $data = $parent;
      $parent = null;
    } else {
      $data = null;
    }
    // default to cache_id and compile_id of Smarty object
    $cache_id = $cache_id === null "plugin {$plugin_name} is not a valid name format");
    }
    // if type is "internal", get plugin from sysplugins
    if (strtolower($_name_parts[1]) == 'internal') {
      $file = SMARTY_SYSPLUGINS_DIR . strtolower($plugin_name) . '.php';
      if (file_exists($file)) {
        require_once($file);
        return $file;
      } else {
        return false;
      }
    }
    // plugin filename is expected to be: [type].[name].php
    $_plugin_filename = "{$_name_parts[1]}.{$_name_parts[2]}.php";
    $_stream_resolve_include_path = function_exists('stream_resolve_include_path');
    // loop through plugin dirs and find the plugin
    foreach ($this->getPluginsDir() as $_plugin_dir) {
      $names = array(
        $_plugin_dir . $_plugin_filename,
        $_plugin_dir . strtolower($_plugin_filename),
      );
      foreach ($names as $file) {
        if (file_exists($file)) {
          require_once($file);
          return $file;
        }
        if ($this->use_include_path && !preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_plugin_dir)) {
          // try PHP include_path
          if ($_stream_resolve_include_path) {
            $file = stream_resolve_include_path($file);
          } else {
            $file = Smarty_Internal_Get_Include_Path::getIncludePath($file);
          }
          if ($file !== false) {
            require_once($file);
            return $file;
          }
        }
      }
    }
    // no plugin loaded
    return false;
  }
  /**
   * Compile all template files
   *
   * @param string $extension   file extension
   * @param bool  $force_compile force all to recompile
   * @param int  $time_limit
   * @param int  $max_errors
   *
   * @return integer number of template files recompiled
   */
  public function compileAllTemplates($extension = '.tpl', $force_compile = false, $time_limit = 0, $max_errors = null)
  {
    return Smarty_Internal_Utility::compileAllTemplates($extension, $force_compile, $time_limit, $max_errors, $this);
  }
  /**
   * Compile all config files
   *
   * @param string $extension   file extension
   * @param bool  $force_compile force all to recompile
   * @param int  $time_limit
   * @param int  $max_errors
   *
   * @return integer number of template files recompiled
   */
  public function compileAllConfig($extension = '.conf', $force_compile = false, $time_limit = 0, $max_errors = null)
  {
    return Smarty_Internal_Utility::compileAllConfig($extension, $force_compile, $time_limit, $max_errors, $this);
  }
  /**
   * Delete compiled template file
   *
   * @param string $resource_name template name
   * @param string $compile_id  compile id
   * @param integer $exp_time   expiration time
   *
   * @return integer number of template files deleted
   */
  public function clearCompiledTemplate($resource_name = null, $compile_id = null, $exp_time = null)
  {
    return Smarty_Internal_Utility::clearCompiledTemplate($resource_name, $compile_id, $exp_time, $this);
  }
  /**
   * Return array of tag/attributes of all tags used by an template
   *
   * @param Smarty_Internal_Template $template
   *
   * @return array of tag/attributes
   */
  public function getTags(Smarty_Internal_Template $template)
  {
    return Smarty_Internal_Utility::getTags($template);
  }
  /**
   * Run installation test
   *
   * @param array $errors Array to write errors into, rather than outputting them
   *
   * @return boolean true if setup is fine, false if something is wrong
   */
  public function testInstall(&$errors = null)
  {
    return Smarty_Internal_Utility::testInstall($this, $errors);
  }
  /**
   * Error Handler to mute expected messages
   *
   * @link http://php.net/set_error_handler
   *
   * @param integer $errno Error level
   * @param     $errstr
   * @param     $errfile
   * @param     $errline
   * @param     $errcontext
   *
   * @return boolean
   */
  public static function mutingErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  {
    $_is_muted_directory = false;
    // add the SMARTY_DIR to the list of muted directories
    if (!isset(Smarty::$_muted_directories[SMARTY_DIR])) {
      $smarty_dir = realpath(SMARTY_DIR);
      if ($smarty_dir !== false) {
        Smarty::$_muted_directories[SMARTY_DIR] = array(
          'file'  => $smarty_dir,
          'length' => strlen($smarty_dir),
        );
      }
    }
    // walk the muted directories and test against $errfile
    foreach (Smarty::$_muted_directories as $key => &$dir) {
      if (!$dir) {
        // resolve directory and length for speedy comparisons
        $file = realpath($key);
        if ($file === false) {
          // this directory does not exist, remove and skip it
          unset(Smarty::$_muted_directories[$key]);
          continue;
        }
        $dir = array(
          'file'  => $file,
          'length' => strlen($file),
        );
      }
      if (!strncmp($errfile, $dir['file'], $dir['length'])) {
        $_is_muted_directory = true;
        break;
      }
    }
    // pass to next error handler if this error did not occur inside SMARTY_DIR
    // or the error was within smarty but masked to be ignored
    if (!$_is_muted_directory || ($errno && $errno & error_reporting())) {
      if (Smarty::$_previous_error_handler) {
        return call_user_func(Smarty::$_previous_error_handler, $errno, $errstr, $errfile, $errline, $errcontext);
      } else {
        return false;
      }
    }
  }
  /**
   * Enable error handler to mute expected messages
   *
   * @return void
   */
  public static function muteExpectedErrors()
  {
    /*
      error muting is done because some people implemented custom error_handlers using
      http://php.net/set_error_handler and for some reason did not understand the following paragraph:
        It is important to remember that the standard PHP error handler is completely bypassed for the
        error types specified by error_types unless the callback function returns FALSE.
        error_reporting() settings will have no effect and your error handler will be called regardless -
        however you are still able to read the current value of error_reporting and act appropriately.
        Of particular note is that this value will be 0 if the statement that caused the error was
        prepended by the @ error-control operator.
      Smarty deliberately uses @filemtime() over file_exists() and filemtime() in some places. Reasons include
        - @filemtime() is almost twice as fast as using an additional file_exists()
        - between file_exists() and filemtime() a possible race condition is opened,
         which does not exist using the simple @filemtime() approach.
    */
    $error_handler = array('Smarty', 'mutingErrorHandler');
    $previous = set_error_handler($error_handler);
    // avoid dead loops
    if ($previous !== $error_handler) {
      Smarty::$_previous_error_handler = $previous;
    }
  }
  /**
   * Disable error handler muting expected messages
   *
   * @return void
   */
  public static function unmuteExpectedErrors()
  {
    restore_error_handler();
  }
}
// Check if we're running on windows
Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
// let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8
if (Smarty::$_CHARSET !== 'UTF-8') {
  Smarty::$_UTF8_MODIFIER = '';
}
/**
 * Smarty exception class
 *
 * @package Smarty
 */
class SmartyException extends Exception
{
  public static $escape = false;
  public function __toString()
  {
    return ' --> Smarty: ' . (self::$escape "_blank" href="https://www.jb51.net/Special/26.htm">smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com