mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 21:13:57 +00:00
adding twig helper for routing and more
This commit is contained in:
62
Templating/ChillTwigHelper.php
Normal file
62
Templating/ChillTwigHelper.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/*
|
||||
*
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ChillTwigHelper extends AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_print_or_message', [$this, 'printOrMessage'], [
|
||||
'needs_environment' => true,
|
||||
'is_safe' => ['html', 'html_attrs']
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Print `value` inside a template, or, if $value is empty,
|
||||
* print $message.
|
||||
*
|
||||
* The template can be customized. The template is a full path to another
|
||||
* template, or one of the key belows:
|
||||
*
|
||||
* - 'default' ;
|
||||
* - 'blockquote' ;
|
||||
*
|
||||
* @param Environment $twig
|
||||
* @param string $value
|
||||
* @param string $message
|
||||
* @param string $template
|
||||
* @return string
|
||||
*/
|
||||
public function printOrMessage(
|
||||
Environment $twig,
|
||||
$value,
|
||||
$message = 'No value',
|
||||
$template = 'default'
|
||||
) {
|
||||
switch ($template) {
|
||||
case 'default':
|
||||
case 'blockquote':
|
||||
$t = '@ChillMain/Extensions/PrintOrMessage/'.$template.'.html.twig';
|
||||
break;
|
||||
default:
|
||||
$t = $template;
|
||||
}
|
||||
|
||||
return $twig->render($t, [
|
||||
'value' => $value,
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user