Skip to content

Commit

Permalink
twiggy: compiles html_classes as n:class
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 7, 2021
1 parent c5c7499 commit 56f87c2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/TwigConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public function convert(string $code): string
$loader = $this->twiggy->getLoader();
$loader->setTemplate('main', $code);
$code = $this->twiggy->compileSource($loader->getSourceContext('main'));
$code = $this->postProcess($code);
return $code;
}


private function postProcess(string $code): string
{
$code = preg_replace('~\bclass=(["\']){html_classes\((.*)\)}~i', 'n:class=$1$2', $code);
return $code;
}
}
6 changes: 6 additions & 0 deletions src/Twiggy/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ public function setNode(int|string $name, self $node): void
}


public function setNodes(array $nodes): void
{
$this->nodes = $nodes;
}


public function removeNode(int|string $name): void
{
unset($this->nodes[$name]);
Expand Down
35 changes: 35 additions & 0 deletions src/Twiggy/NodeVisitor/LatteNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
namespace LatteTools\Twiggy\NodeVisitor;

use LatteTools\Twiggy\Environment;
use LatteTools\Twiggy\Node\Expression\ArrayExpression;
use LatteTools\Twiggy\Node\Expression\ConditionalExpression;
use LatteTools\Twiggy\Node\Expression\ConstantExpression;
use LatteTools\Twiggy\Node\Expression\FilterExpression;
use LatteTools\Twiggy\Node\Expression\FunctionExpression;
use LatteTools\Twiggy\Node\Expression\MethodCallExpression;
Expand Down Expand Up @@ -41,6 +44,15 @@ public function leaveNode(Node $node, Environment $env): ?Node
return $this->filterToFunction($node);
}

if ($node instanceof FunctionExpression) {
$name = $node->getAttribute('name');

// html_classes
if ($name === 'html_classes') {
return $this->functionHtmlClasses($node);
}
}

if ($node instanceof PrintNode) {
$expr = $node->getNode('expr');

Expand Down Expand Up @@ -88,4 +100,27 @@ private function filterToFunction(FilterExpression $node): Node
}
return new FunctionExpression($funcs[$name], new Node($arguments), $node->getTemplateLine());
}


private function functionHtmlClasses(FunctionExpression $node): FunctionExpression
{
$res = [];
$arguments = $node->getNode('arguments');
foreach ($arguments as $argument) {
if ($argument instanceof ArrayExpression) {
foreach ($argument->getKeyValuePairs() as ['key' => $key, 'value' => $value]) {
$res[] = new ConditionalExpression(
$value,
$key,
new ConstantExpression(null, $argument->getTemplateLine()),
$argument->getTemplateLine()
);
}
} else {
$res[] = $argument;
}
}
$arguments->setNodes($res);
return $node;
}
}
2 changes: 1 addition & 1 deletion tests/fixtures-twig/functions/html_classes.latte
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p class="{html_classes(a-class, another-class, [errored => $object->errored, finished => $object->finished, pending => $object->pending])}">How are you doing?</p>
<p n:class="a-class, another-class, $object->errored ? errored, $object->finished ? finished, $object->pending ? pending">How are you doing?</p>

0 comments on commit 56f87c2

Please sign in to comment.