MediaWiki  master
TemplateParser Class Reference

Public Member Functions

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

Protected Member Functions

 compile ( $code)
 Compile the Mustache code into PHP code using LightnCandy. More...
 
 compileForEval ( $fileContents, $filename)
 Wrapper for compile() function that verifies successful compilation and strips out the '<?php' part so that the code is ready for eval() More...
 
 getTemplate ( $templateName)
 Returns a given template function if found, otherwise throws an exception. More...
 
 getTemplateFilename ( $templateName)
 Constructs the location of the source Mustache template. More...
 

Protected Attributes

int $compileFlags
 Compilation flags passed to LightnCandy. More...
 
bool $forceRecompile = false
 Always compile template files. More...
 
callable [] $renderers
 Array of cached rendering functions. More...
 
string $templateDir
 The path to the Mustache templates. More...
 

Detailed Description

Definition at line 26 of file TemplateParser.php.

Constructor & Destructor Documentation

◆ __construct()

TemplateParser::__construct (   $templateDir = null,
  $forceRecompile = false 
)
Parameters
string | null$templateDir
bool$forceRecompile

Definition at line 51 of file TemplateParser.php.

References $forceRecompile.

Member Function Documentation

◆ compile()

TemplateParser::compile (   $code)
protected

Compile the Mustache code into PHP code using LightnCandy.

Parameters
string$codeMustache code
Returns
string PHP code (with '<?php')

Definition at line 187 of file TemplateParser.php.

Referenced by compileForEval().

◆ compileForEval()

TemplateParser::compileForEval (   $fileContents,
  $filename 
)
protected

Wrapper for compile() function that verifies successful compilation and strips out the '<?php' part so that the code is ready for eval()

Parameters
string$fileContentsMustache code
string$filenameName of the template
Returns
string PHP code (without '<?php')
Exceptions
RuntimeException

Definition at line 166 of file TemplateParser.php.

References compile().

Referenced by getTemplate().

◆ enableRecursivePartials()

TemplateParser::enableRecursivePartials (   $enable)

Enable/disable the use of recursive partials.

Parameters
bool$enable

Definition at line 64 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
RuntimeException

Definition at line 97 of file TemplateParser.php.

References $cache, $compileFlags, CACHE_ANYTHING, compileForEval(), ObjectCache\getLocalServerInstance(), and getTemplateFilename().

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 78 of file TemplateParser.php.

Referenced by getTemplate().

◆ 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!'
]
);
Parameters
string$templateNameThe name of the template

Definition at line 218 of file TemplateParser.php.

References $args, and getTemplate().

Member Data Documentation

◆ $compileFlags

int TemplateParser::$compileFlags
protected

Compilation flags passed to LightnCandy.

Definition at line 45 of file TemplateParser.php.

Referenced by getTemplate().

◆ $forceRecompile

bool TemplateParser::$forceRecompile = false
protected

Always compile template files.

Definition at line 40 of file TemplateParser.php.

Referenced by __construct().

◆ $renderers

callable [] TemplateParser::$renderers
protected

Array of cached rendering functions.

Definition at line 35 of file TemplateParser.php.

◆ $templateDir

string TemplateParser::$templateDir
protected

The path to the Mustache templates.

Definition at line 30 of file TemplateParser.php.


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