Skip to content

Commit

Permalink
{capture} creates Html object only in HTML-TEXT context
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 24, 2021
1 parent 4041bd3 commit c3abdb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function macroCapture(MacroNode $node, PhpWriter $writer)
*/
public function macroCaptureEnd(MacroNode $node, PhpWriter $writer)
{
$body = in_array($node->context[0], [Engine::CONTENT_HTML, Engine::CONTENT_XHTML], true)
$body = in_array(implode($node->context), [Engine::CONTENT_HTML, Engine::CONTENT_XHTML], true)
? 'ob_get_length() ? new LR\\Html(ob_get_clean()) : ob_get_clean()'
: 'ob_get_clean()';
return $writer->write("\$_fi = new LR\\FilterInfo(%var); %raw = %modifyContent($body);", implode($node->context), $node->data->variable);
Expand Down
41 changes: 41 additions & 0 deletions tests/Latte/CoreMacros.capture.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Test: {capture}
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

Assert::match(
'',
$latte->renderToString('{capture $var}<html>{/capture}')
);

Assert::match(
'string',
$latte->renderToString('{capture $var}{/capture}{=gettype($var)}')
);

Assert::match(
Latte\Runtime\Html::class,
$latte->renderToString('{capture $var}<html>{/capture}{=get_class($var)}')
);

Assert::match(
'TEST',
$latte->renderToString('{capture $var|stripHtml|upper}<b>Test</b>{/capture}{=$var}')
);

Assert::match(
'<!-- --> &lt;foo&gt;',
$latte->renderToString('<!-- {capture $x}<foo>{/capture} --> {$x}')
);

0 comments on commit c3abdb6

Please sign in to comment.