Skip to content

Commit

Permalink
snippet wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 1, 2023
1 parent 70f8692 commit e85d315
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Bridges/ApplicationLatte/Nodes/ControlNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ControlNode extends StatementNode
public ?ExpressionNode $method = null;
public ArrayNode $args;
public ?bool $escape = null;
public bool $inSnippetArea = false;


public static function create(Tag $tag): static
Expand All @@ -40,6 +41,7 @@ public static function create(Tag $tag): static
$stream = $tag->parser->stream;
$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
$node->inSnippetArea = $tag->closestTag(['snippetArea']) && !$tag->closestTag(['snippet']);
if ($stream->tryConsume(':')) {
$node->method = $tag->parser->parseExpression();
}
Expand Down Expand Up @@ -94,11 +96,14 @@ public function print(PrintContext $context): string
$this->name,
);

$fetchCode .= $this->inSnippetArea
? 'if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->snippetMode = $this->global->uiControl->snippetMode;'
: 'if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, $ʟ_tmp->snippetMode = false);';

if ($this->escape) {
return $context->format(
<<<'XX'
%raw
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);
ob_start(fn() => '');
$ʟ_tmp->%raw(%args) %line;
$ʟ_fi = new LR\FilterInfo(%dump); echo %modifyContent(ob_get_clean());
Expand All @@ -117,7 +122,6 @@ public function print(PrintContext $context): string
return $context->format(
<<<'XX'
%raw
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);
$ʟ_tmp->%raw(%args) %line;


Expand Down
3 changes: 1 addition & 2 deletions src/Bridges/ApplicationLatte/SnippetRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public function renderSnippets(array $blocks, array $params): bool
}

$this->renderingSnippets = true;
$this->control->snippetMode = false;
foreach ($blocks as $name => $block) {
if (!$this->control->isControlInvalid($name)) {
continue;
Expand All @@ -117,7 +116,7 @@ public function renderSnippets(array $blocks, array $params): bool
$function($params);
}

$this->control->snippetMode = true;
$this->control->redrawControl(null, false);
$this->renderChildren();
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Bridges.Latte3/renderSnippets.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class InnerControl extends Nette\Application\UI\Control
public function render()
{
$latte = new Latte\Engine;
$latte->setTempDirectory('temp');
$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($this));
$params['say'] = 'Hello';
$latte->render(__DIR__ . '/templates/snippet-included.latte', $params);
Expand All @@ -43,6 +44,7 @@ class TestPresenter extends Nette\Application\UI\Presenter
public function render()
{
$latte = new Latte\Engine;
$latte->setTempDirectory('temp');
$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($this));
$latte->render(__DIR__ . '/templates/snippet-include.latte');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Bridges.Latte3/{control}.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Assert::match(
<<<'XX'
%A%
$ʟ_tmp = $this->global->uiControl->getComponent('form');
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, $ʟ_tmp->snippetMode = false);
$ʟ_tmp->render() /* line 1 */;
%A%
XX,
Expand All @@ -36,7 +36,7 @@ Assert::match(
<<<'XX'
%A%
if (!is_object($ʟ_tmp = $form)) $ʟ_tmp = $this->global->uiControl->getComponent($ʟ_tmp);
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, $ʟ_tmp->snippetMode = false);
$ʟ_tmp->render() /* line 1 */;
%A%
XX,
Expand Down

0 comments on commit e85d315

Please sign in to comment.