adding twig helper for routing and more

This commit is contained in:
Julien Fastré 2019-05-02 23:11:09 +02:00
parent 18463fb18a
commit 45a5583ab8
10 changed files with 226 additions and 0 deletions

View File

@ -45,3 +45,8 @@ Branche de développement
========================
- insert the title of the export inside the "download" page ;
- add twig helper "print_or_message" ;
- add twig helper for routing ;
- add css layout for boxes ;
- collect supplementary query parameters in SearchController ;

View File

@ -116,6 +116,7 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
$loader->load('services/command.yml');
$loader->load('services/phonenumber.yml');
$loader->load('services/cache.yml');
$loader->load('services/templating.yml');
}
public function getConfiguration(array $config, ContainerBuilder $container)

View File

@ -0,0 +1,11 @@
services:
Chill\MainBundle\Templating\ChillTwigHelper:
tags:
- { name: twig.extension }
Chill\MainBundle\Templating\ChillTwigRoutingHelper:
arguments:
$requestStack: '@Symfony\Component\HttpFoundation\RequestStack'
$originalExtension: '@twig.extension.routing'
tags:
- { name: twig.extension }

View File

@ -12,6 +12,7 @@
@import 'custom/address';
@import 'custom/record_actions';
@import 'custom/flash_messages';
@import 'custom/box';
html,body {

View File

@ -0,0 +1,24 @@
.chill__box {
font-variant: small-caps;
display: inline;
padding: .2em .6em .3em;
font-size: 0.88rem;
font-weight: bold;
line-height: 1;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
color: white;
&.green {
background-color: var(--chill-green);
color: white;
}
&.red {
background-color: var(--chill-red);
color: white;
}
}

View File

@ -39,6 +39,12 @@ Edit: Modifier
Update: Mettre à jour
Back to the list: Retour à la liste
#elements used in software
centers: centres
Centers: Centres
comment: commentaire
Comment: Commentaire
#pagination
Previous: Précédent
Next: Suivant

View File

@ -0,0 +1 @@
{% if value is not empty %}<blockquote class="chill-user-quote">{{ value|nl2br }}</blockquote>{% else %}<span class="chill-no-data-statement">{{ message|trans }}</span>{% endif %}

View File

@ -0,0 +1 @@
{% if value is not empty %}{{ value|trans }}{% else %}<span class="chill-no-data-statement">{{ message|trans }}</span>{% endif %}

View 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
]);
}
}

View File

@ -0,0 +1,114 @@
<?php
/*
*/
namespace Chill\MainBundle\Templating;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
/**
* Provides function to build path with returnPath.
*
* The logic of the function is based on the original routing extension.
*
*/
class ChillTwigRoutingHelper extends AbstractExtension
{
/**
*
* @var RequestStack
*/
protected $requestStack;
/**
*
* @var RoutingExtension
*/
protected $originalExtension;
public function __construct(
RequestStack $requestStack,
RoutingExtension $originalExtension
) {
$this->requestStack = $requestStack;
$this->originalExtension = $originalExtension;
}
public function getFunctions()
{
return [
new TwigFunction('chill_return_path_or', [$this, 'getReturnPathOr'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
new TwigFunction('chill_path_add_return_path', [$this, 'getPathAddReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
new TwigFunction('chill_path_forward_return_path', [$this, 'getPathForwardReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] )
];
}
public function isUrlGenerationSafe(\Twig_Node $argsNode)
{
return $this->originalExtension->isUrlGenerationSafe($argsNode);
}
/**
* Return the return path if it exists, or generate the path if not.
*
* @param string $name
* @param array $parameters
* @param bool $relative
* @return string
*/
public function getReturnPathOr($name, $parameters = [], $relative = false)
{
$request = $this->requestStack->getCurrentRequest();
if ($request->query->has('returnPath')) {
return $request->query->get('returnPath');
}
return $this->originalExtension->getPath($name, $parameters, $relative);
}
/**
* Build an url with a returnPath parameter to current page
*
* @param string $name
* @param array $parameters
* @param bool $relative
* @return string
*/
public function getPathAddReturnPath($name, $parameters = [], $relative = false)
{
$request = $this->requestStack->getCurrentRequest();
$parameters['returnPath'] = $request->getRequestUri();
return $this->originalExtension->getPath($name, $parameters, $relative);
}
/**
* Build an url with a returnPath parameter to current page
*
* @param string $name
* @param array $parameters
* @param bool $relative
* @return string
*/
public function getPathForwardReturnPath($name, $parameters = [], $relative = false)
{
$request = $this->requestStack->getCurrentRequest();
if ($request->query->has('returnPath')) {
$parameters['returnPath'] = $request->query->get('returnPath');
}
return $this->originalExtension
->getPath(
$name,
$parameters,
$relative
);
}
}