MediaWiki REL1_37
TemplateParser Class Reference
Collaboration diagram for TemplateParser:

Public Member Functions

 __construct ( $templateDir=null, $cache=null)
 
 enableRecursivePartials ( $enable)
 Enable/disable the use of recursive partials.
 
 processTemplate ( $templateName, $args, array $scopes=[])
 Returns HTML for a given template by calling the template function with the given args.
 

Protected Member Functions

 compile ( $templateName)
 Compile the Mustache template into PHP code using LightnCandy.
 
 getTemplate ( $templateName)
 Returns a given template function if found, otherwise throws an exception.
 
 getTemplateFilename ( $templateName)
 Constructs the location of the source Mustache template.
 

Protected Attributes

int $compileFlags
 Compilation flags passed to LightnCandy.
 
callable[] $renderers
 Array of cached rendering functions.
 
string $templateDir
 The path to the Mustache templates.
 

Private Attributes

BagOStuff $cache
 
const CACHE_TTL = BagOStuff::TTL_WEEK
 
const CACHE_VERSION = '2.2.0'
 

Detailed Description

Definition at line 27 of file TemplateParser.php.

Constructor & Destructor Documentation

◆ __construct()

TemplateParser::__construct (   $templateDir = null,
  $cache = null 
)
Parameters
string | null$templateDir
BagOStuff | null | true$cacheRead-write cache If set to true, caching is disabled (deprecated since 1.35).

Definition at line 57 of file TemplateParser.php.

References $cache, $templateDir, CACHE_ANYTHING, and wfDeprecated().

Member Function Documentation

◆ compile()

TemplateParser::compile (   $templateName)
protected

Compile the Mustache template into PHP code using LightnCandy.

The compilation step generates both PHP code and metadata, which is also returned in the result. An example result looks as follows:

[
'phpCode' => '...',
'files' => [
'/path/to/template.mustache',
'/path/to/partial1.mustache',
'/path/to/partial2.mustache',
'filesHash' => '...'
]

The files entry is a list of the files read during the compilation of the template. Each entry is the fully-qualified filename, i.e. it includes path information.

The filesHash entry can be used to determine whether the template has changed since it was last compiled without compiling the template again. Currently, the filesHash entry is generated with FileContentsHasher::getFileContentsHash.

Parameters
string$templateNameThe name of the template
Returns
array An associative array containing the PHP code and metadata about its compilation
Exceptions
ExceptionThrown by LightnCandy if it could not compile the Mustache code
RuntimeExceptionIf LightnCandy could not compile the Mustache code but did not throw an exception. This exception is indicative of a bug in LightnCandy

Definition at line 214 of file TemplateParser.php.

References FileContentsHasher\getFileContentsHash(), and getTemplateFilename().

Referenced by getTemplate().

◆ enableRecursivePartials()

TemplateParser::enableRecursivePartials (   $enable)

Enable/disable the use of recursive partials.

Parameters
bool$enable

Definition at line 74 of file TemplateParser.php.

◆ getTemplate()

TemplateParser::getTemplate (   $templateName)
protected

Returns a given template function if found, otherwise throws an exception.

Parameters
string$templateNameThe name of the template (without file suffix)
Returns
callable
Exceptions
RuntimeExceptionWhen the template file cannot be found
RuntimeExceptionWhen the compiled template isn't callable. This is indicative of a bug in LightnCandy

Definition at line 109 of file TemplateParser.php.

References $compileFlags, compile(), and FileContentsHasher\getFileContentsHash().

Referenced by processTemplate().

◆ getTemplateFilename()

TemplateParser::getTemplateFilename (   $templateName)
protected

Constructs the location of the source Mustache template.

Parameters
string$templateNameThe name of the template
Returns
string
Exceptions
UnexpectedValueExceptionIf $templateName attempts upwards directory traversal

Definition at line 88 of file TemplateParser.php.

Referenced by compile().

◆ processTemplate()

TemplateParser::processTemplate (   $templateName,
  $args,
array  $scopes = [] 
)

Returns HTML for a given template by calling the template function with the given args.

echo $templateParser->processTemplate(
'ExampleTemplate',
[
'username' => $user->getName(),
'message' => 'Hello!'
]
);
$templateParser
Parameters
string$templateNameThe name of the template
-taint$templateNameexec_misc
mixed$args
-taint$argsnone
array$scopes
-taint$scopesnone
Returns
string

Definition at line 291 of file TemplateParser.php.

References $args, and getTemplate().

Member Data Documentation

◆ $cache

BagOStuff TemplateParser::$cache
private

Definition at line 35 of file TemplateParser.php.

Referenced by __construct().

◆ $compileFlags

int TemplateParser::$compileFlags
protected

Compilation flags passed to LightnCandy.

Definition at line 50 of file TemplateParser.php.

Referenced by getTemplate().

◆ $renderers

callable [] TemplateParser::$renderers
protected

Array of cached rendering functions.

Definition at line 45 of file TemplateParser.php.

◆ $templateDir

string TemplateParser::$templateDir
protected

The path to the Mustache templates.

Definition at line 40 of file TemplateParser.php.

Referenced by __construct().

◆ CACHE_TTL

const TemplateParser::CACHE_TTL = BagOStuff::TTL_WEEK
private

Definition at line 30 of file TemplateParser.php.

◆ CACHE_VERSION

const TemplateParser::CACHE_VERSION = '2.2.0'
private

Definition at line 29 of file TemplateParser.php.


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