daux.io/libs/Format/HTML/Template.php

212 lines
6.8 KiB
PHP
Raw Normal View History

<?php
namespace Todaymade\Daux\Format\HTML;
2015-04-23 10:24:50 +02:00
use League\Plates\Engine;
2018-06-05 20:31:51 +02:00
use Symfony\Component\Console\Output\OutputInterface;
2017-12-11 21:14:18 +01:00
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory;
2015-04-23 10:24:50 +02:00
class Template
{
protected $engine;
protected $params;
2015-07-17 23:38:06 +02:00
/**
* @param string $base
* @param string $theme
*/
2017-12-11 21:14:18 +01:00
public function __construct(Config $params)
2015-04-23 10:24:50 +02:00
{
$this->params = $params;
2017-12-11 21:14:18 +01:00
}
public function getEngine(Config $params)
{
if ($this->engine) {
return $this->engine;
}
$base = $params['templates'];
$theme = $params['theme']['templates'];
2017-10-18 21:15:35 +02:00
// Use internal templates if no templates
// dir exists in the working directory
if (!is_dir($base)) {
2017-10-18 21:15:35 +02:00
$base = __DIR__ . '/../../../templates';
}
2015-04-23 10:24:50 +02:00
// Create new Plates instance
$this->engine = new Engine($base);
if (!is_dir($theme)) {
$theme = $base;
}
$this->engine->addFolder('theme', $theme, true);
2018-06-05 20:31:51 +02:00
Daux::writeln("Starting Template engine with basedir '$base' and theme folder '$theme'.", OutputInterface::VERBOSITY_VERBOSE);
2017-12-11 21:14:18 +01:00
$this->registerFunctions($this->engine);
return $this->engine;
2015-04-23 10:24:50 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* @param string $name
* @param array $data
* @return string
*/
2016-07-27 21:32:51 +02:00
public function render($name, array $data = [])
2015-04-23 10:24:50 +02:00
{
2017-12-11 21:14:18 +01:00
$engine = $this->getEngine($data['params']);
$engine->addData([
2015-04-23 10:24:50 +02:00
'base_url' => $data['params']['base_url'],
'base_page' => $data['params']['base_page'],
'page' => $data['page'],
'params' => $data['params'],
'tree' => $data['params']['tree'],
]);
2018-06-05 20:31:51 +02:00
Daux::writeln("Rendering template '$name'", OutputInterface::VERBOSITY_VERBOSE);
2017-12-11 21:14:18 +01:00
return $engine->render($name, $data);
2015-04-23 10:24:50 +02:00
}
2017-12-11 21:14:18 +01:00
protected function registerFunctions($engine)
2015-04-23 10:24:50 +02:00
{
$engine->registerFunction('get_navigation', function($tree, $path, $current_url, $base_page, $mode) {
2015-04-23 20:42:44 +02:00
$nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode);
2016-07-27 21:32:51 +02:00
2015-04-23 20:42:44 +02:00
return $this->renderNavigation($nav);
2015-04-23 10:24:50 +02:00
});
$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) {
2015-04-23 10:24:50 +02:00
$title = '';
$breadcrumb_trail = $page['breadcrumb_trail'];
$separator = $this->getSeparator($page['breadcrumb_separator']);
foreach ($breadcrumb_trail as $value) {
$title .= '<a href="' . $base_page . $value['url'] . '">' . $value['title'] . '</a>' . $separator;
2015-04-23 10:24:50 +02:00
}
if ($page['filename'] === 'index' || $page['filename'] === '_index') {
if ($page['title'] != '') {
$title = substr($title, 0, -1 * strlen($separator));
}
} else {
$title .= '<a href="' . $base_page . $page['request'] . '">' . $page['title'] . '</a>';
}
2016-07-27 21:32:51 +02:00
2015-04-23 10:24:50 +02:00
return $title;
});
}
2015-04-23 20:42:44 +02:00
private function renderNavigation($entries)
{
2016-07-27 21:32:51 +02:00
$nav = '';
2015-04-23 20:42:44 +02:00
foreach ($entries as $entry) {
if (array_key_exists('children', $entry)) {
2016-05-23 20:09:15 +02:00
$icon = '<i class="Nav__arrow">&nbsp;</i>';
2015-04-23 20:42:44 +02:00
if (array_key_exists('href', $entry)) {
2019-09-22 21:42:03 +02:00
$link = '<a href="' . $entry['href'] . '" class="Nav__item__link">' . $icon . $entry['title'] . '</a>';
2015-04-23 20:42:44 +02:00
} else {
2019-09-22 21:42:03 +02:00
$link = '<a href="#" class="Nav__item__link Nav__item__link--nopage">' . $icon . $entry['title'] . '</a>';
2015-04-23 20:42:44 +02:00
}
$link .= $this->renderNavigation($entry['children']);
} else {
$link = '<a href="' . $entry['href'] . '">' . $entry['title'] . '</a>';
}
2016-05-23 20:09:15 +02:00
$nav .= "<li class='Nav__item $entry[class]'>$link</li>";
2015-04-23 20:42:44 +02:00
}
2016-05-23 20:09:15 +02:00
return "<ul class='Nav'>$nav</ul>";
2015-04-23 20:42:44 +02:00
}
private function buildNavigation(Directory $tree, $path, $current_url, $base_page, $mode)
2015-04-23 10:24:50 +02:00
{
2015-04-23 20:42:44 +02:00
$nav = [];
foreach ($tree->getEntries() as $node) {
2015-04-23 10:24:50 +02:00
$url = $node->getUri();
if ($node instanceof Content) {
if ($node->isIndex()) {
2015-04-23 10:24:50 +02:00
continue;
}
2015-04-23 20:42:44 +02:00
2015-04-23 10:24:50 +02:00
$link = ($path === '') ? $url : $path . '/' . $url;
2015-04-23 20:42:44 +02:00
$nav[] = [
'title' => $node->getTitle(),
'href' => $base_page . $link,
'class' => $node->isHotPath() ? 'Nav__item--active' : '',
2015-04-23 20:42:44 +02:00
];
} elseif ($node instanceof Directory) {
2015-07-29 08:30:41 +02:00
if (!$node->hasContent()) {
continue;
}
2015-04-23 20:42:44 +02:00
$folder = [
'title' => $node->getTitle(),
'class' => $node->isHotPath() ? 'Nav__item--open' : '',
2015-04-23 20:42:44 +02:00
];
if ($index = $node->getIndexPage()) {
$folder['href'] = $base_page . $index->getUrl();
2015-04-23 10:24:50 +02:00
}
2015-04-23 20:42:44 +02:00
//Child pages
2015-04-23 10:24:50 +02:00
$new_path = ($path === '') ? $url : $path . '/' . $url;
2015-04-23 20:42:44 +02:00
$folder['children'] = $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
if (!empty($folder['children'])) {
$folder['class'] .= ' has-children';
}
2015-04-23 20:42:44 +02:00
$nav[] = $folder;
2015-04-23 10:24:50 +02:00
}
}
2016-07-27 21:32:51 +02:00
2015-04-23 10:24:50 +02:00
return $nav;
}
2015-07-17 23:38:06 +02:00
/**
* @param string $separator
* @return string
*/
2015-04-23 10:24:50 +02:00
private function getSeparator($separator)
{
switch ($separator) {
case 'Chevrons':
2016-05-23 20:09:15 +02:00
return ' <svg class="Page__header--separator" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 477.175 477.175"><path d="M360.73 229.075l-225.1-225.1c-5.3-5.3-13.8-5.3-19.1 0s-5.3 13.8 0 19.1l215.5 215.5-215.5 215.5c-5.3 5.3-5.3 13.8 0 19.1 2.6 2.6 6.1 4 9.5 4 3.4 0 6.9-1.3 9.5-4l225.1-225.1c5.3-5.2 5.3-13.8.1-19z"/></svg> ';
2015-04-23 10:24:50 +02:00
default:
return $separator;
}
}
}