Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

(no description)

File Size: 829 lines (26 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

Mustache_Engine:: (37 methods):
  __construct()
  render()
  getEscape()
  getEntityFlags()
  getCharset()
  getPragmas()
  setLoader()
  getLoader()
  setPartialsLoader()
  getPartialsLoader()
  setPartials()
  setHelpers()
  getHelpers()
  addHelper()
  getHelper()
  hasHelper()
  removeHelper()
  setLogger()
  getLogger()
  setTokenizer()
  getTokenizer()
  setParser()
  getParser()
  setCompiler()
  getCompiler()
  setCache()
  getCache()
  getLambdaCache()
  getTemplateClassName()
  loadTemplate()
  loadPartial()
  loadLambda()
  loadSource()
  tokenize()
  parse()
  compile()
  log()


Class: Mustache_Engine  - X-Ref

A Mustache implementation in PHP.

{@link http://defunkt.github.com/mustache}

Mustache is a framework-agnostic logic-less templating language. It enforces separation of view
logic from template files. In fact, it is not even possible to embed logic in the template.

This is very, very rad.

__construct(array $options = array()   X-Ref
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,

// Enable pragmas across all templates, regardless of the presence of pragma tags in the individual
// templates.
'pragmas' => [Mustache_Engine::PRAGMA_FILTERS],
);

param: array $options (default: array())

render($template, $context = array()   X-Ref
Shortcut 'render' invocation.

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

param: string $template
param: mixed  $context  (default: array())
return: string Rendered template

getEscape()   X-Ref
Get the current Mustache escape callback.

return: callable|null

getEntityFlags()   X-Ref
Get the current Mustache entitity type to escape.

return: int

getCharset()   X-Ref
Get the current Mustache character set.

return: string

getPragmas()   X-Ref
Get the current globally enabled pragmas.

return: array

setLoader(Mustache_Loader $loader)   X-Ref
Set the Mustache template Loader instance.

param: Mustache_Loader $loader

getLoader()   X-Ref
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: Mustache_Loader

setPartialsLoader(Mustache_Loader $partialsLoader)   X-Ref
Set the Mustache partials Loader instance.

param: Mustache_Loader $partialsLoader

getPartialsLoader()   X-Ref
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: Mustache_Loader

setPartials(array $partials = array()   X-Ref
Set partials for the current partials Loader instance.

param: array $partials (default: array())

setHelpers($helpers)   X-Ref
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.

param: array|Traversable $helpers

getHelpers()   X-Ref
Get the current set of Mustache helpers.

return: Mustache_HelperCollection

addHelper($name, $helper)   X-Ref
Add a new Mustache helper.

param: string $name
param: mixed  $helper

getHelper($name)   X-Ref
Get a Mustache helper by name.

param: string $name
return: mixed Helper

hasHelper($name)   X-Ref
Check whether this Mustache instance has a helper.

param: string $name
return: bool True if the helper is present

removeHelper($name)   X-Ref
Remove a helper by name.

param: string $name

setLogger($logger = null)   X-Ref
Set the Mustache Logger instance.

param: Mustache_Logger|Psr\Log\LoggerInterface $logger

getLogger()   X-Ref
Get the current Mustache Logger instance.

return: Mustache_Logger|Psr\Log\LoggerInterface

setTokenizer(Mustache_Tokenizer $tokenizer)   X-Ref
Set the Mustache Tokenizer instance.

param: Mustache_Tokenizer $tokenizer

getTokenizer()   X-Ref
Get the current Mustache Tokenizer instance.

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

return: Mustache_Tokenizer

setParser(Mustache_Parser $parser)   X-Ref
Set the Mustache Parser instance.

param: Mustache_Parser $parser

getParser()   X-Ref
Get the current Mustache Parser instance.

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

return: Mustache_Parser

setCompiler(Mustache_Compiler $compiler)   X-Ref
Set the Mustache Compiler instance.

param: Mustache_Compiler $compiler

getCompiler()   X-Ref
Get the current Mustache Compiler instance.

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

return: Mustache_Compiler

setCache(Mustache_Cache $cache)   X-Ref
Set the Mustache Cache instance.

param: Mustache_Cache $cache

getCache()   X-Ref
Get the current Mustache Cache instance.

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

return: Mustache_Cache

getLambdaCache()   X-Ref
Get the current Lambda Cache instance.

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

return: Mustache_Cache

getTemplateClassName($source)   X-Ref
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.

param: string|Mustache_Source $source
return: string Mustache Template class name

loadTemplate($name)   X-Ref
Load a Mustache Template by name.

param: string $name
return: Mustache_Template

loadPartial($name)   X-Ref
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.

param: string $name
return: Mustache_Template

loadLambda($source, $delims = null)   X-Ref
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.

param: string $source
param: string $delims (default: null)
return: Mustache_Template

loadSource($source, Mustache_Cache $cache = null)   X-Ref
Instantiate and return a Mustache Template instance by source.

Optionally provide a Mustache_Cache instance. This is used internally by Mustache_Engine::loadLambda to respect
the 'cache_lambda_templates' configuration option.

param: string|Mustache_Source $source
param: Mustache_Cache         $cache  (default: null)
return: Mustache_Template

tokenize($source)   X-Ref
Helper method to tokenize a Mustache template.

param: string $source
return: array Tokens

parse($source)   X-Ref
Helper method to parse a Mustache template.

param: string $source
return: array Token tree

compile($source)   X-Ref
Helper method to compile a Mustache template.

param: string|Mustache_Source $source
return: string generated Mustache template class code

log($level, $message, array $context = array()   X-Ref
Add a log record if logging is enabled.

param: int    $level   The logging level
param: string $message The log message
param: array  $context The log context