function TemplateRecompileAll($directory = TEMPLATES_DIR) { $files = Dir::ReadFiles($directory, '~^(?!email).*?(\\.tpl$|\\.css$)~'); foreach ($files as $file) { $compiled = TEMPLATE_COMPILE_DIR . '/' . $file; if (($code = Template_Compiler::CompileFile($file, $directory)) === false) { return array('message' => 'Template ' . $file . ' contains errors', 'errors' => Template_Compiler::GetErrors()); } else { file_put_contents($compiled, $code); @chmod($compiled, 0666); } } return true; }
/** * Compiles are templates in the template directory. Used to avoid checking if compiled * files exist in a production environment. * * @return bool Always returns true. */ public static function compileAllTemplates() { $template = new Template(); $resource = new Template_ResourceManager($template); $files = $resource->getAllTemplateFiles(); foreach ($files as $file) { echo $file . "<bR>"; $compiler = new Template_Compiler($resource); $compiler->compile($file); } return true; }
private static function CompileTag($matches) { try { $original = preg_replace('~^\\(\\*\\d+\\*\\)~', '', $matches[0]); self::$current_line = $matches[1]; $tag = $matches[2]; // Remove template comments if ($tag[0] === self::COMMENT_TAG) { return String::BLANK; } $tag = self::ParseTag($tag); // Inside a literal section, don't parse code if (!in_array($tag['tag'], self::$literal_close_tags) && count(self::$literal_stack) > 0) { return $original; } // Tag name is a variable if ($tag['tag'][0] == self::VARIABLE_TAG) { return self::PHP_START_ECHO . self::ParseVars($tag['tag'], $tag['modifiers'], true) . ';' . self::PHP_END; } switch ($tag['tag']) { case 'string': return $tag['term']; case 'translate': if (empty($tag['args'])) { return _T($tag['term']); } else { return self::PHP_START_ECHO . "_T('" . $tag['term'] . "'" . $tag['args'] . ");" . self::PHP_END; } case 'define': self::CompileDefineTag($tag['attributes']); return; case 'if': self::PushTag(self::$tag_stack, 'if'); return self::PHP_START . 'if( ' . self::ParseVars($tag['attributes']) . ' ):' . self::PHP_END; case 'elsif': case 'elseif': self::CheckForUnexpected(self::$tag_stack, 'if', 'elseif'); return self::PHP_START . 'elseif( ' . self::ParseVars($tag['attributes']) . ' ):' . self::PHP_END; case 'else': self::CheckForUnexpected(self::$tag_stack, 'if', 'else'); return self::PHP_START . 'else:' . self::PHP_END; case '/if': self::PopTag(self::$tag_stack, 'if'); return self::PHP_START . 'endif;' . self::PHP_END; case 'capture': self::VerifyHasAttributes($tag['attributes'], $tag['tag']); self::VerifyRequiredAttributes(array('var'), $tag['attributes'], $tag['tag']); self::PushTag(self::$tag_stack, 'capture'); self::$capture_stack[] = $tag['attributes']['var']; return self::PHP_START . 'ob_start();' . self::PHP_END; case '/capture': self::PopTag(self::$tag_stack, 'capture'); return self::PHP_START . self::ParseVars(array_pop(self::$capture_stack)) . ' = ob_get_clean();' . self::PHP_END; case 'nocache': self::$has_nocache = true; return '{nocache}'; case '/nocache': return '{/nocache}'; case 'literal': self::PushTag(self::$literal_stack, 'literal'); return String::BLANK; case '/literal': self::PopTag(self::$literal_stack, 'literal'); return String::BLANK; case 'insert': self::PushTag(self::$tag_stack, 'insert'); return self::CompileInsertTag($tag['attributes']); case '/insert': self::PopTag(self::$tag_stack, 'insert'); return self::PHP_START . 'endif;' . self::PHP_END; case 'range': self::PushTag(self::$tag_stack, 'range'); return self::CompileRangeStart($tag['attributes']); case '/range': self::PopTag(self::$tag_stack, 'range'); return self::PHP_START . 'endforeach;' . self::PHP_END; case 'foreach': self::PushTag(self::$tag_stack, 'foreach'); return self::CompileForeachStart($tag['attributes']); case 'foreachdone': return self::PHP_START . 'break;' . self::PHP_END; case '/foreach': self::PopTag(self::$tag_stack, 'foreach'); return self::PHP_START . String::NEWLINE_UNIX . ' endforeach;' . String::NEWLINE_UNIX . 'endif;' . String::NEWLINE_UNIX . self::PHP_END; case 'php': self::PushTag(self::$literal_stack, 'php'); return self::PHP_START; case '/php': self::PopTag(self::$literal_stack, 'php'); return self::PHP_END; case 'phpcode': self::PushTag(self::$literal_stack, 'phpcode'); return self::PHP_START . "echo '" . self::PHP_START . " ';" . self::PHP_END; case '/phpcode': self::PopTag(self::$literal_stack, 'phpcode'); return self::PHP_START . "echo ' " . self::PHP_END . "';" . self::PHP_END; case 'setlocale': self::VerifyHasAttributes($tag['attributes'], $tag['tag']); self::VerifyRequiredAttributes(array('value'), $tag['attributes'], $tag['tag']); return self::PHP_START . 'setlocale(LC_TIME, \'' . $tag['attributes']['value'] . '\');' . self::PHP_END; case 'datelocale': self::VerifyRequiredAttributes(array('value', 'format'), $tag['attributes'], $tag['tag']); switch (strtolower($tag['attributes']['value'])) { case 0: case '0': case '-0': case '-0 day': case '-0 days': case '+0': case '+0 day': case '+0 days': case 'today': case 'now': $tag['attributes']['value'] = null; break; default: $tag['attributes']['value'] = ' ' . $tag['attributes']['value']; break; } return self::PHP_START_ECHO . "ucwords(strftime('" . $tag['attributes']['format'] . "', strtotime(date('Y-m-d H:i:s')" . (empty($tag['attributes']['value']) ? '' : " . '" . $tag['attributes']['value'] . "'") . ")));" . self::PHP_END; case 'date': self::VerifyRequiredAttributes(array('value', 'format'), $tag['attributes'], $tag['tag']); switch (strtolower($tag['attributes']['value'])) { case 0: case '0': case '-0': case '-0 day': case '-0 days': case '+0': case '+0 day': case '+0 days': case 'today': case 'now': $tag['attributes']['value'] = null; break; default: $tag['attributes']['value'] = ' ' . $tag['attributes']['value']; break; } return self::PHP_START_ECHO . "date('" . $tag['attributes']['format'] . "', strtotime(date('Y-m-d H:i:s')" . (empty($tag['attributes']['value']) ? '' : " . '" . $tag['attributes']['value'] . "'") . "));" . self::PHP_END; case 'assign': return self::CompileAssignTag($tag['attributes']); case 'include': self::VerifyRequiredAttributes(array('file'), $tag['attributes'], $tag['tag']); return self::PHP_START . "readfile('" . $tag['attributes']['file'] . "');" . self::PHP_END; case 'options': return self::CompileOptionsTag($tag['attributes']); case 'reasons': return self::CompileReasonsTag($tag['attributes']); case 'template': return self::CompileTemplateTag($tag['attributes']); case 'tags': return self::CompileTagsTag($tag['attributes']); case 'categories': return self::CompileCategoriesTag($tag['attributes']); case 'category': return self::CompileCategoryTag($tag['attributes']); case 'searchterms': return self::CompileSearchTermsTag($tag['attributes']); case 'videos': return self::CompileVideosTag($tag['attributes']); case 'video': return self::CompileVideoTag($tag['attributes']); case 'clips': return self::CompileClipsTag($tag['attributes']); case 'player': return self::CompilePlayerTag($tag['attributes']); case 'sponsors': return self::CompileSponsorsTag($tag['attributes']); case 'sponsor': return self::CompileSponsorTag($tag['attributes']); case 'banner': return self::CompileBannerTag($tag['attributes']); case 'stats': return self::CompileStatsTag($tag['attributes']); case 'ratings': return self::CompileRatingsTag($tag['attributes']); case 'comments': return self::CompileCommentsTag($tag['attributes']); case 'user': return self::CompileUserTag($tag['attributes']); } return $original; } catch (CompilerException $e) { self::$errors[] = $e->getMessage(); } }
function tbxSiteTemplateSearchReplace() { Privileges::Check(Privileges::TEMPLATES); $output = array(); $v = Validator::Create(); $v->Register(count(Request::Get('templates')), Validator_Type::GREATER, 'You must select at least one template for this action', 0); $v->Register(Request::Get('search'), Validator_Type::NOT_EMPTY, 'The Search For field is required'); foreach (Request::Get('templates') as $template) { $template = File::Sanitize($template); $filename = TEMPLATES_DIR . '/' . $template; $v->Register(is_writeable($filename), Validator_Type::NOT_FALSE, "The template file {$template} has incorrect permissions; change to 666 then try again"); } if (!$v->Validate()) { $output['message'] = 'Search and replace could not be executed; please fix the following items'; $output['errors'] = $v->GetErrors(); JSON::Failure($output); } else { $search = String::FormatNewlines(Request::Get('search'), String::NEWLINE_UNIX); $replace = String::FormatNewlines(Request::Get('replace'), String::NEWLINE_UNIX); $total_replacements = 0; foreach (Request::Get('templates') as $template) { $template = File::Sanitize($template); $filename = TEMPLATES_DIR . "/{$template}"; $template_code = file_get_contents($filename); $template_code = str_replace($search, $replace, $template_code, $replacements); // Changes have been made if ($replacements > 0 && ($code = Template_Compiler::Compile($template_code)) !== false) { file_put_contents($filename, $template_code); file_put_contents(TEMPLATE_COMPILE_DIR . "/{$template}", $code); @chmod($compiled, 0666); $total_replacements += $replacements; } } $output['message'] = 'Search and replace has been completed. Replacements made: ' . NumberFormatInteger($total_replacements); JSON::Success($output); } }
public function CompileTemplate($template) { $compiled_code = Template_Compiler::CompileFile($template); File::Overwrite($this->compile_dir . "/{$template}", $compiled_code); }