function tpl_display($view = '', $vars = array()) { $dir_sep = DIRECTORY_SEPARATOR; $dir_base = __DIR__ . $dir_sep; $dir_smarty = $dir_base . implode($dir_sep, array('..', 'smarty-3.1.16', 'libs', '')); $dir_tpls = $dir_base . 'tpls' . $dir_sep; $dir_tplc = $dir_base . 'tplc' . $dir_sep; $dir_plgs = $dir_base . 'plugins' . $dir_sep; require_once $dir_smarty . 'Smarty.class.php'; $smarty = new Smarty(); $smarty->left_delimiter = '<%'; $smarty->right_delimiter = '%>'; $smarty->setTemplateDir($dir_tpls); $smarty->setCompileDir($dir_tplc); $smarty->addPluginsDir($dir_plgs); $smarty->registerFilter('pre', 'tpl_pre_filter'); if (isset($vars['debug'])) { $smarty->force_compile = true; } $smarty->assign($vars); $smarty->display($view . '.tpl'); if (isset($vars['debug'])) { $smarty->clearCompiledTemplate(); } }
public function __construct(Smarty $smarty) { foreach ($this->methodsMap as $filterType => $method) { check_condition(method_exists($this, $method), "Method [{$method}] not exists in class " . __CLASS__); $this->CALLS[$method] = 0; $smarty->registerFilter($filterType, array($this, "_{$method}")); } }
/** * @inheritdoc */ public function register(\Smarty $smarty) { if ($this->isPlugin()) { $smarty->registerPlugin($this->getExtensionType(), $this->getExtensionName(), [$this, 'process']); } elseif ($this->getExtensionType() === static::EXTENSION_TYPE_FILTER) { $smarty->registerFilter($this->getExtensionName(), [$this, 'process']); } }
/** * Register the filters for the tpl file. * * @param string $handle */ function load_external_filters($handle) { if (isset($this->external_filters[$handle])) { $compile_id = ''; foreach ($this->external_filters[$handle] as $filters) { foreach ($filters as $filter) { list($type, $callback) = $filter; $compile_id .= $type . (is_array($callback) ? implode('', $callback) : $callback); $this->smarty->registerFilter($type, $callback); } } $this->smarty->compile_id .= '.' . base_convert(crc32($compile_id), 10, 36); } }
// 注意:我们使用 smarty 模板引擎,所以这里 UI 对我们没什么用,这里唯一的用途是用于验证码(captcha)生成时候指定字体 $f3->set('UI', PROTECTED_PATH . '/Framework/F3/UI/'); // 初始化 smarty 模板引擎 require_once PROTECTED_PATH . '/Framework/Smarty/Smarty.class.php'; $smarty = new Smarty(); //修改 smarty 缺省的 {},太容易导致错误了 $smarty->left_delimiter = '{{'; $smarty->right_delimiter = '}}'; // smarty 缺省对所有的输出都要做 html_specialchars 防止出错 $smarty->escape_html = true; // Smarty 严重安全漏洞,由于 smarty 不会过滤 <script language="php">phpinfo();</php> 这样的代码,我们只能自己过滤 function smarty_helper_security_output_filter($source, Smarty_Internal_Template $smartyTemplate) { return preg_replace('/<script[^>]*language[^>]*>(.*?)<\\/script>/is', "", $source); } $smarty->registerFilter('output', 'smarty_helper_security_output_filter'); //缺省不缓存,防止出错,后面不同页面根据实际需要再自己定义缓存 $smarty->setCaching(Smarty::CACHING_OFF); $smarty->setCacheLifetime(0); /** * 定义全局的 smarty 缓存开关 * * @param $enable boolean 开/关 * @param $ttl integer 缓存时间 * @param $policy smarty 缓存时间的策略,比如 \Smarty::CACHING_LIFETIME_SAVED * */ function enableSmartyCache($enable, $ttl = 0, $policy = \Smarty::CACHING_LIFETIME_SAVED) { global $f3; global $smarty; if ($enable && $f3->get('sysConfig[smarty_caching]')) {
$smarty->debugging = false; $smarty->debugging_ctrl = 'URL'; // 'NONE' on production $smarty->deprecation_notices = false; // so many depreciated yet not migrated smarty calls if (Configuration::get('PS_FORCE_SMARTY_2')) { $smarty->debug_tpl = _PS_ALL_THEMES_DIR_ . 'debug.tpl'; if (Configuration::get('PS_HTML_THEME_COMPRESSION')) { $smarty->register_outputfilter('smartyMinifyHTML'); } if (Configuration::get('PS_JS_HTML_THEME_COMPRESSION')) { $smarty->register_outputfilter('smartyPackJSinHTML'); } } else { if (Configuration::get('PS_HTML_THEME_COMPRESSION')) { $smarty->registerFilter('output', 'smartyMinifyHTML'); } if (Configuration::get('PS_JS_HTML_THEME_COMPRESSION')) { $smarty->registerFilter('output', 'smartyPackJSinHTML'); } } smartyRegisterFunction($smarty, 'modifier', 'truncate', 'smarty_modifier_truncate'); smartyRegisterFunction($smarty, 'modifier', 'secureReferrer', array('Tools', 'secureReferrer')); smartyRegisterFunction($smarty, 'function', 't', 'smartyTruncate'); // unused smartyRegisterFunction($smarty, 'function', 'm', 'smartyMaxWords'); // unused smartyRegisterFunction($smarty, 'function', 'p', 'smartyShowObject'); // unused smartyRegisterFunction($smarty, 'function', 'd', 'smartyDieObject'); // unused
/** * Return smarty instance * * @return Smarty */ public function getSmarty() { static $oSmarty; if (!$oSmarty) { //create smarty object require_once 'Smarty.class.php'; $oSmarty = new Smarty(); foreach ($this->_aSmartyProperties as $sName => $mValue) { $oSmarty->{$sName} = substr($sName, -4) == '_dir' ? Volcano_Tools::fixPath($mValue) : $mValue; } foreach ($this->_aCustomFunctions as $sType => $aItems) { foreach ($aItems as $aItem) { if (substr($oSmarty->_version, 0, 1) != '2') { //bad code, but smarty3 has version like 'Smarty3-SVN$Rev: 3286 $' if ($sType == 'modifier' || $sType == 'function') { $oSmarty->registerPlugin($sType, $aItem[1], $aItem[0]); } elseif ($sType == 'outputfilter') { $oSmarty->registerFilter('output', $aItem[0]); } elseif ($sType == 'postfilter') { $oSmarty->registerFilter('post', $aItem[0]); } elseif ($sType == 'prefilter') { $oSmarty->registerFilter('pre', $aItem[0]); } } else { if ($sType == 'modifier') { $oSmarty->register_modifier($aItem[1], $aItem[0]); } elseif ($sType == 'function') { $oSmarty->register_function($aItem[1], $aItem[0]); } elseif ($sType == 'outputfilter') { $oSmarty->register_outputfilter($aItem[0]); } elseif ($sType == 'postfilter') { $oSmarty->register_postfilter($aItem[0]); } elseif ($sType == 'prefilter') { $oSmarty->register_prefilter($aItem[0]); } } } } } $oSmarty->error_reporting = error_reporting() & ~E_NOTICE; return $oSmarty; }
<?php require_once "config_root.php"; require_once SMARTY_DIR . '/Smarty.class.php'; function minify_html($tpl_output, Smarty_Internal_Template $template) { $tpl_output = preg_replace('![\\t ]*[\\r\\n]+[\\t ]*!', '', $tpl_output); return $tpl_output; } $smarty = new Smarty(); $smarty->setPluginsDir(SMARTY_PLUGIN); $smarty->cache_dir = CACHE_SMARTY_DIR; $smarty->template_dir = TEMPLATE_DIR; $smarty->compile_dir = CACHE_SMARTY_COMPILE; $smarty->config_dir = CONFIG_DIR; if (COMPRESSED_FILES_MOD) { $smarty->registerFilter("output", "minify_html"); } $configRoot = array("root_url" => ROOT_URL, "root" => ROOT_DIR, "config" => CONFIG_DIR, "class" => CLASS_DIR, "smarty" => SMARTY_DIR, "smarty_sysplugins" => SMARTY_SYSPLUGINS_DIR, "smary_plugin" => SMARTY_PLUGIN, "cache" => CACHE_DIR, "cache_smarty" => CACHE_SMARTY_DIR, "cache_smarty_c" => CACHE_SMARTY_COMPILE, "template" => TEMPLATE_DIR, "img" => IMG_DIR, "js" => JS_DIR, "css" => CSS_DIR, "adm" => ADM_DIR); $configUrl = array("root_url" => ROOT_URL, "root" => ROOT_URL, "config" => ROOT_URL . CONFIG_DIR, "class" => ROOT_URL . CLASS_DIR, "smarty" => ROOT_URL . SMARTY_DIR, "smarty_sysplugins" => ROOT_URL . SMARTY_SYSPLUGINS_DIR, "smary_plugin" => ROOT_URL . SMARTY_PLUGIN, "cache" => ROOT_URL . CACHE_DIR, "cache_smarty" => ROOT_URL . CACHE_SMARTY_DIR, "cache_smarty_c" => ROOT_URL . CACHE_SMARTY_COMPILE, "template" => ROOT_URL . TEMPLATE_DIR, "img" => ROOT_URL . IMG_DIR, "js" => ROOT_URL . JS_DIR, "css" => ROOT_URL . CSS_DIR, "adm" => ROOT_URL . ADM_DIR); $configMod = array("dev" => DEV_MOD, "compressed_files" => COMPRESSED_FILES_MOD); $smarty->assign("url", $configUrl); $smarty->assign("dir", $configRoot); $smarty->assign("mod", $configMod);
/** * Метод вызывается для привязки фильтра к Smarty * * @param Smarty $smarty */ public final function bind(Smarty $smarty) { $smarty->registerFilter(Smarty::FILTER_PRE, array($this, 'preCompile')); $smarty->registerFilter(Smarty::FILTER_POST, array($this, 'postCompile')); $smarty->registerFilter(Smarty::FILTER_OUTPUT, array($this, 'output')); }
return preg_replace("/changed/", '----------', $tpl_source); } function varFilterTest($v) { return preg_replace("/FILTER_TEST/", 'changed in VAR filter', $v); } function outputFilterTest($tpl_source, Smarty_Internal_Template $template) { return preg_replace("/FILTER_TEST/", 'changed in POST filter', $tpl_source); } function filterTest() { return "FILTER_TEST"; } $smarty4 = new Smarty(); $smarty4->registerFilter('pre', 'preFilterTest'); $smarty4->registerFilter('pre', 'preFilterTest2'); $smarty4->registerFilter('variable', 'varFilterTest'); $smarty4->registerFilter('output', 'outputFilterTest'); $smarty4->assign('foo', 'FILTER_TEST'); $smarty4->assign('t', 'test'); $smarty->assign('filtered', $smarty4->fetch('filtered.tpl')); $smarty5 = new Smarty(); $smarty5->escape_html = true; $smarty5->assign('textWithHTML', '<span style="color:red;"><i><b>foo</b></i></span>'); $smarty->assign('escapeHtml', $smarty5->fetch('escape_html.tpl')); $smarty6 = new Smarty(); $smarty6->default_modifiers = array("replace:'text_to_replace':'replaced'", 'escape:"htmlall"'); //<-no template variables allowed here (e.g. 'replace:'a':$b' - error) $smarty6->assign('replace_me', '<b>text_to_replace</b>'); $smarty->assign('defaultModifier', $smarty6->fetch('default_modifiers.tpl'));
} else { $token = $_SESSION['token']; } // initialize smarty define('SMARTY_DIR', str_replace("\\", "/", getcwd()) . '/includes/Smarty-3.1.15/libs/'); require_once SMARTY_DIR . 'Smarty.class.php'; require_once SMARTY_DIR . 'SmartyPaginate.class.php'; function escFilter($content) { return htmlspecialchars($content, ENT_QUOTES, 'cp1251'); } $smarty = new Smarty(); $smarty->setCacheDir($global_temporary_directory . '/'); $smarty->setCompileDir($global_temporary_directory . '/'); $smarty->setTemplateDir('includes/templates/'); $smarty->registerFilter('variable', 'escFilter'); $smarty->loadFilter('output', 'trimwhitespace'); $smarty->caching = false; $smarty->assign('show_help_to_users', $show_help_to_users); $smarty->assign('show_http_to_users', $show_http_to_users); $smarty->assign('show_logons_to_users', $show_logons_to_users); $smarty->assign('show_other_to_users', $show_other_to_users); $smarty->assign('enable_http_mode', $enable_http_mode); $smarty->assign('disable_ip_logger', $disable_ip_logger); $smarty->assign('enable_email_mode', $enable_email_mode); $smarty->assign('show_email_to_users', $show_email_to_users); $smarty->assign('show_domains', $show_domains); $smarty->assign('show_domains_to_users', $show_domains_to_users); $smarty->assign('token', $token); // initialize common used variables $self_file = $_SERVER['SCRIPT_NAME'];
public function register(\Smarty $smarty) { $smarty->registerFilter('pre', array($this, 'preFilter')); $smarty->registerPlugin('compiler', 'do', array($this, 'compile_do')); }