Skip to content

Commit

Permalink
Pass twig false as vue false in forum/submit.twig
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Oct 14, 2024
1 parent 2a317f2 commit cc45bec
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
20 changes: 20 additions & 0 deletions app/Services/TwigBridge/Extensions/Vue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Coyote\Services\TwigBridge\Extensions;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class Vue extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('vueBoolean', $this->vueBoolean(...)),
];
}

private function vueBoolean(bool $value): string
{
return $value ? 'true' : 'false';
}
}
1 change: 1 addition & 0 deletions config/twigbridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
'Coyote\Services\TwigBridge\Extensions\FormBuilder',
'Coyote\Services\TwigBridge\Extensions\Media',
'Coyote\Services\TwigBridge\Extensions\Icon',
'Coyote\Services\TwigBridge\Extensions\Vue',
],

/*
Expand Down
4 changes: 2 additions & 2 deletions resources/views/forum/submit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
:show-sticky-checkbox="showStickyCheckbox"
:post="undefinedPost"
:popular-tags="popularTags"
require-tag="{{ forum.require_tag }}"
show-tags-input="{{ forum.enable_tags }}"
:require-tag="{{ vueBoolean(forum.require_tag) }}"
:show-tags-input="{{ vueBoolean(forum.enable_tags) }}"
@save="redirectToTopic"
></vue-form>
<vue-notifications></vue-notifications>
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Vue/TwigExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Tests\Unit\Vue;

use Coyote\Domain\Html;
use Coyote\Services\TwigBridge\Extensions\Vue;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Runtime\EscaperRuntime;

class TwigExtensionTest extends TestCase
{
#[Test]
public function booleanTrue(): void
{
$this->assertSame(
'true',
$this->twig("{{ vueBoolean(value) }}", [
'value' => true,
]));
}

#[Test]
public function booleanFalse(): void
{
$this->assertSame(
'false',
$this->twig("{{ vueBoolean(value) }}", [
'value' => false,
]));
}

private function twig(string $sourceCode, array $values): string
{
$twig = new Environment(new ArrayLoader());
$twig->getRuntime(EscaperRuntime::class)->addSafeClass(Html::class, ['html']);
$twig->addExtension(new Vue());
return $twig->createTemplate($sourceCode)->render($values);
}
}

0 comments on commit cc45bec

Please sign in to comment.