generated from mirzaev/pot
Initial commit
This commit is contained in:
214
author/project/system/views/templater.php
Executable file
214
author/project/system/views/templater.php
Executable file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace mirzaev\shabashka\views;
|
||||
|
||||
// Files of the project
|
||||
use mirzaev\shabashka\models\session,
|
||||
mirzaev\shabashka\models\enumerations\language;
|
||||
|
||||
// Framework for PHP
|
||||
use mirzaev\minimal\controller;
|
||||
|
||||
// Templater of views
|
||||
use Twig\Loader\FilesystemLoader,
|
||||
Twig\Environment as twig,
|
||||
Twig\Extra\Intl\IntlExtension as intl,
|
||||
Twig\TwigFilter,
|
||||
Twig\TwigFunction;
|
||||
|
||||
// Built-in libraries
|
||||
use ArrayAccess as array_access,
|
||||
Error as error;
|
||||
|
||||
/**
|
||||
* Templater
|
||||
*
|
||||
* @package mirzaev\shabashka\views
|
||||
*
|
||||
* @param twig $twig Instance of the twig templater
|
||||
* @param array $variables Registry of view global variables
|
||||
*
|
||||
* @method void __construct(?session &$session) Constructor
|
||||
* @method string|null render(string $file, ?array $variables) Render the HTML-document
|
||||
*
|
||||
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||
* @author mirzaev <mail@domain.zone>
|
||||
*/
|
||||
final class templater extends controller implements array_access
|
||||
{
|
||||
/**
|
||||
* Twig
|
||||
*
|
||||
* @var twig $twig Instance of the twig templater
|
||||
*/
|
||||
readonly public twig $twig;
|
||||
|
||||
/**
|
||||
* Variables
|
||||
*
|
||||
* @var array $variables Registry of view global variables
|
||||
*/
|
||||
public array $variables = [];
|
||||
|
||||
/**
|
||||
* Constructor of an instance
|
||||
*
|
||||
* @param ?session $session Instance of the session in ArangoDB
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(?session &$session = null)
|
||||
{
|
||||
// Initializing the Twig instance
|
||||
$this->twig = new twig(new FilesystemLoader(VIEWS));
|
||||
|
||||
// Initializing global variables
|
||||
$this->twig->addGlobal('theme', 'default');
|
||||
$this->twig->addGlobal('server', $_SERVER);
|
||||
$this->twig->addGlobal('cookies', $_COOKIE);
|
||||
if (!empty($session->status())) $this->twig->addGlobal('session', $session);
|
||||
$this->twig->addGlobal('language', $language = $session?->buffer['language'] ?? language::en);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render
|
||||
*
|
||||
* Render the HTML-document
|
||||
*
|
||||
* @param string $file Related path to a HTML-document
|
||||
* @param array $variables Registry of variables to push into registry of global variables
|
||||
*
|
||||
* @return ?string HTML-document
|
||||
*/
|
||||
public function render(string $file, array $variables = []): ?string
|
||||
{
|
||||
// Generation and exit (success)
|
||||
return $this->twig->render('themes' . DIRECTORY_SEPARATOR . $this->twig->getGlobals()['theme'] . DIRECTORY_SEPARATOR . $file, $variables + $this->variables);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write
|
||||
*
|
||||
* Write the variable into the registry of the view global variables
|
||||
*
|
||||
* @param string $name Name of the variable
|
||||
* @param mixed $value Value of the variable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __set(string $name, mixed $value = null): void
|
||||
{
|
||||
// Write the variable and exit (success)
|
||||
$this->variables[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read
|
||||
*
|
||||
* Read the variable from the registry of the view global variables
|
||||
*
|
||||
* @param string $name Name of the variable
|
||||
*
|
||||
* @return mixed Content of the variable, if they are found
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
// Read the variable and exit (success)
|
||||
return $this->variables[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* Delete the variable from the registry of the view global variables
|
||||
*
|
||||
* @param string $name Name of the variable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __unset(string $name): void
|
||||
{
|
||||
// Delete the variable and exit (success)
|
||||
unset($this->variables[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check of initialization
|
||||
*
|
||||
* Check of initialization in the registry of the view global variables
|
||||
*
|
||||
* @param string $name Name of the variable
|
||||
*
|
||||
* @return bool The variable is initialized?
|
||||
*/
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
// Check of initialization of the variable and exit (success)
|
||||
return isset($this->variables[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write
|
||||
*
|
||||
* Write the variable into the registry of the view global variables
|
||||
*
|
||||
* @param mixed $name Name of an offset of the variable
|
||||
* @param mixed $value Value of the variable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet(mixed $name, mixed $value): void
|
||||
{
|
||||
// Write the variable and exit (success)
|
||||
$this->variables[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read
|
||||
*
|
||||
* Read the variable from the registry of the view global variables
|
||||
*
|
||||
* @param mixed $name Name of the variable
|
||||
*
|
||||
* @return mixed Content of the variable, if they are found
|
||||
*/
|
||||
public function offsetGet(mixed $name): mixed
|
||||
{
|
||||
// Read the variable and exit (success)
|
||||
return $this->variables[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* Delete the variable from the registry of the view global variables
|
||||
*
|
||||
* @param mixed $name Name of the variable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset(mixed $name): void
|
||||
{
|
||||
// Delete the variable and exit (success)
|
||||
unset($this->variables[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check of initialization
|
||||
*
|
||||
* Check of initialization in the registry of the view global variables
|
||||
*
|
||||
* @param mixed $name Name of the variable
|
||||
*
|
||||
* @return bool The variable is initialized?
|
||||
*/
|
||||
public function offsetExists(mixed $name): bool
|
||||
{
|
||||
// Check of initialization of the variable and exit (success)
|
||||
return isset($this->variables[$name]);
|
||||
}
|
||||
}
|
||||
|
10
author/project/system/views/themes/default/aside.html
Normal file
10
author/project/system/views/themes/default/aside.html
Normal file
@@ -0,0 +1,10 @@
|
||||
{% block css %}
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<aside>
|
||||
</aside>
|
||||
{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{% endblock %}
|
31
author/project/system/views/themes/default/core.html
Executable file
31
author/project/system/views/themes/default/core.html
Executable file
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="ru">
|
||||
|
||||
<head>
|
||||
{% use '/themes/default/head.html' with title as head_title, meta as head_meta, css as head_css %}
|
||||
|
||||
{% block title %}
|
||||
{{ block('head_title') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block meta %}
|
||||
{{ block('head_meta') }}
|
||||
{% endblock %}
|
||||
|
||||
{{ block('head_css') }}
|
||||
{% block css %}
|
||||
{% endblock %}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{% block body %}
|
||||
{% endblock %}
|
||||
|
||||
{% include '/themes/default/js.html' %}
|
||||
{% block js %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
10
author/project/system/views/themes/default/footer.html
Executable file
10
author/project/system/views/themes/default/footer.html
Executable file
@@ -0,0 +1,10 @@
|
||||
{% block css %}
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<footer>
|
||||
</footer>
|
||||
{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{% endblock %}
|
24
author/project/system/views/themes/default/head.html
Executable file
24
author/project/system/views/themes/default/head.html
Executable file
@@ -0,0 +1,24 @@
|
||||
{% block title %}
|
||||
<title>{% if head.title != empty %}{{ head.title }}{% else %}shabashka by mirzaev{% endif %}</title>
|
||||
{% endblock %}
|
||||
|
||||
{% block meta %}
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
{% for meta in head.metas %}
|
||||
<meta {% for name, value in meta.attributes %}{{ name }}="{{ value }}" {% endfor %}>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
||||
{% block css %}
|
||||
{% for element in css %}
|
||||
<link type="text/css" rel="stylesheet"{% if element.href %} href="{{ element.href }}"{% endif %} />
|
||||
{% endfor %}
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/fonts.css" />
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/system.css" />
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/header.css" />
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/main.css" />
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/aside.css" />
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/themes/default/footer.css" />
|
||||
<link type="text/css" rel="stylesheet" href="/themes/default/css/themes/default/colors.css" />
|
||||
{% endblock %}
|
10
author/project/system/views/themes/default/header.html
Executable file
10
author/project/system/views/themes/default/header.html
Executable file
@@ -0,0 +1,10 @@
|
||||
{% block css %}
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<header>
|
||||
</header>
|
||||
{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{% endblock %}
|
28
author/project/system/views/themes/default/index.html
Executable file
28
author/project/system/views/themes/default/index.html
Executable file
@@ -0,0 +1,28 @@
|
||||
{% extends "/themes/default/core.html" %}
|
||||
|
||||
{% use "/themes/default/header.html" with css as header_css, body as header, js as header_js %}
|
||||
{% use "/themes/default/aside.html" with css as aside_css, body as aside, js as aside_js %}
|
||||
{% use "/themes/default/footer.html" with css as footer_css, body as footer, js as footer_js %}
|
||||
|
||||
{% block css %}
|
||||
{{ block('header_css') }}
|
||||
{{ block('aside_css') }}
|
||||
{{ block('footer_css') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
{{ block('header') }}
|
||||
{{ block('aside') }}
|
||||
<main>
|
||||
{% block main %}
|
||||
{{ main|raw }}
|
||||
{% endblock %}
|
||||
</main>
|
||||
{{ block('footer') }}
|
||||
{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{{ block('footer_js') }}
|
||||
{{ block('header_js') }}
|
||||
{{ block('aside_js') }}
|
||||
{% endblock %}
|
5
author/project/system/views/themes/default/js.html
Executable file
5
author/project/system/views/themes/default/js.html
Executable file
@@ -0,0 +1,5 @@
|
||||
{% block js %}
|
||||
{% for element in js %}
|
||||
<script {% if element.src %}src="{{ element.src }}"{% endif %} {% if element.type %}type="{{ element.type }}"{% endif %}>{{ element.innerText }}</script>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
Reference in New Issue
Block a user