mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?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
|
|
]);
|
|
}
|
|
}
|