public function render($name, array $values = array()) { if (defined('ENV_DEV')) { //$this->engine->clearCacheFiles(); //$this->engine->clearTemplateCache(); $this->engine->clearAllCache(); $this->engine->clearCompiledTemplate(); } $template = $this->engine->createTemplate($name . $this->suffix); $template->assign($values); return $template->fetch(); }
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(); } }
/** * Deletes all compiled templates. */ function delete_compiled_templates() { $save_compile_id = $this->smarty->compile_id; $this->smarty->compile_id = null; $this->smarty->clearCompiledTemplate(); $this->smarty->compile_id = $save_compile_id; file_put_contents($this->smarty->getCompileDir() . '/index.htm', 'Not allowed!'); }
/** * Check the compile cache needs to be invalidated (multi front + local cache compatible) */ public function check_compile_cache_invalidation() { static $last_flush = null; if (!file_exists($this->getCompileDir() . 'last_flush')) { @touch($this->getCompileDir() . 'last_flush'); } elseif (defined('_DB_PREFIX_')) { if ($last_flush === null) { $sql = 'SELECT UNIX_TIMESTAMP(last_flush) as last_flush FROM `' . _DB_PREFIX_ . 'smarty_last_flush` WHERE type=\'compile\''; $last_flush = Db::getInstance()->getValue($sql, false); } if ((int) $last_flush && @filemtime($this->getCompileDir() . 'last_flush') < $last_flush) { @touch($this->getCompileDir() . 'last_flush'); parent::clearCompiledTemplate(); } } }
* not, see: * * @link <http://www.gnu.org/licenses/>. * @author niel * @copyright 2014 nZEDb */ require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'www' . DIRECTORY_SEPARATOR . 'config.php'; use nzedb\db\DbUpdate; use nzedb\utility\Utility; if (!Utility::isCLI()) { exit; } if (isset($argc) && $argc > 1 && isset($argv[1]) && $argv[1] == true) { $backup = isset($argv[2]) && $argv[2] == 'safe' ? true : false; $updater = new DbUpdate(['backup' => $backup]); echo $updater->log->header("Db updater starting ..."); $patched = $updater->processPatches(['safe' => $backup]); if ($patched > 0) { echo $updater->log->info("{$patched} patch(es) applied."); $smarty = new Smarty(); $cleared = $smarty->clearCompiledTemplate(); if ($cleared) { $msg = "The smarty template cache has been cleaned for you\n"; } else { $msg = "You should clear your smarty template cache at: " . SMARTY_DIR . "templates_c\n"; } $updater->log->info($msg); } } else { echo "Usage: php update_db.php true"; }
/** * Очищает кеш компиленных шаблонов */ public function ClearCompiledTemplates() { $this->oSmarty->clearCompiledTemplate(); }
/** * Execute compile command */ protected function execute(InputInterface $input, OutputInterface $output) { //get project name & set source/output dirs $project = $input->getArgument('project'); $this->config = new \Implico\Email\Config($project, $input->getOption('dir')); if ($error = $this->config->getErrors()) { switch ($error) { case 'projectNotFound': $output->writeln('<fg=red>ERROR: project directory not found</fg=red>'); exit(1); break; } } SmartyUtils::init($this->config); //get script name(s) $scripts = $input->getOption('script'); //if script name(s) not passed, set all scripts (exclude dirs) if (!$scripts) { $scripts = array_filter(array_diff(scandir($this->config['scriptsDir']), array('.', '..')), function ($script) { return !is_dir($this->config['scriptsDir'] . $script); }); } //add ".tpl" extension when applicable foreach ($scripts as $i => $script) { if (strpos($script, '.') === false) { $scripts[$i] = $script . '.tpl'; } } //get watch option $watch = $input->getOption('watch'); //get output option $outputMode = $input->getOption('output'); //create & configure Smarty object $smarty = new \Smarty(); $smarty->setCompileDir(IE_SMARTY_COMPILE_DIR); $smarty->addPluginsDir(IE_SMARTY_PLUGINS_DIR); $smarty->addPluginsDir(IE_SMARTY_CUSTOM_PLUGINS_DIR); $smarty->compile_check = false; $smarty->force_compile = true; $smarty->error_reporting = E_ALL; $smarty->registerClass('SmartyUtils', 'Implico\\Email\\Utils\\Smarty'); //set directories $smarty->setTemplateDir(array(0 => $this->config['dir'], 'core' => IE_CORE_DIR, 'layouts' => $this->config['layoutsDir'], 'scripts' => $this->config['scriptsDir'], 'styles' => $this->config['stylesDir'])); //master config file $smarty->configLoad(IE_CORE_DIR . 'config.conf'); //optional master custom config file $customConf = IE_CUSTOM_DIR . 'config.conf'; if (file_exists($customConf)) { $smarty->configLoad($customConf); } //console message for watching if ($watch) { $output->writeln('Watching for changes...'); } //main loop - watch for changes (or execute once if not watching) $compileNo = 1; $compileDirStamp = ''; //dirs to inspect file change $checkDirs = array($this->config['configsDir'], $this->config['configsScriptsDir'], $this->config['layoutsDir'], $this->config['scriptsDir'], $this->config['stylesDir']); //set output mode variables $outputMinified = in_array('m', $outputMode); $outputFormatted = in_array('f', $outputMode); //formatter object $formatter = null; //css inliner object $cssToInlineStyles = new CssToInlineStyles(); while (true) { //compile only if not watching or the dirs filestamp changes if (!$watch || $compileDirStamp != $this->getDirStamp($checkDirs)) { //clear compiled templates $smarty->clearCompiledTemplate(); //Smarty assign project-specific config file path $configFile = $this->config['configsDir'] . 'config.conf'; $loadConfigFile = file_exists($configFile); //set random complile_id (forces Smarty to compile) $smarty->compile_id = uniqid(); //list of compiled scripts $compiledScripts = $scripts; //fetch & save templates foreach ($scripts as $i => $script) { //script name without extension $scriptName = substr($script, 0, strrpos($script, '.')); $smarty->clearConfig(); if ($loadConfigFile) { $smarty->configLoad($configFile); } //set script-specific config file path if exists $configFileScript = $this->config['configsScriptsDir'] . $scriptName . '.conf'; if (file_exists($configFileScript)) { $smarty->configLoad($configFileScript); } //lazy create indenter if ($outputFormatted && !$formatter) { $formatter = new \Gajus\Dindent\Indenter(array('indentation_character' => $smarty->getConfigVars('indentChar'))); } //set encoding $outputEncoding = $smarty->getConfigVars('encoding'); if (!$outputEncoding) { $outputEncoding = 'utf-8'; } $outputEncodingUtf8 = strtoupper($outputEncoding) == 'UTF-8'; try { //get the html $html = $smarty->fetch($this->config['scriptsDir'] . $script); //get inline styles $inlineCss = $smarty->fetch($this->config['stylesDir'] . 'inline.tpl'); if (trim($inlineCss)) { $cssToInlineStyles->setHTML($html); $cssToInlineStyles->setCSS($inlineCss); $html = $cssToInlineStyles->convert(); } //save minified if ($outputMinified) { $htmlSave = $html; if (!$outputEncodingUtf8) { $htmlSave = mb_convert_encoding($htmlSave, $outputEncoding, 'utf-8'); } //max line width = 900 chars $maxPerLine = 750; $endLine = false; $newHtml = ''; for ($i = 0; $i < mb_strlen($htmlSave, $outputEncoding); $i++) { if ($i % $maxPerLine == 0 && $i > 0) { $endLine = true; } $curChar = mb_substr($htmlSave, $i, 1, $outputEncoding); $newHtml .= $curChar; if ($endLine) { if ($curChar == '>') { $newHtml .= PHP_EOL; $endLine = false; } } } $htmlSave = $newHtml; $this->saveOutput($this->config['outputsDir'] . $scriptName . '.min.html', $htmlSave); } //save formatted if ($outputFormatted) { $htmlSave = $formatter->indent($html); if (!$outputEncodingUtf8) { $htmlSave = mb_convert_encoding($htmlSave, $outputEncoding, 'utf-8'); } $this->saveOutput($this->config['outputsDir'] . $scriptName . '.html', $htmlSave, true); } } catch (\Exception $e) { $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>'); $compiledScripts[$i] .= ' <fg=red>(ERROR)</fg=red>'; } } //console info message $output->writeln(($watch ? '#' . $compileNo++ . ' ' : '') . 'Compiled ' . date('d-m-Y H:i:s') . ' ' . implode(', ', $compiledScripts)); } //break if not watching if (!$watch) { break; } //calculate dirs filestamp to compare $compileDirStamp = $this->getDirStamp($checkDirs); //pause usleep(500000); } }