<?php namespace App\Libraries;
use CodeIgniter\View\View;
use Config\Services;
class TemplateLibrary {
private $renderer;
private $template;
public function __construct() {
$this->template = [];
/* Theme */
$this->template['theme'] = 'default';
/* Headline */
$this->template['headline'] = '';
/* Breadcrumb */
$this->template['breadcrumb'] = '';
/* Canonical */
$this->template['canonical'] = '';
/* Meta */
$this->template['meta'] = [
'title' => '',
'description' => '',
'keywords' => '',
'robots' => 'INDEX, FOLLOW'
];
/* Css files */
$this->template['css_files'] = [];
/* Js files */
$this->template['js_files'] = [];
}
public function setTheme(string $theme) {
$this->template['theme'] = $theme;
}
public function setHeadline(string $headline) {
$this->template['headline'] = $headline;
}
public function setBreadcrumb(array $breadcrumb) {
$this->template['breadcrumb'] = $breadcrumb;
}
public function setCanonical(string $canonical) {
$this->template['canonical'] = $canonical;
}
public function setMeta(string $key, $value) {
if ( array_key_exists($key, $this->template['meta']) ) {
$this->template['meta'][$key] = (string) $value;
}
}
public function addCssFile(string $key, string $path, int $priority) {
if ( strpos($path, 'https://') === false ) {
$path = href('themes/' . strtolower($this->template['theme']) . '/' . $path);
}
$this->template['css_files'][$key]['path'] = $path;
$this->template['css_files'][$key]['priority'] = $priority;
usort($this->template['css_files'], function($a, $b) {
return $a['priority'] - $b['priority'];
});
}
public function addJsFile(string $key, string $path, int $priority) {
if ( strpos($path, 'https://') === false ) {
$path = href('themes/' . strtolower($this->template['theme']) . '/' . $path);
}
$this->template['js_files'][$key]['path'] = $path;
$this->template['js_files'][$key]['priority'] = $priority;
usort($this->template['js_files'], function($a, $b) {
return $a['priority'] - $b['priority'];
});
}
public function getTemplate() {
return $this->template;
}
public function getTheme() {
return $this->template['theme'];
}
public function getHeadline() {
return $this->template['headline'];
}
public function getBreadcrumb() {
return $this->template['breadcrumb'];
}
public function getCanonical() {
return $this->template['canonical'];
}
public function getMeta(string $key) {
if ( array_key_exists($key, $this->template['meta']) ) {
return $this->template['meta'][$key];
} return NULL;
}
public function getCssFiles() {
return $this->template['css_files'];
}
public function getJsFiles() {
return $this->template['js_files'];
}
/*
public function renderView(string $name, array $data = [], array $options = []) {
return view('MyApp\Views\\' . ucfirst($this->template['theme']) . '\\' . $name, $data, $options);
}
public function render(string $name, array $data = [], array $options = []) {
$data['template'] = $this;
return $this->renderView($name, $data, ['saveData' => true]);
}
*/
private function getRenderer() {
if ( $this->renderer !== NULL ) {
return $this->renderer;
}
$path = ROOTPATH . 'myapp/Views/' . ucfirst($this->template['theme']);
$this->renderer = Services::renderer($path, null, false);
return $this->renderer;
}
public function renderView(string $name, array $data = [], array $options = []) {
$renderer = $this->getRenderer();
$saveData = null;
if (array_key_exists('saveData', $options)) {
$saveData = (bool) $options['saveData'];
unset($options['saveData']);
}
return $renderer->setData($data, 'raw')
->render($name, $options, $saveData);
}
public function render(string $name, array $data = []) {
$data['template'] = $this->template;
return $this->renderView($name, $data, ['saveData' => true]);
}
}