Using date and interval filters

This commit is contained in:
Jeroen De Meerleer 2021-04-08 13:28:13 +02:00
parent e38f7d106e
commit af9bb9691a
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
2 changed files with 26 additions and 2 deletions

View File

@ -6,6 +6,7 @@ namespace JeroenED\Framework;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
class Twig
@ -19,6 +20,7 @@ class Twig
$this->environment = new Environment($loader);
$this->kernel = $kernel;
$this->addFunctions();
$this->addFilters();
}
public function render(string $template, array $vars = []): string
@ -32,6 +34,28 @@ class Twig
return $this->kernel->getRouter()->getUrlForRoute($route);
});
$this->environment->addFunction($path);
}
public function addFilters() {
$secondsToInterval = new TwigFilter('interval', function(int $time) {
$days = floor($time / (60 * 60 * 24));
$time -= $days * (60 * 60 * 24);
$hours = floor($time / (60 * 60));
$time -= $hours * (60 * 60);
$minutes = floor($time / 60);
$time -= $minutes * 60;
$seconds = floor($time);
$time -= $seconds;
return "{$days}d {$hours}h {$minutes}m {$seconds}s";
});
$this->environment->addFilter($secondsToInterval);
}
}

View File

@ -20,8 +20,8 @@
<tr{% if(job.norun == true) %} class="norun"{% endif %}>
<td>{{ job.name }}</td>
<td>{{ job.data.host }}</td>
<td>{{ job.delay }}</td>
<td>{{ job.nextrun }}</td>
<td>{{ job.delay | interval }}</td>
<td>{{ job.nextrun | date("d/m/Y H:i:s") }}</td>
<td>
<a href="#" data-id="{{ job.id }}" class="runcron btn btn-default"><span class="glyphicon glyphicon-play"><span></a>
<a href="runs.php?jobID={{ job.id }}" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></a>