90 $options = array_merge(array(
93 'base_template_class' =>
'Twig_Template',
94 'strict_variables' =>
false,
95 'autoescape' =>
'html',
97 'auto_reload' => null,
98 'optimizations' => -1,
101 $this->debug = (bool) $options[
'debug'];
102 $this->charset = strtoupper($options[
'charset']);
103 $this->baseTemplateClass = $options[
'base_template_class'];
104 $this->autoReload = null === $options[
'auto_reload'] ? $this->debug : (bool) $options[
'auto_reload'];
105 $this->strictVariables = (bool) $options[
'strict_variables'];
106 $this->runtimeInitialized =
false;
108 $this->functionCallbacks = array();
109 $this->filterCallbacks = array();
114 $this->extensionInitialized =
false;
135 $this->baseTemplateClass =
$class;
151 $this->debug =
false;
169 $this->autoReload =
true;
177 $this->autoReload =
false;
195 $this->strictVariables =
true;
203 $this->strictVariables =
false;
246 if (
false === $this->cache) {
265 return $this->templateClassPrefix.md5($this->
getLoader()->getCacheKey($name)).(null === $index ?
'' :
'_'.$index);
286 public function render($name, array $context = array())
297 public function display($name, array $context = array())
314 if (isset($this->loadedTemplates[$cls])) {
315 return $this->loadedTemplates[$cls];
318 if (!class_exists($cls,
false)) {
330 if (!$this->runtimeInitialized) {
334 return $this->loadedTemplates[$cls] =
new $cls($this);
352 $r =
new ReflectionObject($extension);
353 if (filemtime($r->getFileName()) > $time) {
358 return $this->
getLoader()->isFresh($name, $time);
363 if (!is_array($names)) {
364 $names = array($names);
367 foreach ($names as $name) {
378 if (1 === count($names)) {
382 throw new Twig_Error_Loader(sprintf(
'Unable to find one of the following templates: "%s".', implode(
'", "', $names)));
390 $this->loadedTemplates = array();
398 if (
false === $this->cache) {
402 foreach (
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->cache), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
403 if ($file->isFile()) {
404 @unlink($file->getPathname());
416 if (null === $this->lexer) {
443 return $this->
getLexer()->tokenize($source, $name);
453 if (null === $this->parser) {
479 return $this->
getParser()->parse($tokens);
489 if (null === $this->compiler) {
515 return $this->
getCompiler()->compile($node)->getSource();
533 }
catch (Exception $e) {
534 throw new Twig_Error_Runtime(sprintf(
'An exception has been thrown during the compilation of a template ("%s").', $e->getMessage()), -1, $name, $e);
555 if (null === $this->loader) {
556 throw new LogicException(
'You must set a loader first.');
569 $this->charset = strtoupper(
$charset);
587 $this->runtimeInitialized =
true;
590 $extension->initRuntime($this);
603 return isset($this->extensions[$name]);
615 if (!isset($this->extensions[$name])) {
619 return $this->extensions[$name];
629 if ($this->extensionInitialized) {
630 throw new LogicException(sprintf(
'Unable to register extension "%s" as extensions have already been initialized.', $extension->
getName()));
647 if ($this->extensionInitialized) {
648 throw new LogicException(sprintf(
'Unable to remove extension "%s" as extensions have already been initialized.', $name));
651 unset($this->extensions[$name]);
683 if ($this->extensionInitialized) {
684 throw new LogicException(
'Unable to add a token parser as extensions have already been initialized.');
687 $this->staging->addTokenParser($parser);
697 if (!$this->extensionInitialized) {
716 $tags[$parser->getTag()] =
$parser;
730 if ($this->extensionInitialized) {
731 throw new LogicException(
'Unable to add a node visitor as extensions have already been initialized.');
734 $this->staging->addNodeVisitor($visitor);
744 if (!$this->extensionInitialized) {
760 throw new LogicException(
'A filter must be an instance of Twig_FilterInterface or Twig_SimpleFilter');
765 $name = $filter->getName();
768 if ($this->extensionInitialized) {
769 throw new LogicException(sprintf(
'Unable to add filter "%s" as extensions have already been initialized.', $name));
772 $this->staging->addFilter($name, $filter);
787 if (!$this->extensionInitialized) {
791 if (isset($this->filters[$name])) {
792 return $this->filters[$name];
795 foreach ($this->filters as $pattern => $filter) {
796 $pattern = str_replace(
'\\*',
'(.*?)', preg_quote($pattern,
'#'), $count);
799 if (preg_match(
'#^'.$pattern.
'$#', $name, $matches)) {
800 array_shift($matches);
801 $filter->setArguments($matches);
808 foreach ($this->filterCallbacks as $callback) {
809 if (
false !== $filter = call_user_func($callback, $name)) {
819 $this->filterCallbacks[] = $callable;
833 if (!$this->extensionInitialized) {
849 throw new LogicException(
'A test must be an instance of Twig_TestInterface or Twig_SimpleTest');
854 $name = $test->getName();
857 if ($this->extensionInitialized) {
858 throw new LogicException(sprintf(
'Unable to add test "%s" as extensions have already been initialized.', $name));
861 $this->staging->addTest($name, $test);
871 if (!$this->extensionInitialized) {
887 if (!$this->extensionInitialized) {
891 if (isset($this->tests[$name])) {
892 return $this->tests[$name];
907 throw new LogicException(
'A function must be an instance of Twig_FunctionInterface or Twig_SimpleFunction');
912 $name = $function->getName();
915 if ($this->extensionInitialized) {
916 throw new LogicException(sprintf(
'Unable to add function "%s" as extensions have already been initialized.', $name));
919 $this->staging->addFunction($name, $function);
934 if (!$this->extensionInitialized) {
938 if (isset($this->functions[$name])) {
939 return $this->functions[$name];
942 foreach ($this->functions as $pattern => $function) {
943 $pattern = str_replace(
'\\*',
'(.*?)', preg_quote($pattern,
'#'), $count);
946 if (preg_match(
'#^'.$pattern.
'$#', $name, $matches)) {
947 array_shift($matches);
948 $function->setArguments($matches);
955 foreach ($this->functionCallbacks as $callback) {
956 if (
false !== $function = call_user_func($callback, $name)) {
966 $this->functionCallbacks[] = $callable;
980 if (!$this->extensionInitialized) {
998 if ($this->extensionInitialized || $this->runtimeInitialized) {
999 if (null === $this->globals) {
1010 if ($this->extensionInitialized || $this->runtimeInitialized) {
1012 $this->globals[$name] = $value;
1014 $this->staging->addGlobal($name, $value);
1025 if (!$this->runtimeInitialized && !$this->extensionInitialized) {
1029 if (null === $this->globals) {
1047 foreach ($this->
getGlobals() as $key => $value) {
1048 if (!array_key_exists($key, $context)) {
1049 $context[$key] = $value;
1063 if (!$this->extensionInitialized) {
1077 if (!$this->extensionInitialized) {
1086 $alternatives = array();
1087 foreach ($items as $item) {
1088 $lev = levenshtein($name, $item);
1089 if ($lev <= strlen($name) / 3 ||
false !== strpos($item, $name)) {
1090 $alternatives[$item] = $lev;
1093 asort($alternatives);
1095 return array_keys($alternatives);
1102 $extGlob = $extension->getGlobals();
1103 if (!is_array($extGlob)) {
1104 throw new UnexpectedValueException(sprintf(
'"%s::getGlobals()" must return an array of globals.', get_class($extension)));
1110 $globals[] = $this->staging->getGlobals();
1112 return call_user_func_array(
'array_merge',
$globals);
1117 if ($this->extensionInitialized) {
1121 $this->extensionInitialized =
true;
1123 $this->filters = array();
1124 $this->functions = array();
1125 $this->tests = array();
1126 $this->visitors = array();
1127 $this->unaryOperators = array();
1128 $this->binaryOperators = array();
1139 foreach ($extension->
getFilters() as $name => $filter) {
1142 $name = $filter->getName();
1143 }
elseif ($filter instanceof Twig_SimpleFilter) {
1144 $name = $filter->getName();
1147 $this->filters[$name] = $filter;
1151 foreach ($extension->
getFunctions() as $name => $function) {
1154 $name = $function->getName();
1155 }
elseif ($function instanceof Twig_SimpleFunction) {
1156 $name = $function->getName();
1159 $this->functions[$name] = $function;
1163 foreach ($extension->
getTests() as $name => $test) {
1166 $name = $test->getName();
1167 }
elseif ($test instanceof Twig_SimpleTest) {
1168 $name = $test->getName();
1171 $this->tests[$name] = $test;
1177 $this->parsers->addTokenParser(
$parser);
1179 $this->parsers->addTokenParserBroker(
$parser);
1181 throw new LogicException(
'getTokenParsers() must return an array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances');
1187 $this->visitors[] = $visitor;
1192 if (2 !== count($operators)) {
1193 throw new InvalidArgumentException(sprintf(
'"%s::getOperators()" does not return a valid operators array.', get_class($extension)));
1196 $this->unaryOperators = array_merge($this->unaryOperators, $operators[0]);
1197 $this->binaryOperators = array_merge($this->binaryOperators, $operators[1]);
1203 $dir = dirname($file);
1204 if (!is_dir($dir)) {
1205 if (
false === @mkdir($dir, 0777,
true) && !is_dir($dir)) {
1206 throw new RuntimeException(sprintf(
"Unable to create the cache directory (%s).", $dir));
1208 }
elseif (!is_writable($dir)) {
1209 throw new RuntimeException(sprintf(
"Unable to write in the cache directory (%s).", $dir));
1212 $tmpFile = tempnam(dirname($file), basename($file));
1213 if (
false !== @file_put_contents($tmpFile, $content)) {
1215 if (@rename($tmpFile, $file) || (@copy($tmpFile, $file) && unlink($tmpFile))) {
1216 @chmod($file, 0666 & ~umask());
1222 throw new RuntimeException(sprintf(
'Failed to write cache file "%s".', $file));
compile(Twig_NodeInterface $node)
Compiles a Node.
clearTemplateCache()
Clears the internal template cache.
getOperators()
Returns a list of operators to add to the existing list.
getBinaryOperators()
Gets the registered binary Operators.
render($name, array $context=array())
Renders a template.
getLoader()
Gets the Loader instance.
initRuntime()
Initializes the runtime environment.
isAutoReload()
Checks if the auto_reload option is enabled.
enableAutoReload()
Enables the auto_reload option.
getCompiler()
Gets the Compiler instance.
addExtension(Twig_ExtensionInterface $extension)
Registers an extension.
Interface implemented by compiler classes.
getGlobals()
Gets the registered Globals.
getNodeVisitors()
Gets the registered Node Visitors.
addFilter($name, $filter=null)
Registers a Filter.
getNodeVisitors()
Returns the node visitor instances to add to the existing list.
setParser(Twig_ParserInterface $parser)
Sets the Parser instance.
Default implementation of a token parser broker.
if($rub=fetch_assoc($rub_query)) if(check_if_module_active('url_rewriting')) $class
Compiles a node to PHP code.
Represents a template function.
getLexer()
Gets the Lexer instance.
getTokenParsers()
Returns the token parser instances to add to the existing list.
getFilters()
Gets the registered Filters.
getExtensions()
Returns all registered extensions.
parse(Twig_TokenStream $tokens)
Parses a token stream.
__construct(Twig_LoaderInterface $loader=null, $options=array())
Constructor.
getFunction($name)
Get a function by name.
getParser()
Gets the Parser instance.
getFilter($name)
Get a filter by name.
initExtension(Twig_ExtensionInterface $extension)
isTemplateFresh($name, $time)
Returns true if the template is still fresh.
removeExtension($name)
Removes an extension by name.
computeAlternatives($name, $items)
getTests()
Gets the registered Tests.
getTemplateClass($name, $index=null)
Gets the template class associated with the given string.
Represents a template function.
clearCacheFiles()
Clears the template cache files on the filesystem.
addFunction($name, $function=null)
Registers a Function.
setLoader(Twig_LoaderInterface $loader)
Sets the Loader instance.
isStrictVariables()
Checks if the strict_variables option is enabled.
disableDebug()
Disables debugging mode.
getCacheFilename($name)
Gets the cache filename for a given template.
setBaseTemplateClass($class)
Sets the base template class for compiled templates.
Default parser implementation.
Interface implemented by lexer classes.
disableAutoReload()
Disables the auto_reload option.
Interface implemented by token parsers.
getTests()
Returns a list of tests to add to the existing list.
getTest($name)
Gets a test by name.
getBaseTemplateClass()
Gets the base template class for compiled templates.
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
setCache($cache)
Sets the cache directory or false if cache is disabled.
getCache()
Gets the cache directory or false if cache is disabled.
compileSource($source, $name=null)
Compiles a template source code.
addGlobal($name, $value)
Registers a Global.
getName()
Returns the name of the extension.
loadTemplate($name, $index=null)
Loads a template by name.
addTokenParser(Twig_TokenParserInterface $parser)
Registers a Token Parser.
getTokenParsers()
Gets the registered Token Parsers.
isDebug()
Checks if debug mode is enabled.
Default base class for compiled templates.
addTest($name, $test=null)
Registers a Test.
mergeGlobals(array $context)
Merges a context with the defined globals.
registerUndefinedFunctionCallback($callable)
Represents a token stream.
writeCacheFile($file, $content)
Interface implemented by token parser brokers.
Represents a template filter.
addNodeVisitor(Twig_NodeVisitorInterface $visitor)
Registers a Node Visitor.
Interface implemented by extension classes.
getFilters()
Returns a list of filters to add to the existing list.
getExtension($name)
Gets an extension by name.
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
Represents a node in the AST.
setLexer(Twig_LexerInterface $lexer)
Sets the Lexer instance.
hasExtension($name)
Returns true if the given extension is registered.
disableStrictVariables()
Disables the strict_variables option.
getTemplateClassPrefix()
Gets the template class prefix.
registerUndefinedFilterCallback($callable)
Stores the Twig configuration.
getCharset()
Gets the default template charset.
Interface all loaders must implement.
setCompiler(Twig_CompilerInterface $compiler)
Sets the Compiler instance.
enableDebug()
Enables debugging mode.
setExtensions(array $extensions)
Registers an array of extensions.
getUnaryOperators()
Gets the registered unary Operators.
display($name, array $context=array())
Displays a template.
Represents a template test.
Represents a template filter.
Represents a template test.
getFunctions()
Returns a list of functions to add to the existing list.
Exception thrown when an error occurs during template loading.
Interface implemented by parser classes.
getTags()
Gets registered tags.
Exception thrown when an error occurs at runtime.
tokenize($source, $name=null)
Tokenizes a source code.
getFunctions()
Gets registered functions.
setTemplateFile($filename)
Sets the filename where the error occurred.
enableStrictVariables()
Enables the strict_variables option.
setCharset($charset)
Sets the default template charset.