Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
Mustache_Engine Class Reference
Inheritance diagram for Mustache_Engine:
core\output\mustache_engine

Public Member Functions

 __construct (array $options=array())
 Mustache class constructor.
 
 addHelper ($name, $helper)
 Add a new Mustache helper.
 
 getCache ()
 Get the current Mustache Cache instance.
 
 getCharset ()
 Get the current Mustache character set.
 
 getCompiler ()
 Get the current Mustache Compiler instance.
 
 getEntityFlags ()
 Get the current Mustache entitity type to escape.
 
 getEscape ()
 Get the current Mustache escape callback.
 
 getHelper ($name)
 Get a Mustache helper by name.
 
 getHelpers ()
 Get the current set of Mustache helpers.
 
 getLoader ()
 Get the current Mustache template Loader instance.
 
 getLogger ()
 Get the current Mustache Logger instance.
 
 getParser ()
 Get the current Mustache Parser instance.
 
 getPartialsLoader ()
 Get the current Mustache partials Loader instance.
 
 getPragmas ()
 Get the current globally enabled pragmas.
 
 getTemplateClassName ($source)
 Helper method to generate a Mustache template class.
 
 getTokenizer ()
 Get the current Mustache Tokenizer instance.
 
 hasHelper ($name)
 Check whether this Mustache instance has a helper.
 
 loadLambda ($source, $delims=null)
 Load a Mustache lambda Template by source.
 
 loadPartial ($name)
 Load a Mustache partial Template by name.
 
 loadTemplate ($name)
 Load a Mustache Template by name.
 
 removeHelper ($name)
 Remove a helper by name.
 
 render ($template, $context=array())
 Shortcut 'render' invocation.
 
 setCache (Mustache_Cache $cache)
 Set the Mustache Cache instance.
 
 setCompiler (Mustache_Compiler $compiler)
 Set the Mustache Compiler instance.
 
 setHelpers ($helpers)
 Set an array of Mustache helpers.
 
 setLoader (Mustache_Loader $loader)
 Set the Mustache template Loader instance.
 
 setLogger ($logger=null)
 Set the Mustache Logger instance.
 
 setParser (Mustache_Parser $parser)
 Set the Mustache Parser instance.
 
 setPartials (array $partials=array())
 Set partials for the current partials Loader instance.
 
 setPartialsLoader (Mustache_Loader $partialsLoader)
 Set the Mustache partials Loader instance.
 
 setTokenizer (Mustache_Tokenizer $tokenizer)
 Set the Mustache Tokenizer instance.
 

Public Attributes

const PRAGMA_ANCHORED_DOT = 'ANCHORED-DOT'
 
const PRAGMA_BLOCKS = 'BLOCKS'
 
const PRAGMA_FILTERS = 'FILTERS'
 
const SPEC_VERSION = '1.2.2'
 
const VERSION = '2.14.2'
 

Protected Member Functions

 getLambdaCache ()
 Get the current Lambda Cache instance.
 

Constructor & Destructor Documentation

◆ __construct()

Mustache_Engine::__construct ( array $options = array())

Mustache class constructor.

Passing an $options array allows overriding certain Mustache options during instantiation:

$options = array(
    // The class prefix for compiled templates. Defaults to '__Mustache_'.
    'template_class_prefix' => '__MyTemplates_',

    // A Mustache cache instance or a cache directory string for compiled templates.
    // Mustache will not cache templates unless this is set.
    'cache' => dirname(__FILE__).'/tmp/cache/mustache',

    // Override default permissions for cache files. Defaults to using the system-defined umask. It is
    // *strongly* recommended that you configure your umask properly rather than overriding permissions here.
    'cache_file_mode' => 0666,

    // Optionally, enable caching for lambda section templates. This is generally not recommended, as lambda
    // sections are often too dynamic to benefit from caching.
    'cache_lambda_templates' => true,

    // Customize the tag delimiters used by this engine instance. Note that overriding here changes the
    // delimiters used to parse all templates and partials loaded by this instance. To override just for a
    // single template, use an inline "change delimiters" tag at the start of the template file:
    //
    //     {{=<% %>=}}
    //
    'delimiters' => '<% %>',

    // A Mustache template loader instance. Uses a StringLoader if not specified.
    'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),

    // A Mustache loader instance for partials.
    'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials'),

    // An array of Mustache partials. Useful for quick-and-dirty string template loading, but not as
    // efficient or lazy as a Filesystem (or database) loader.
    'partials' => array('foo' => file_get_contents(dirname(__FILE__).'/views/partials/foo.mustache')),

    // An array of 'helpers'. Helpers can be global variables or objects, closures (e.g. for higher order
    // sections), or any other valid Mustache context value. They will be prepended to the context stack,
    // so they will be available in any template loaded by this Mustache instance.
    'helpers' => array('i18n' => function ($text) {
        // do something translatey here...
    }),

    // An 'escape' callback, responsible for escaping double-mustache variables.
    'escape' => function ($value) {
        return htmlspecialchars($buffer, ENT_COMPAT, 'UTF-8');
    },

    // Type argument for `htmlspecialchars`.  Defaults to ENT_COMPAT.  You may prefer ENT_QUOTES.
    'entity_flags' => ENT_QUOTES,

    // Character set for `htmlspecialchars`. Defaults to 'UTF-8'. Use 'UTF-8'.
    'charset' => 'ISO-8859-1',

    // A Mustache Logger instance. No logging will occur unless this is set. Using a PSR-3 compatible
    // logging library -- such as Monolog -- is highly recommended. A simple stream logger implementation is
    // available as well:
    'logger' => new Mustache_Logger_StreamLogger('php://stderr'),

    // Only treat Closure instances and invokable classes as callable. If true, values like
    // `array('ClassName', 'methodName')` and `array($classInstance, 'methodName')`, which are traditionally
    // "callable" in PHP, are not called to resolve variables for interpolation or section contexts. This
    // helps protect against arbitrary code execution when user input is passed directly into the template.
    // This currently defaults to false, but will default to true in v3.0.
    'strict_callables' => true,

    // Do not render the output of lambdas. Use this to prevent repeated rendering if the lambda already
    // takes care of rendering its content. This helps protect against mustache code injection when user
    // input is passed directly into the template. Defaults to false.
    'disable_lambda_rendering' => true,

    // Enable pragmas across all templates, regardless of the presence of pragma tags in the individual
    // templates.
    'pragmas' => [Mustache_Engine::PRAGMA_FILTERS],
);
Exceptions
Mustache_Exception_InvalidArgumentExceptionIf escape option is not callable
Parameters
array$options(default: array())

Reimplemented in core\output\mustache_engine.

Member Function Documentation

◆ addHelper()

Mustache_Engine::addHelper ( $name,
$helper )

Add a new Mustache helper.

See also
Mustache_Engine\setHelpers
Parameters
string$name
mixed$helper

◆ getCache()

Mustache_Engine::getCache ( )

Get the current Mustache Cache instance.

If no Cache instance has been explicitly specified, this method will instantiate and return a new one.

Return values
Mustache_Cache

◆ getCharset()

Mustache_Engine::getCharset ( )

Get the current Mustache character set.

Return values
string

◆ getCompiler()

Mustache_Engine::getCompiler ( )

Get the current Mustache Compiler instance.

If no Compiler instance has been explicitly specified, this method will instantiate and return a new one.

Return values
Mustache_Compiler

◆ getEntityFlags()

Mustache_Engine::getEntityFlags ( )

Get the current Mustache entitity type to escape.

Return values
int

◆ getEscape()

Mustache_Engine::getEscape ( )

Get the current Mustache escape callback.

Return values
callable|null

◆ getHelper()

Mustache_Engine::getHelper ( $name)

Get a Mustache helper by name.

See also
Mustache_Engine\setHelpers
Parameters
string$name
Return values
mixedHelper

◆ getHelpers()

Mustache_Engine::getHelpers ( )

Get the current set of Mustache helpers.

See also
Mustache_Engine\setHelpers
Return values
Mustache_HelperCollection

Reimplemented in core\output\mustache_engine.

◆ getLambdaCache()

Mustache_Engine::getLambdaCache ( )
protected

Get the current Lambda Cache instance.

If 'cache_lambda_templates' is enabled, this is the default cache instance. Otherwise, it is a NoopCache.

See also
Mustache_Engine\getCache
Return values
Mustache_Cache

◆ getLoader()

Mustache_Engine::getLoader ( )

Get the current Mustache template Loader instance.

If no Loader instance has been explicitly specified, this method will instantiate and return a StringLoader instance.

Return values
Mustache_Loader

◆ getLogger()

Mustache_Engine::getLogger ( )

Get the current Mustache Logger instance.

Return values
Mustache_Logger|Psr\Log\LoggerInterface

◆ getParser()

Mustache_Engine::getParser ( )

Get the current Mustache Parser instance.

If no Parser instance has been explicitly specified, this method will instantiate and return a new one.

Return values
Mustache_Parser

◆ getPartialsLoader()

Mustache_Engine::getPartialsLoader ( )

Get the current Mustache partials Loader instance.

If no Loader instance has been explicitly specified, this method will instantiate and return an ArrayLoader instance.

Return values
Mustache_Loader

◆ getPragmas()

Mustache_Engine::getPragmas ( )

Get the current globally enabled pragmas.

Return values
array

◆ getTemplateClassName()

Mustache_Engine::getTemplateClassName ( $source)

Helper method to generate a Mustache template class.

This method must be updated any time options are added which make it so the same template could be parsed and compiled multiple different ways.

Parameters
string | Mustache_Source$source
Return values
stringMustache Template class name

◆ getTokenizer()

Mustache_Engine::getTokenizer ( )

Get the current Mustache Tokenizer instance.

If no Tokenizer instance has been explicitly specified, this method will instantiate and return a new one.

Return values
Mustache_Tokenizer

◆ hasHelper()

Mustache_Engine::hasHelper ( $name)

Check whether this Mustache instance has a helper.

See also
Mustache_Engine\setHelpers
Parameters
string$name
Return values
boolTrue if the helper is present

◆ loadLambda()

Mustache_Engine::loadLambda ( $source,
$delims = null )

Load a Mustache lambda Template by source.

This is a helper method used by Template instances to generate subtemplates for Lambda sections. You can most likely ignore it completely.

Parameters
string$source
string$delims(default: null)
Return values
Mustache_Template

◆ loadPartial()

Mustache_Engine::loadPartial ( $name)

Load a Mustache partial Template by name.

This is a helper method used internally by Template instances for loading partial templates. You can most likely ignore it completely.

Parameters
string$name
Return values
Mustache_Template

◆ loadTemplate()

Mustache_Engine::loadTemplate ( $name)

Load a Mustache Template by name.

Parameters
string$name
Return values
Mustache_Template

◆ removeHelper()

Mustache_Engine::removeHelper ( $name)

Remove a helper by name.

See also
Mustache_Engine\setHelpers
Parameters
string$name

◆ render()

Mustache_Engine::render ( $template,
$context = array() )

Shortcut 'render' invocation.

Equivalent to calling $mustache->loadTemplate($template)->render($context);

See also
Mustache_Engine\loadTemplate
Mustache_Template\render
Parameters
string$template
mixed$context(default: array())
Return values
stringRendered template

◆ setCache()

Mustache_Engine::setCache ( Mustache_Cache $cache)

Set the Mustache Cache instance.

Parameters
Mustache_Cache$cache

◆ setCompiler()

Mustache_Engine::setCompiler ( Mustache_Compiler $compiler)

Set the Mustache Compiler instance.

Parameters
Mustache_Compiler$compiler

◆ setHelpers()

Mustache_Engine::setHelpers ( $helpers)

Set an array of Mustache helpers.

An array of 'helpers'. Helpers can be global variables or objects, closures (e.g. for higher order sections), or any other valid Mustache context value. They will be prepended to the context stack, so they will be available in any template loaded by this Mustache instance.

Exceptions
Mustache_Exception_InvalidArgumentExceptionif $helpers is not an array or Traversable
Parameters
array | Traversable$helpers

◆ setLoader()

Mustache_Engine::setLoader ( Mustache_Loader $loader)

Set the Mustache template Loader instance.

Parameters
Mustache_Loader$loader

◆ setLogger()

Mustache_Engine::setLogger ( $logger = null)

Set the Mustache Logger instance.

Exceptions
Mustache_Exception_InvalidArgumentExceptionIf logger is not an instance of Mustache_Logger or Psr\Log\LoggerInterface
Parameters
Mustache_Logger | Psr\Log\LoggerInterface$logger

◆ setParser()

Mustache_Engine::setParser ( Mustache_Parser $parser)

Set the Mustache Parser instance.

Parameters
Mustache_Parser$parser

◆ setPartials()

Mustache_Engine::setPartials ( array $partials = array())

Set partials for the current partials Loader instance.

Exceptions
Mustache_Exception_RuntimeExceptionIf the current Loader instance is immutable
Parameters
array$partials(default: array())

◆ setPartialsLoader()

Mustache_Engine::setPartialsLoader ( Mustache_Loader $partialsLoader)

Set the Mustache partials Loader instance.

Parameters
Mustache_Loader$partialsLoader

◆ setTokenizer()

Mustache_Engine::setTokenizer ( Mustache_Tokenizer $tokenizer)

Set the Mustache Tokenizer instance.

Parameters
Mustache_Tokenizer$tokenizer

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