Small building blocks for single-page sites without a framework: a Latte engine with $basePath,
traits for the session, flash messages and redirects, and a translator.
- PHP >= 8.3, < 9
- Nette Framework
- Composer
composer require drago-ex/simple
Drago\Simple\Latte is a Latte engine that passes $basePath to every template:
use Drago\Simple\Latte;
$latte = new Latte;
$latte->setTempDirectory(__DIR__ . '/../temp/latte');<link rel="stylesheet" href="{$basePath}/css/style.css">use Drago\Simple\Base\Session;
// The session is created from the current request on the first call and then reused
$this->session();use Drago\Simple\Base\Message;
// Save a message for the next page
$this->flashMessage('Message...');
// Read the message; it is removed after reading, so it is shown only once
$this->getFlashMessage();use Drago\Simple\Base\Response;
// Redirect to a URL; the script stops after the redirect is sent
$this->redirect('#');use Drago\Simple\Base\Message;
use Drago\Simple\Latte;
final class Home
{
use Message;
public function __construct(
private Latte $latte,
) {
}
public function render(): void
{
$this->latte->render(__DIR__ . '/templates/Home.latte', [
'message' => $this->getFlashMessage(),
]);
}
}<p n:if="$message">{$message}</p>Install Nette Forms via Composer:
composer require nette/forms
Register the form tags in Latte:
$latte->addExtension(new \Nette\Bridges\FormsLatte\FormsExtension);Drago\Simple\Translator reads the messages of one language from a PHP file returning an array:
// locale/cs.php
return [
'Hello' => 'Ahoj',
'Hello %s' => 'Ahoj %s',
];Choose the language from the request and register the translator in Latte:
use Drago\Simple\Translator;
use Nette\Http\RequestFactory;
$lang = (new RequestFactory)->fromGlobals()->detectLanguage(['en', 'cs']) ?? 'en';
$translator = new Translator(__DIR__ . '/locale', $lang);
$latte->addExtension(new \Latte\Essential\TranslatorExtension($translator));Translations are then available in templates as {_'Hello'}, {_'Hello %s', $name} or {$text|translate};
a message without a translation is shown as it is. Every language needs its file (locale/en.php may return
[]), otherwise the translator throws an exception. It also works as the translator of Nette Forms.
Drago Simple Project is a ready project with this package and a Docker server.