Skip to content

Commit

Permalink
Add Twig functions to get variants args and blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
loicsapone committed Apr 25, 2024
1 parent 0e47d00 commit cabcda4
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/IQ2iStoriaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use IQ2i\StoriaBundle\Menu\MenuBuilder;
use IQ2i\StoriaBundle\Twig\HighlightExtension;
use IQ2i\StoriaBundle\Twig\MenuExtension;
use IQ2i\StoriaBundle\Twig\ViewExtension;
use IQ2i\StoriaBundle\View\ViewBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
Expand Down Expand Up @@ -81,6 +82,11 @@ public function loadExtension(array $config, ContainerConfigurator $container, C
->setArguments([
new Reference(MenuBuilder::class),
]);
$builder->register(ViewExtension::class)
->addTag('twig.extension')
->setArguments([
'%iq2i_storia.default_path%',
]);
}

public function build(ContainerBuilder $container): void
Expand Down
60 changes: 60 additions & 0 deletions src/Twig/ViewExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the UI Storia project.
*
* (c) Loïc Sapone <loic@sapone.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace IQ2i\StoriaBundle\Twig;

use IQ2i\StoriaBundle\Config\ViewConfiguration;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Yaml;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class ViewExtension extends AbstractExtension
{
public function __construct(
private readonly string $defaultPath,
) {
}

public function getFunctions(): array
{
return [
new TwigFunction('iq2i_storia_variant_args', [$this, 'getVariantArgs']),
new TwigFunction('iq2i_storia_variant_blocks', [$this, 'getVariantBlocks']),
];
}

public function getVariantArgs(string $view, string $variant): array
{
$yaml = Yaml::parse(file_get_contents($this->defaultPath.'/components/'.$view.'.yaml'));
$config = (new Processor())->processConfiguration(new ViewConfiguration(), [$yaml]);

if (!\array_key_exists($variant, $config['variants'])) {
throw new \InvalidArgumentException('Unknown variant: '.$variant);
}

return $config['variants'][$variant]['args'];
}

public function getVariantBlocks(string $view, string $variant): array
{
$yaml = Yaml::parse(file_get_contents($this->defaultPath.'/components/'.$view.'.yaml'));
$config = (new Processor())->processConfiguration(new ViewConfiguration(), [$yaml]);

if (!\array_key_exists($variant, $config['variants'])) {
throw new \InvalidArgumentException('Unknown variant: '.$variant);
}

return $config['variants'][$variant]['blocks'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@

{% block content %}
<p>This is the main content of the page</p>

{% set button_args = iq2i_storia_variant_args('button/button', 'plain_icon_right') %}
<twig:Button class="{{ button_args.class }}" icon="{{ button_args.icon }}">
{{ iq2i_storia_variant_blocks('button/button', 'plain_icon_right').content }}
</twig:Button>
{% endblock %}
{% endblock %}
30 changes: 30 additions & 0 deletions tests/Twig/ViewExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the UI Storia project.
*
* (c) Loïc Sapone <loic@sapone.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace IQ2i\StoriaBundle\Tests\Twig;

use IQ2i\StoriaBundle\Twig\ViewExtension;
use Twig\Test\IntegrationTestCase;

class ViewExtensionTest extends IntegrationTestCase
{
protected function getFixturesDir(): string
{
return __DIR__.'/fixtures';
}

protected function getExtensions(): iterable
{
yield new ViewExtension(\dirname(__DIR__).'/TestApplication/storia');
}
}
8 changes: 8 additions & 0 deletions tests/Twig/fixtures/variant_args.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
"iq2i_storia_variant_args" function
--TEMPLATE--
{{ iq2i_storia_variant_args('button/button', 'plain_icon_right').class }}
--DATA--
return []
--EXPECT--
text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center inline-flex items-center me-2 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800
8 changes: 8 additions & 0 deletions tests/Twig/fixtures/variant_blocks.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
"iq2i_storia_variant_blocks" function
--TEMPLATE--
{{ iq2i_storia_variant_blocks('button/button', 'plain_icon_right').content }}
--DATA--
return []
--EXPECT--
Choose plan

0 comments on commit cabcda4

Please sign in to comment.