/** * Returns the current smarty object, creating it and the prototype if necessary * * @return Smarty object */ public function smarty() { // set time for benchmarking $time = microtime(TRUE); // see if we need to set up the smarty object for this instance if ($this->_smarty === NULL) { // see if we need to set up the prototype smarty object if (self::$_smarty_prototype === NULL) { // nearly everything can be done in the confif file $config = Kohana::$config->load('smarty'); // instantiate the prototype Smarty object require_once $config->smarty_class_file; $smarty = new Smarty(); self::$_smarty_prototype = $smarty; // set up the prototype with options from config file foreach ($config->smarty_config as $key => $value) { $smarty->{$key} = $value; } // deal with config options that are not simple properties $smarty->php_handling = constant($config->php_handling); if ($config->check_dirs) { // check we can write to the compiled templates directory if (!is_writeable($smarty->compile_dir)) { self::create_dir($smarty->compile_dir, 'Smarty compiled template'); } // if smarty caching is enabled, check we can write to the cache directory if ($smarty->caching && !is_writeable($smarty->cache_dir)) { self::create_dir($smarty->cache_dir, 'Smarty cache'); } } // now assign useful globals $smarty->assignGlobal('base_url', URL::base()); $smarty->assignGlobal('helper', new Smarty_Helper()); // and register useful plugins // set timing for benchmark self::$_init_time = microtime(TRUE) - $time; } $this->_smarty = clone self::$_smarty_prototype; $time = microtime(TRUE) - $time; $this->_clone_time = $time; self::$_total_clone_time += $time; self::$_clone_count++; } return $this->_smarty; }
/** * Returns the smarty prototype object, creating it if necessary * * @return Smarty prototype object */ public static function smarty_prototype() { // set time for benchmarking $time = microtime(TRUE); // see if we need to set up the prototype smarty object if (self::$_smarty_prototype === NULL) { // nearly everything can be done in the config file if (Kohana::VERSION > '3.2') { $config = Kohana::$config->load('smarty'); } else { $config = Kohana::config('smarty'); } // locate a Smarty class - first check if it is already loaded if (!class_exists('Smarty', false)) { // next check if a path is set in config/smarty.php if ($file = $config->smarty_class_file) { require_once $file; // save the location in case we have more than one Smarty version around self::$_smarty_path = realpath(dirname($file)) . DIRECTORY_SEPARATOR; } elseif (!class_exists('Smarty')) { // try and autoload it // if it doesn't autoload, fall back to letting Kohana find the bundled version $file = Kohana::find_file('vendor', 'smarty/libs/Smarty.class'); require_once $file; // save the location in case we have more than one Smarty version around self::$_smarty_path = realpath(dirname($file)) . DIRECTORY_SEPARATOR; } } // instantiate the prototype Smarty object $smarty = new Smarty(); self::$_smarty_prototype = $smarty; // set up the prototype with options from config file foreach ($config->smarty_config as $key => $value) { $smarty->{$key} = $value; } // deal with config options that are not simple properties $smarty->php_handling = constant($config->php_handling); // add the path to the plugins for the located Smarty distro $smarty->addPluginsDir(self::$_smarty_path . 'plugins'); // add views directories for all loaded modules (including Smarty3) $dirs = array(); foreach (Kohana::modules() as $dir) { $dirs[] = "{$dir}views"; } $smarty->addTemplateDir($dirs); if ($config->check_dirs) { // check we can write to the compiled templates directory if (!is_writeable($smarty->compile_dir)) { self::create_dir($smarty->compile_dir, 'Smarty compiled template'); } // if smarty caching is enabled, check we can write to the cache directory if ($smarty->caching && !is_writeable($smarty->cache_dir)) { self::create_dir($smarty->cache_dir, 'Smarty cache'); } } // now assign useful globals $smarty->assignGlobal('base_url', URL::base()); $smarty->assignGlobal('helper', new Smarty_Helper()); $bound = View::$_global_bound_variables; // register any globals foreach (View::$_global_data as $key => $value) { if (isset($bound[$key])) { Smarty::$global_tpl_vars[$key] = new Smarty_variable($value); Smarty::$global_tpl_vars[$key]->value =& $value; } else { $smarty->assignGlobal($key, $value); } } // and register useful plugins // add to registered template engines View::$_smarty_is_loaded = TRUE; // set timing for benchmark self::$_init_time = microtime(TRUE) - $time; } return self::$_smarty_prototype; }