ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig_Loader_Array Class Reference

Loads a template from an array. More...

+ Inheritance diagram for Twig_Loader_Array:
+ Collaboration diagram for Twig_Loader_Array:

Public Member Functions

 __construct (array $templates=array())
 
 setTemplate ($name, $template)
 Adds or overrides a template. More...
 
 getSource ($name)
 Gets the source code of a template, given its name. More...
 
 getSourceContext ($name)
 Returns the source context for a given template logical name. More...
 
 exists ($name)
 Check if we have the source code of a template, given its name. More...
 
 getCacheKey ($name)
 Gets the cache key to use for the cache for a given template name. More...
 
 isFresh ($name, $time)
 Returns true if the template is still fresh. More...
 

Protected Attributes

 $templates = array()
 

Detailed Description

Loads a template from an array.

When using this loader with a cache mechanism, you should know that a new cache key is generated each time a template content "changes" (the cache key being the source code of the template). If you don't want to see your cache grows out of control, you need to take care of clearing the old cache file by yourself.

This loader should only be used for unit testing.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 26 of file Array.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Loader_Array::__construct ( array  $templates = array())
Parameters
array$templatesAn array of templates (keys are the names, and values are the source code)

Definition at line 33 of file Array.php.

References $templates.

34  {
35  $this->templates = $templates;
36  }

Member Function Documentation

◆ exists()

Twig_Loader_Array::exists (   $name)

Check if we have the source code of a template, given its name.

Parameters
string$nameThe name of the template to check if we can load
Returns
bool If the template source code is handled by this loader or not

Implements Twig_ExistsLoaderInterface.

Definition at line 71 of file Array.php.

References $name.

72  {
73  return isset($this->templates[(string) $name]);
74  }

◆ getCacheKey()

Twig_Loader_Array::getCacheKey (   $name)

Gets the cache key to use for the cache for a given template name.

Parameters
string$nameThe name of the template to load
Returns
string The cache key
Exceptions
Twig_Error_LoaderWhen $name is not found

Implements Twig_LoaderInterface.

Definition at line 76 of file Array.php.

References $name.

77  {
78  $name = (string) $name;
79  if (!isset($this->templates[$name])) {
80  throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
81  }
82 
83  return $name.':'.$this->templates[$name];
84  }
Exception thrown when an error occurs during template loading.
Definition: Loader.php:25

◆ getSource()

Twig_Loader_Array::getSource (   $name)

Gets the source code of a template, given its name.

Parameters
string$nameThe name of the template to load
Returns
string The template source code
Exceptions
Twig_Error_LoaderWhen $name is not found
Deprecated:
since 1.27 (to be removed in 2.0), implement Twig_SourceContextLoaderInterface

Implements Twig_LoaderInterface.

Definition at line 49 of file Array.php.

References $name.

50  {
51  @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
52 
53  $name = (string) $name;
54  if (!isset($this->templates[$name])) {
55  throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
56  }
57 
58  return $this->templates[$name];
59  }
Exception thrown when an error occurs during template loading.
Definition: Loader.php:25

◆ getSourceContext()

Twig_Loader_Array::getSourceContext (   $name)

Returns the source context for a given template logical name.

Parameters
string$nameThe template logical name
Returns
Twig_Source
Exceptions
Twig_Error_LoaderWhen $name is not found

Implements Twig_SourceContextLoaderInterface.

Definition at line 61 of file Array.php.

References $name.

62  {
63  $name = (string) $name;
64  if (!isset($this->templates[$name])) {
65  throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
66  }
67 
68  return new Twig_Source($this->templates[$name], $name);
69  }
Exception thrown when an error occurs during template loading.
Definition: Loader.php:25
Holds information about a non-compiled Twig template.
Definition: Source.php:19

◆ isFresh()

Twig_Loader_Array::isFresh (   $name,
  $time 
)

Returns true if the template is still fresh.

Parameters
string$nameThe template name
int$timeTimestamp of the last modification time of the cached template
Returns
bool true if the template is fresh, false otherwise
Exceptions
Twig_Error_LoaderWhen $name is not found

Implements Twig_LoaderInterface.

Definition at line 86 of file Array.php.

References $name.

87  {
88  $name = (string) $name;
89  if (!isset($this->templates[$name])) {
90  throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
91  }
92 
93  return true;
94  }
Exception thrown when an error occurs during template loading.
Definition: Loader.php:25

◆ setTemplate()

Twig_Loader_Array::setTemplate (   $name,
  $template 
)

Adds or overrides a template.

Parameters
string$nameThe template name
string$templateThe template source

Definition at line 44 of file Array.php.

References $name, and $template.

45  {
46  $this->templates[(string) $name] = $template;
47  }
$template

Field Documentation

◆ $templates

Twig_Loader_Array::$templates = array()
protected

Definition at line 28 of file Array.php.

Referenced by __construct().


The documentation for this class was generated from the following file: