/** * Get the evaluated contents of the view. * * @param string $path * @param array $data * * @throws \Exception * @return string */ public function get($path, array $data = array()) { ob_start(); try { $smarty = new \Smarty(); $smarty->caching = $this->config->get('smarty-view::caching'); $smarty->debugging = $this->config->get('smarty-view::debugging'); $smarty->cache_lifetime = $this->config->get('smarty-view::cache_lifetime'); $smarty->compile_check = $this->config->get('smarty-view::compile_check'); $smarty->error_reporting = $this->config->get('smarty-view::error_reporting'); if (\App::environment('local')) { $smarty->force_compile = true; } $smarty->setTemplateDir($this->config->get('smarty-view::template_dir')); $smarty->setCompileDir($this->config->get('smarty-view::compile_dir')); $smarty->setCacheDir($this->config->get('smarty-view::cache_dir')); $smarty->registerResource('view', new ViewResource()); $smarty->addPluginsDir(__DIR__ . '/plugins'); foreach ((array) $this->config->get('smarty-view::plugins_path', array()) as $pluginPath) { $smarty->addPluginsDir($pluginPath); } foreach ($data as $key => $value) { $smarty->assign($key, $value); } $smarty->display($path); } catch (\Exception $e) { ob_get_clean(); throw $e; } return ltrim(ob_get_clean()); }
/** * Инициализация шаблонизатора * */ protected function _tplInit() { if ($this->oSmarty) { return; } // * Создаём объект Smarty $this->oSmarty = $this->CreateSmartyObject(); // * Устанавливаем необходимые параметры для Smarty $this->oSmarty->compile_check = (bool) Config::Get('smarty.compile_check'); $this->oSmarty->force_compile = (bool) Config::Get('smarty.force_compile'); $this->oSmarty->merge_compiled_includes = (bool) Config::Get('smarty.merge_compiled_includes'); // * Подавляем NOTICE ошибки - в этом вся прелесть смарти ) $this->oSmarty->error_reporting = error_reporting() & ~E_NOTICE; // * Папки расположения шаблонов по умолчанию $aDirs = F::File_NormPath(F::Str2Array(Config::Get('path.smarty.template'))); if (sizeof($aDirs) == 1) { $sDir = $aDirs[0]; $aDirs['themes'] = F::File_NormPath($sDir . '/themes'); $aDirs['tpls'] = F::File_NormPath($sDir . '/tpls'); } $this->oSmarty->setTemplateDir($aDirs); if (Config::Get('smarty.dir.templates')) { $this->oSmarty->addTemplateDir(F::File_NormPath(F::Str2Array(Config::Get('smarty.dir.templates')))); } // * Для каждого скина устанавливаем свою директорию компиляции шаблонов $sCompilePath = F::File_NormPath(Config::Get('path.smarty.compiled')); F::File_CheckDir($sCompilePath); $this->oSmarty->setCompileDir($sCompilePath); $this->oSmarty->setCacheDir(Config::Get('path.smarty.cache')); // * Папки расположения пдагинов Smarty $this->oSmarty->addPluginsDir(array(Config::Get('path.smarty.plug'), 'plugins')); if (Config::Get('smarty.dir.plugins')) { $this->oSmarty->addPluginsDir(F::File_NormPath(F::Str2Array(Config::Get('smarty.dir.plugins')))); } $this->oSmarty->default_template_handler_func = array($this, 'SmartyDefaultTemplateHandler'); // * Параметры кеширования, если заданы if (Config::Get('smarty.cache_lifetime')) { $this->oSmarty->caching = Smarty::CACHING_LIFETIME_SAVED; $this->oSmarty->cache_lifetime = F::ToSeconds(Config::Get('smarty.cache_lifetime')); } // Settings for Smarty 3.1.16 and more $this->oSmarty->inheritance_merge_compiled_includes = false; F::IncludeFile('./plugs/resource.file.php'); $this->oSmarty->registerResource('file', new Smarty_Resource_File()); // Mutes expected Smarty minor errors $this->oSmarty->muteExpectedErrors(); }
public function getSmarty() { $smarty = new \Smarty(); $finder = $this->app['view.finder']; $extension = $this->app['config']->get('smartyview::extension', 'tpl'); $escapeHTML = $this->app['config']->get('smartyview::escape_html', true); $allowOtherTemplateTypes = $this->app['config']->get('smartyview::allow_other_template_types', true); $errorReporting = $this->app['config']->get('smartyview::error_reporting', 0); $shouldCache = $this->app['config']->get('smartyview::cache', false); $compileDir = $this->app['path.storage'] . '/views/smarty/compile'; $cacheDir = $this->app['path.storage'] . '/views/smarty/cache'; $delimiters = $this->app['config']->get('smartyview::delimiters', array('left' => '{', 'right' => '}')); $plugins = $this->app['config']->get('smartyview::plugins', array('url' => new URLPlugin(), 'localize' => new LocalizePlugin())); $resources = $this->app['config']->get('smartyview::resources', array('laravel' => new LaravelResource($finder, $extension))); $defaultResource = $this->app['config']->get('smartyview::default_resource', empty($resources) ? null : key($resources)); $smarty->escape_html = $escapeHTML; $smarty->error_reporting = $errorReporting; $smarty->setCompileDir($compileDir); $smarty->setCacheDir($cacheDir); $smarty->left_delimiter = $delimiters['left']; $smarty->right_delimiter = $delimiters['right']; if ($shouldCache) { $smarty->compile_check = true; $smarty->caching = \Smarty::CACHING_LIFETIME_SAVED; } foreach ($plugins as $name => $plugin) { $smarty->registerPlugin($plugin->getType(), $name, array($plugin, 'pluginCallback')); } foreach ($resources as $name => $resource) { $smarty->registerResource($name, $resource); } if (!is_null($defaultResource)) { $smarty->default_resource_type = $defaultResource; } if ($allowOtherTemplateTypes) { $smarty->template_class = '\\SmartyView\\Smarty\\Template\\Laravel'; } $this->app['events']->fire('smartyview.smarty', array('smarty' => $smarty)); return $smarty; }
/** * Load Resource Handler * * @param Smarty $smarty smarty object * @param string $type name of the resource * @return Smarty_Resource Resource Handler */ public static function load(Smarty $smarty, $type) { // try smarty's cache if (isset($smarty->_resource_handlers[$type])) { return $smarty->_resource_handlers[$type]; } // try registered resource if (isset($smarty->registered_resources[$type])) { if ($smarty->registered_resources[$type] instanceof Smarty_Resource) { $smarty->_resource_handlers[$type] = $smarty->registered_resources[$type]; // note registered to smarty is not kept unique! return $smarty->_resource_handlers[$type]; } if (!isset(self::$resources['registered'])) { self::$resources['registered'] = new Smarty_Internal_Resource_Registered(); } if (!isset($smarty->_resource_handlers[$type])) { $smarty->_resource_handlers[$type] = self::$resources['registered']; } return $smarty->_resource_handlers[$type]; } // try sysplugins dir if (isset(self::$sysplugins[$type])) { if (!isset(self::$resources[$type])) { $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type); self::$resources[$type] = new $_resource_class(); } return $smarty->_resource_handlers[$type] = self::$resources[$type]; } // try plugins dir $_resource_class = 'Smarty_Resource_' . ucfirst($type); if ($smarty->loadPlugin($_resource_class)) { if (isset(self::$resources[$type])) { return $smarty->_resource_handlers[$type] = self::$resources[$type]; } if (class_exists($_resource_class, false)) { self::$resources[$type] = new $_resource_class(); return $smarty->_resource_handlers[$type] = self::$resources[$type]; } else { $smarty->registerResource($type, array("smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp", "smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted")); // give it another try, now that the resource is registered properly return self::load($smarty, $type); } } // try streams $_known_stream = stream_get_wrappers(); if (in_array($type, $_known_stream)) { // is known stream if (is_object($smarty->security_policy)) { $smarty->security_policy->isTrustedStream($type); } if (!isset(self::$resources['stream'])) { self::$resources['stream'] = new Smarty_Internal_Resource_Stream(); } return $smarty->_resource_handlers[$type] = self::$resources['stream']; } // TODO: try default_(template|config)_handler // give up throw new SmartyException("Unkown resource type '{$type}'"); }
/** * @inheritdoc */ public function register(\Smarty $smarty) { $smarty->registerResource($this->getExtensionName(), $this); }
public static function load(Smarty $smarty, $type) { if (isset($smarty->_resource_handlers[$type])) { return $smarty->_resource_handlers[$type]; } if (isset($smarty->registered_resources[$type])) { if ($smarty->registered_resources[$type] instanceof Smarty_Resource) { $smarty->_resource_handlers[$type] = $smarty->registered_resources[$type]; return $smarty->_resource_handlers[$type]; } if (!isset(self::$resources['registered'])) { self::$resources['registered'] = new Smarty_Internal_Resource_Registered(); } if (!isset($smarty->_resource_handlers[$type])) { $smarty->_resource_handlers[$type] = self::$resources['registered']; } return $smarty->_resource_handlers[$type]; } if (isset(self::$sysplugins[$type])) { if (!isset(self::$resources[$type])) { $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type); self::$resources[$type] = new $_resource_class(); } return $smarty->_resource_handlers[$type] = self::$resources[$type]; } $_resource_class = 'Smarty_Resource_' . ucfirst($type); if ($smarty->loadPlugin($_resource_class)) { if (isset(self::$resources[$type])) { return $smarty->_resource_handlers[$type] = self::$resources[$type]; } if (class_exists($_resource_class, false)) { self::$resources[$type] = new $_resource_class(); return $smarty->_resource_handlers[$type] = self::$resources[$type]; } else { $smarty->registerResource($type, array("smarty_resource_{$type}_source", "smarty_resource_{$type}_timestamp", "smarty_resource_{$type}_secure", "smarty_resource_{$type}_trusted")); return self::load($smarty, $type); } } $_known_stream = stream_get_wrappers(); if (in_array($type, $_known_stream)) { if (is_object($smarty->security_policy)) { $smarty->security_policy->isTrustedStream($type); } if (!isset(self::$resources['stream'])) { self::$resources['stream'] = new Smarty_Internal_Resource_Stream(); } return $smarty->_resource_handlers[$type] = self::$resources['stream']; } throw new SmartyException("Unknown resource type '{$type}'"); }
/** * Load Resource Handler * * @param Smarty $smarty smarty object * @param string $resource_type name of the resource * @return Smarty_Resource Resource Handler */ public static function load(Smarty $smarty, $resource_type) { // try the instance cache if (isset(self::$resources[$resource_type])) { return self::$resources[$resource_type]; } // try registered resource if (isset($smarty->registered_resources[$resource_type])) { if ($smarty->registered_resources[$resource_type] instanceof Smarty_Resource) { return self::$resources[$resource_type] = $smarty->registered_resources[$resource_type]; } if (!isset(self::$resources['registered'])) { self::$resources['registered'] = new Smarty_Internal_Resource_Registered(); } return self::$resources['registered']; } // try sysplugins dir if (isset(self::$sysplugins[$resource_type])) { $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($resource_type); return self::$resources[$resource_type] = new $_resource_class(); } // try plugins dir $_resource_class = 'Smarty_Resource_' . ucfirst($resource_type); if ($smarty->loadPlugin($_resource_class)) { if (class_exists($_resource_class, false)) { return self::$resources[$resource_type] = new $_resource_class(); } else { $smarty->registerResource($resource_type, array("smarty_resource_{$resource_type}_source", "smarty_resource_{$resource_type}_timestamp", "smarty_resource_{$resource_type}_secure", "smarty_resource_{$resource_type}_trusted")); // give it another try, now that the resource is registered properly return self::load($smarty, $resource_type); } } // try streams $_known_stream = stream_get_wrappers(); if (in_array($resource_type, $_known_stream)) { // is known stream if (is_object($smarty->security_policy)) { $smarty->security_policy->isTrustedStream($resource_type); } if (!isset(self::$resources['stream'])) { self::$resources['stream'] = new Smarty_Internal_Resource_Stream(); } return self::$resources['stream']; } // give up throw new SmartyException('Unkown resource type \'' . $resource_type . '\''); }
} /** * Determine basename for compiled filename * * @param Smarty_Template_Source $source source object * @return string resource's basename */ public function getBasename(Smarty_Template_Source $source) { return $this->fileResource->getBasename($source); } } // We initialize smarty here $debug->append('Instantiating Smarty Object', 3); $smarty = new Smarty(); // Assign our local paths $debug->append('Define Smarty Paths', 3); $smarty->template_dir = BASEPATH . 'templates/' . THEME . '/'; $smarty->compile_dir = BASEPATH . 'templates/compile/' . THEME . '/'; $smarty->registerResource('hybrid', new Smarty_Resource_Hybrid(new Smarty_Resource_Database($template), new Smarty_Internal_Resource_File())); $smarty->default_resource_type = "hybrid"; $smarty_cache_key = md5(serialize($_REQUEST) . serialize(@$_SESSION['USERDATA']['id'])); // Optional smarty caching, check Smarty documentation for details if ($config['smarty']['cache']) { $debug->append('Enable smarty cache'); $smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED); $smarty->cache_lifetime = $config['smarty']['cache_lifetime']; $smarty->cache_dir = BASEPATH . "templates/cache/" . THEME; $smarty->escape_html = true; $smarty->use_sub_dirs = true; }
/** * * @param Smarty $template_processor */ function registerResources($template_processor) { $default_resource_name = strtolower('template_' . self::$theme . '_' . SJB_System::getSystemSettings('SYSTEM_ACCESS_TYPE')); $template_processor->registerResource($default_resource_name, new SJB\Smarty\Resource($this)); $template_processor->default_resource_type = $default_resource_name; $template_processor->registerPlugin('function', "image", array($this, "getImageURL")); $template_processor->registerPlugin('function', "common_js", array($this, "getCommonJsURL")); $template_processor->registerPlugin('modifier', 'paymentTranslate', 'SJB\\Smarty\\Modifier\\PaymentTranslator::translate'); $template_processor->registerPlugin('function', "WYSIWYGEditor", array($this, "WYSIWYGEditor")); $template_processor->registerPlugin('block', "php", array($this, "php")); }
if (!mkdir(HEURIST_SMARTY_TEMPLATES_DIR . "templates_c/", 0777, true)) { die('Failed to create folder for smarty templates'); } } if (!file_exists(HEURIST_SMARTY_TEMPLATES_DIR . "cache/")) { if (!mkdir(HEURIST_SMARTY_TEMPLATES_DIR . "cache/", 0777, true)) { die('Failed to create folder for smarty templates'); } } $smarty->template_dir = HEURIST_SMARTY_TEMPLATES_DIR; $smarty->config_dir = HEURIST_SMARTY_TEMPLATES_DIR . "configs/"; $smarty->compile_dir = HEURIST_SMARTY_TEMPLATES_DIR . 'templates_c/'; $smarty->cache_dir = HEURIST_SMARTY_TEMPLATES_DIR . 'cache/'; /*****DEBUG****/ //error_log(">>>>>".HEURIST_SMARTY_TEMPLATES_DIR); $smarty->registerResource("string", array("str_get_template", "str_get_timestamp", "str_get_secure", "str_get_trusted")); function str_get_template($tpl_name, &$tpl_source, &$smarty_obj) { $tpl_source = $tpl_name; $tpl_name = "from_str_test"; // return true on success, false to generate failure notification return true; } function str_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) { // do database call here to populate $tpl_timestamp // with unix epoch time value of last template modification. // This is used to determine if recompile is necessary. $tpl_timestamp = time(); // this example will always recompile! // return true on success, false to generate failure notification
public static function renderWidget(&$parser, $widgetName) { global $IP; $smarty = new Smarty(); $smarty->left_delimiter = '<!--{'; $smarty->right_delimiter = '}-->'; $smarty->compile_dir = "{$IP}/extensions/Widgets/compiled_templates/"; // registering custom Smarty plugins $smarty->addPluginsDir("{$IP}/extensions/Widgets/smarty_plugins/"); $smarty->enableSecurity(); // These settings were for Smarty v2 - they don't seem to // have an equivalent in Smarty v3. /* $smarty->security_settings = array( 'IF_FUNCS' => array( 'is_array', 'isset', 'array', 'list', 'count', 'sizeof', 'in_array', 'true', 'false', 'null' ), 'MODIFIER_FUNCS' => array( 'validate' ) ); */ // Register the Widgets extension functions. $smarty->registerResource('wiki', array(array('WidgetRenderer', 'wiki_get_template'), array('WidgetRenderer', 'wiki_get_timestamp'), array('WidgetRenderer', 'wiki_get_secure'), array('WidgetRenderer', 'wiki_get_trusted'))); $params = func_get_args(); // The first and second params are the parser and the widget // name - we already have both. array_shift($params); array_shift($params); $params_tree = array(); foreach ($params as $param) { $pair = explode('=', $param, 2); if (count($pair) == 2) { $key = trim($pair[0]); $val = trim($pair[1]); } else { $key = $param; $val = true; } if ($val == 'false') { $val = false; } /* If the name of the parameter has object notation a.b.c.d then we assign stuff to hash of hashes, not scalar */ $keys = explode('.', $key); // $subtree will be moved from top to the bottom and // at the end will point to the last level. $subtree =& $params_tree; // Go through all the keys but the last one. $last_key = array_pop($keys); foreach ($keys as $subkey) { // If next level of subtree doesn't exist yet, // create an empty one. if (!array_key_exists($subkey, $subtree)) { $subtree[$subkey] = array(); } // move to the lower level $subtree =& $subtree[$subkey]; } // last portion of the key points to itself if (isset($subtree[$last_key])) { // If this is already an array, push into it; // otherwise, convert into an array first. if (!is_array($subtree[$last_key])) { $subtree[$last_key] = array($subtree[$last_key]); } $subtree[$last_key][] = $val; } else { // doesn't exist yet, just setting a value $subtree[$last_key] = $val; } } $smarty->assign($params_tree); try { $output = $smarty->fetch("wiki:{$widgetName}"); } catch (Exception $e) { return '<div class=\\"error\\">' . wfMsgExt('widgets-desc', array('parsemag'), htmlentities($widgetName)) . '</div>'; } // Hide the widget from the parser. $output = 'ENCODED_CONTENT ' . self::$mRandomString . base64_encode($output) . ' END_ENCODED_CONTENT'; return $output; }
require_once dirname(__FILE__) . '/src/libs/Smarty.class.php'; class EvaledFileResource extends Smarty_Internal_Resource_File { public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) { parent::populate($source, $_template); $source->recompiled = true; } } $smarty = new Smarty(); // Set up directories $smarty->setTemplateDir($pageinfo['templatedir'] . '/' . $pageinfo['pagetype']); //$smarty->setCacheDir(dirname(__FILE__)."/src/cache"); $smarty->addPluginsDir($pageinfo['templatedir'] . '/_plugins'); $smarty->setConfigDir($pageinfo['templatedir'] . '/configs'); $smarty->registerResource('file', new EvaledFileResource()); // Set up common settings $smarty->compile_check = true; $smarty->debugging = false; //print_r($pageinfo); //$smarty->testInstall(); return $smarty; } // hack because we don't have htaccess function do_me($pagename, $pagetype, $sitename, $themename) { $_REQUEST['siteName'] = $sitename; $_REQUEST['themeName'] = $themename; $_REQUEST['pageType'] = $pagetype; $_REQUEST['pageName'] = $pagename; $pageinfo = read_page_info();