Added path function

This commit is contained in:
Jeroen De Meerleer 2021-04-08 13:19:33 +02:00
parent 03a3d6659e
commit e38f7d106e
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 41 additions and 4 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ cache/*
vendor/
webcron.old/config.inc.php
\.idea/
.env
.env
storage/database.sqlite

View File

@ -18,8 +18,7 @@ abstract class Controller
public function __construct(Request $request, Kernel $kernel)
{
$loader = new FilesystemLoader([$kernel->getTemplateDir()]);
$this->twig = new Environment($loader);
$this->twig = new Twig($kernel);
$this->request = $request;
$this->kernel = $kernel;
}

37
lib/Framework/Twig.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace JeroenED\Framework;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class Twig
{
private Environment $environment;
private Kernel $kernel;
public function __construct(Kernel $kernel)
{
$loader = new FilesystemLoader([$kernel->getTemplateDir()]);
$this->environment = new Environment($loader);
$this->kernel = $kernel;
$this->addFunctions();
}
public function render(string $template, array $vars = []): string
{
return $this->environment->render($template, $vars);
}
public function addFunctions()
{
$path = new TwigFunction('path', function(string $route) {
return $this->kernel->getRouter()->getUrlForRoute($route);
});
$this->environment->addFunction($path);
}
}

View File

@ -17,7 +17,7 @@
<body>
<div class="col-md-4 col-md-offset-4 col-xs-12">
<h1>Webcron management</h1>
<form class="form-horizontal" method="post" action="/login_check">
<form class="form-horizontal" method="post" action="{{ path('login_check') }}">
{% if not error == "" %}
<div class="alert alert-danger fade in">
<a href="#" class="close" data-dismiss="alert">&times;</a>