config = $config; } public function getEngine(GlobalConfig $config) { if ($this->engine) { return $this->engine; } $base = $config->getTemplates(); $theme = $config->getTheme()->getTemplates(); // Use internal templates if no templates // dir exists in the working directory if (!is_dir($base)) { $base = __DIR__ . '/../../../templates'; } // Create new Plates instance $this->engine = new Engine($base); if (!is_dir($theme)) { $theme = $base; } $this->engine->addFolder('theme', $theme, true); Daux::writeln("Starting Template engine with basedir '$base' and theme folder '$theme'.", OutputInterface::VERBOSITY_VERBOSE); $this->registerFunctions($this->engine); return $this->engine; } /** * @param string $name * @param array $data * @return string */ public function render($name, array $data = []) { $engine = $this->getEngine($data['config']); $engine->addData([ 'base_url' => $data['config']->getBaseUrl(), 'base_page' => $data['config']->getBasePage(), 'page' => $data['page'], 'params' => $data['config'], // legacy name for config 'config' => $data['config'], 'tree' => $data['config']['tree'], ]); Daux::writeln("Rendering template '$name'", OutputInterface::VERBOSITY_VERBOSE); return $engine->render($name, $data); } protected function registerFunctions($engine) { $engine->registerFunction('get_navigation', function ($tree, $path, $current_url, $base_page, $mode) { $nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode); return $this->renderNavigation($nav); }); $engine->registerFunction('translate', function ($key) { $language = $this->config->getLanguage(); if (isset($this->engine->getData('page')['page'])) { $page = $this->engine->getData('page'); if (!empty($page['page']['language'])) { $language = $page['page']['language']; } } if ($this->config->hasTranslationKey($language, $key)) { return $this->config->getTranslationKey($language, $key); } if ($this->config->hasTranslationKey('en', $key)) { return $this->config->getTranslationKey('en', $key); } return "Unknown key $key"; }); $engine->registerFunction('get_breadcrumb_title', function ($page, $base_page) { $title = ''; $breadcrumb_trail = $page['breadcrumb_trail']; $separator = $this->getSeparator($page['breadcrumb_separator']); foreach ($breadcrumb_trail as $value) { $title .= '' . $value['title'] . '' . $separator; } if ($page['filename'] === 'index' || $page['filename'] === '_index') { if ($page['title'] != '') { $title = substr($title, 0, -1 * strlen($separator)); } } else { $title .= '' . $page['title'] . ''; } return $title; }); } private function renderNavigation($entries) { $nav = ''; foreach ($entries as $entry) { if (array_key_exists('children', $entry)) { $icon = ' '; if (array_key_exists('href', $entry)) { $link = '' . $icon . $entry['title'] . ''; } else { $link = '' . $icon . $entry['title'] . ''; } $link .= $this->renderNavigation($entry['children']); } else { $link = '' . $entry['title'] . ''; } $nav .= "