diff --git a/src/Twig/Extension/RoutingExtension.php b/src/Twig/Extension/RoutingExtension.php index cc5dd0e..d54d2c0 100644 --- a/src/Twig/Extension/RoutingExtension.php +++ b/src/Twig/Extension/RoutingExtension.php @@ -31,6 +31,7 @@ public function getFunctions(): array { return [ new TwigFunction('ctrl_fqcn', $this->getControllerFqcn(...)), + new TwigFunction('attr_if', $this->getAttributeIf(...)), ]; } @@ -70,4 +71,18 @@ public function getControllerFqcn(string $ctrlShortname): string throw new \InvalidArgumentException('No controller found for the "'.$ctrlShortname.'" shortname.'); } + + /** + * Returns an HTML attribute with a given value only if a condition is met. + * + * @see templates/base.html.twig + */ + public function getAttributeIf(bool $condition, string $attribute, string $value): string + { + if (!$condition) { + return ''; + } + + return \sprintf(' %s="%s"', $attribute, $value); + } } diff --git a/templates/base.html.twig b/templates/base.html.twig index d7a699e..4a07823 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -42,13 +42,14 @@ Menu @@ -57,7 +58,7 @@
  • 🌞 - 🌘 + 🌘