params = $params; } public function getEngine(Config $params) { if ($this->engine) { return $this->engine; } $base = $params['templates']; $theme = $params['theme']['templates']; // 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['params']); $engine->addData([ 'base_url' => $data['params']['base_url'], 'base_page' => $data['params']['base_page'], 'page' => $data['page'], 'params' => $data['params'], 'tree' => $data['params']['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->params['language']; if (isset($this->engine->getData('page')['page'])) { $page = $this->engine->getData('page'); if (!empty($page['page']['language'])) { $language = $page['page']['language']; } } if (array_key_exists($language, $this->params['strings'])) { if (array_key_exists($key, $this->params['strings'][$language])) { return $this->params['strings'][$language][$key]; } } if (array_key_exists($key, $this->params['strings']['en'])) { return $this->params['strings']['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 .= "