mustache_templates-8.x-1.0-beta4/mustache.module
mustache.module
<?php
/**
* @file
* Mustache Templates module file.
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_theme().
*/
function mustache_theme($existing, $type, $theme, $path) {
// See the README for render array examples,
// and have a look at the helper class MustacheRenderTemplate.
return [
'mustache' => ['render element' => 'element'],
'mustache_template_js_inline' => ['render element' => 'element'],
];
}
/**
* Implements hook_library_info_build().
*/
function mustache_library_info_build() {
// Rebuild libraries for summable scripts, if enabled.
/** @var \Drupal\mustache\Summable\SummableScriptsInterface $summables */
$summables = \Drupal::service('mustache.summables');
if ($summables->isEnabled()) {
return $summables->getAllLibraries();
}
return [];
}
/**
* Implements hook_cache_flush().
*/
function mustache_cache_flush() {
\Drupal::service('mustache.templates')->clearCaches();
}
/**
* Implements hook_mustache_templates().
*/
function mustache_mustache_templates() {
$path = \Drupal::service('extension.path.resolver')->getPath('module', 'mustache') . '/templates/';
return ['summable' => ['file' => $path . 'summable.mustache.tpl']];
}
/**
* Preprocess content markup provided by Mustache templates.
*
* @param array &$variables
* The variables array holding the render element.
*/
function template_preprocess_mustache(array &$variables) {
$element = &$variables['element'];
$variables['content'] = &$element['#content'];
$variables['template'] = &$element['#template'];
$variables['use_sync'] = &$element['#use_sync'];
$variables['sync'] = &$element['#sync'];
$variables['inline_partials'] = &$element['#inline_partials'];
$variables['form'] = &$element['#form'];
}
/**
* Preprocess a Mustache template as inline Javascript.
*
* @param array &$variables
* The variables array holding the render element.
*/
function template_preprocess_mustache_template_js_inline(array &$variables) {
$element = &$variables['element'];
$variables['template'] = &$element['#template'];
}
/**
* Implements hook_preprocess_HOOK() for node.
*/
function mustache_preprocess_node(&$variables) {
$tokens = &drupal_static('mustache_tokens', []);
$tokens['node'] = $variables['node'];
}
/**
* Implements hook_preprocess_HOOK() for field.
*/
function mustache_preprocess_field(&$variables) {
if (isset($variables['element']['#object'])) {
$entity = $variables['element']['#object'];
if ($entity instanceof EntityInterface) {
$tokens = &drupal_static('mustache_tokens', []);
$tokens[$entity->getEntityTypeId()] = $entity;
}
}
}
