Skip to content

Commit dc5363d

Browse files
committed
tests: compatibility with Latte master
1 parent 421256c commit dc5363d

File tree

9 files changed

+212
-175
lines changed

9 files changed

+212
-175
lines changed

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function finalize()
4747
{
4848
$prolog = '
4949
// snippets support
50-
if (empty($_l->extends) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
50+
if (empty($this->local->parentName) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
5151
return Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($_control, $_b, get_defined_vars());
5252
}';
5353
return [$prolog, ''];
@@ -73,10 +73,10 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
7373
if (!Strings::contains($node->args, '=>')) {
7474
$param = substr($param, $param[0] === '[' ? 1 : 6, -1); // removes array() or []
7575
}
76-
return ($name[0] === '$' ? "if (is_object($name)) \$_l->tmp = $name; else " : '')
77-
. '$_l->tmp = $_control->getComponent(' . $name . '); '
78-
. 'if ($_l->tmp instanceof Nette\Application\UI\IRenderable) $_l->tmp->redrawControl(NULL, FALSE); '
79-
. ($node->modifiers === '' ? "\$_l->tmp->$method($param)" : $writer->write("ob_start(function () {}); \$_l->tmp->$method($param); echo %modify(ob_get_clean())"));
76+
return ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
77+
. '$_tmp = $_control->getComponent(' . $name . '); '
78+
. 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(NULL, FALSE); '
79+
. ($node->modifiers === '' ? "\$_tmp->$method($param)" : $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean())"));
8080
}
8181

8282

src/Bridges/ApplicationLatte/UIRuntime.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ class UIRuntime
1919
{
2020
use Nette\StaticClass;
2121

22-
public static function renderSnippets(UI\Control $control, \stdClass $local = NULL, array $params = [])
22+
public static function renderSnippets(UI\Control $control, \stdClass $blockContext = NULL, array $params = [])
2323
{
2424
$control->snippetMode = FALSE;
2525
$payload = $control->getPresenter()->getPayload();
26-
if (isset($local->blocks)) {
27-
foreach ($local->blocks as $name => $function) {
26+
if (isset($blockContext->blocks)) {
27+
foreach ($blockContext->blocks as $name => $function) {
2828
if ($name[0] !== '_' || !$control->isControlInvalid((string) substr($name, 1))) {
2929
continue;
3030
}
3131
ob_start(function () {});
3232
$function = reset($function);
33-
$snippets = $function($local, $params + ['_snippetMode' => TRUE]);
33+
$snippets = $function($blockContext, $params + ['_snippetMode' => TRUE]);
3434
$payload->snippets[$id = $control->getSnippetId((string) substr($name, 1))] = ob_get_clean();
3535
if ($snippets !== NULL) { // pass FALSE from snippetArea
3636
if ($snippets) {

tests/Bridges.Latte/UIMacros.control.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ UIMacros::install($compiler);
1616

1717
// {control ...}
1818
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render() ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
19-
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render(); echo $template->filter(%a%) ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode);
19+
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render(); echo call_user_func($this->filters->filter, %a%) ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode);
2020
Assert::match('<?php if (is_object($form)) %a% else %a% $_control->getComponent($form); %a%->render() ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
2121
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType() ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
2222
Assert::match('<?php %a% $_control->getComponent("form"); %a%->{"render$type"}() ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
2323
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType(\'param\') ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
2424
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
2525
Assert::match('<?php %a% $_control->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
26-
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render(); echo $template->striptags(%a%) ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode);
26+
Assert::match('<?php %a% $_control->getComponent("form"); %a%->render(); echo call_user_func($this->filters->striptags, %a%) ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode);

tests/Bridges.Latte/UIMacros.link.2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ link:['login']
8787
<a href="link:['default!#hash',10,20]"></a>
8888
EOD
8989

90-
, $latte->renderToString(<<<EOD
90+
, strtr($latte->renderToString(<<<EOD
9191
{plink Homepage:}
9292
9393
{plink Homepage: }
@@ -118,4 +118,4 @@ EOD
118118
119119
<a n:href="default!#hash 10, 20"></a>
120120
EOD
121-
, $params));
121+
, $params), ['&#039;' => "'"]));

tests/Bridges.Latte/UIMacros.link.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ UIMacros::install($compiler);
1717

1818
// {link ...}
1919
Assert::same('<?php echo $_control->link("p") ?>', $compiler->expandMacro('link', 'p', '')->openingCode);
20-
Assert::same('<?php echo $template->filter($_control->link("p")) ?>', $compiler->expandMacro('link', 'p', 'filter')->openingCode);
20+
Assert::same('<?php echo call_user_func($this->filters->filter, $_control->link("p")) ?>', $compiler->expandMacro('link', 'p', 'filter')->openingCode);
2121
Assert::same('<?php echo $_control->link("p:a") ?>', $compiler->expandMacro('link', 'p:a', '')->openingCode);
2222
Assert::same('<?php echo $_control->link($dest) ?>', $compiler->expandMacro('link', '$dest', '')->openingCode);
2323
Assert::same('<?php echo $_control->link($p:$a) ?>', $compiler->expandMacro('link', '$p:$a', '')->openingCode);
Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,61 @@
11
<?php
2-
// source: %a%
3-
%A%
4-
// prolog Latte\Macros\BlockMacros
5-
//
6-
// block _outer1
7-
//
8-
if (!function_exists($_b->blocks['_outer1'][] = '_%[a-z0-9]+%__outer1')) { function _%[a-z0-9]+%__outer1($_b, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v; $_control->redrawControl('outer1', FALSE)
9-
;%a% foreach (array(1,2,3) as $id) { ?>
10-
<div<?php echo ' id="' . ($_l->dynSnippetId = $_control->getSnippetId("inner-$id")) . '"' ?>>
11-
<?php ob_start() ?> #<?php echo Latte\Runtime\Filters::escapeHtml($id, ENT_NOQUOTES) ?>
12-
13-
<?php $_l->dynSnippets[$_l->dynSnippetId] = ob_get_flush() ?> </div>
14-
<?php $iterations++; } ?>
15-
<?php if (isset($_l->dynSnippets)) return $_l->dynSnippets;
16-
}}
17-
18-
//
19-
// block _outer2
20-
//
21-
if (!function_exists($_b->blocks['_outer2'][] = '_%[a-z0-9]+%__outer2')) { function _%[a-z0-9]+%__outer2($_b, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v; $_control->redrawControl('outer2', FALSE)
22-
;%a% foreach (array(1,2,3) as $id) { ?>
23-
<div<?php echo ' id="' . ($_l->dynSnippetId = $_control->getSnippetId("inner-$id")) . '"' ?>>
24-
<?php ob_start() ?> #<?php echo Latte\Runtime\Filters::escapeHtml($id, ENT_NOQUOTES) ?>
25-
26-
<?php $_l->dynSnippets[$_l->dynSnippetId] = ob_get_flush() ?> </div>
27-
<?php $iterations++; } ?>
28-
<?php if (isset($_l->dynSnippets)) return $_l->dynSnippets;
29-
}}
30-
31-
//
32-
// end of blocks
33-
//
34-
35-
// template extending
2+
// source: %A%
363

37-
$_l->extends = empty($_g->extended) && isset($_control) && $_control instanceof Nette\Application\UI\Presenter ? $_control->findLayoutTemplateFile() : NULL; $_g->extended = TRUE;
4+
class Template%a% extends Latte\Template
5+
{
6+
public $blocks = [
7+
'_outer1' => ['blockOuter1', 'html'],
8+
'_outer2' => ['blockOuter2', 'html'],
9+
];
3810

39-
if ($_l->extends) { ob_start(function () {});}
4011

12+
function render()
13+
{
14+
%A%
4115
// prolog Nette\Bridges\ApplicationLatte\UIMacros
4216

4317
// snippets support
44-
if (empty($_l->extends) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
18+
if (empty($this->local->parentName) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
4519
return Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($_control, $_b, get_defined_vars());
4620
}
4721

48-
//
4922
// main template
50-
//
51-
if ($_l->extends) { ob_end_clean(); return $template->renderChildTemplate($_l->extends, get_defined_vars()); } ?>
52-
<div id="<?php echo $_control->getSnippetId('outer1') ?>"><?php call_user_func(reset($_b->blocks['_outer1']), $_b, $template->getParameters()) ?>
23+
if ($this->tryRenderParent(get_defined_vars())) return ?>
24+
<div id="<?php echo $_control->getSnippetId('outer1') ?>"><?php call_user_func(reset($_b->blocks['_outer1']), $_b, $this->params) ?>
5325
</div>
5426

55-
<div id="<?php echo $_control->getSnippetId('outer2') ?>"><?php call_user_func(reset($_b->blocks['_outer2']), $_b, $template->getParameters()) ?>
27+
<div id="<?php echo $_control->getSnippetId('outer2') ?>"><?php call_user_func(reset($_b->blocks['_outer2']), $_b, $this->params) ?>
5628
</div><?php
57-
%A%
29+
}
30+
31+
32+
function blockOuter1($_b, $_args)
33+
{
34+
extract($_args);
35+
$_control->redrawControl('outer1', FALSE);
36+
37+
%a% foreach (array(1,2,3) as $id) { ?>
38+
<div<?php echo ' id="' . ($this->local->dynSnippetId = $_control->getSnippetId("inner-$id")) . '"' ?>>
39+
<?php ob_start() ?> #<?php echo LFilters::escapeHtmlText($id) ?>
40+
41+
<?php $this->local->dynSnippets[$this->local->dynSnippetId] = ob_get_flush() ?> </div>
42+
<?php $iterations++; } ?>
43+
<?php if (isset($this->local->dynSnippets)) return $this->local->dynSnippets;
44+
}
45+
46+
47+
function blockOuter2($_b, $_args)
48+
{
49+
extract($_args);
50+
$_control->redrawControl('outer2', FALSE);
51+
52+
%a% foreach (array(1,2,3) as $id) { ?>
53+
<div<?php echo ' id="' . ($this->local->dynSnippetId = $_control->getSnippetId("inner-$id")) . '"' ?>>
54+
<?php ob_start() ?> #<?php echo LFilters::escapeHtmlText($id) ?>
55+
56+
<?php $this->local->dynSnippets[$this->local->dynSnippetId] = ob_get_flush() ?> </div>
57+
<?php $iterations++; } ?>
58+
<?php if (isset($this->local->dynSnippets)) return $this->local->dynSnippets;
59+
}
60+
61+
}
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
<?php
2-
// source: %a%
3-
%A%
4-
// prolog Latte\Macros\BlockMacros
5-
//
6-
// block _outer
7-
//
8-
if (!function_exists($_b->blocks['_outer'][] = '_%[a-z0-9]+%__outer')) { function _%[a-z0-9]+%__outer($_b, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v; $_control->redrawControl('outer', FALSE)
9-
;%a% foreach (array(1,2,3) as $id) { ?>
10-
<div id="<?php echo $_l->dynSnippetId = $_control->getSnippetId("inner-$id") ?>
11-
"><?php ob_start() ?>
12-
13-
#<?php echo Latte\Runtime\Filters::escapeHtml($id, ENT_NOQUOTES) ?>
14-
15-
<?php $_l->dynSnippets[$_l->dynSnippetId] = ob_get_flush() ?>
16-
</div>
17-
<?php $iterations++; } ?>
18-
<?php if (isset($_l->dynSnippets)) return $_l->dynSnippets;
19-
}}
20-
21-
//
22-
// end of blocks
23-
//
24-
25-
// template extending
2+
// source: %A%
263

27-
$_l->extends = empty($_g->extended) && isset($_control) && $_control instanceof Nette\Application\UI\Presenter ? $_control->findLayoutTemplateFile() : NULL; $_g->extended = TRUE;
4+
class Template%a% extends Latte\Template
5+
{
6+
public $blocks = [
7+
'_outer' => ['blockOuter', 'html'],
8+
];
289

29-
if ($_l->extends) { ob_start(function () {});}
3010

11+
function render()
12+
{
13+
%A%
3114
// prolog Nette\Bridges\ApplicationLatte\UIMacros
3215

3316
// snippets support
34-
if (empty($_l->extends) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
17+
if (empty($this->local->parentName) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
3518
return Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($_control, $_b, get_defined_vars());
3619
}
3720

38-
//
3921
// main template
40-
//
41-
if ($_l->extends) { ob_end_clean(); return $template->renderChildTemplate($_l->extends, get_defined_vars()); } ?>
42-
<div id="<?php echo $_control->getSnippetId('outer') ?>"><?php call_user_func(reset($_b->blocks['_outer']), $_b, $template->getParameters()) ?>
22+
if ($this->tryRenderParent(get_defined_vars())) return ?>
23+
<div id="<?php echo $_control->getSnippetId('outer') ?>"><?php call_user_func(reset($_b->blocks['_outer']), $_b, $this->params) ?>
4324
</div><?php
44-
%A%
25+
}
26+
27+
28+
function blockOuter($_b, $_args)
29+
{
30+
extract($_args);
31+
$_control->redrawControl('outer', FALSE);
32+
33+
%a% foreach (array(1,2,3) as $id) { ?>
34+
<div id="<?php echo $this->local->dynSnippetId = $_control->getSnippetId("inner-$id") ?>
35+
"><?php ob_start() ?>
36+
37+
#<?php echo LFilters::escapeHtmlText($id) ?>
38+
39+
<?php $this->local->dynSnippets[$this->local->dynSnippetId] = ob_get_flush() ?>
40+
</div>
41+
<?php $iterations++; } ?>
42+
<?php if (isset($this->local->dynSnippets)) return $this->local->dynSnippets;
43+
}
44+
45+
}
Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,64 @@
11
<?php
2-
// source: %a%
3-
%A%
4-
// prolog Latte\Macros\BlockMacros
5-
//
6-
// block _outer
7-
//
8-
if (!function_exists($_b->blocks['_outer'][] = '_%[a-z0-9]+%__outer')) { function _%[a-z0-9]+%__outer($_b, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v; $_control->redrawControl('outer', FALSE)
9-
?> <p>Outer</p>
10-
<?php
11-
}}
12-
13-
//
14-
// block _inner
15-
//
16-
if (!function_exists($_b->blocks['_inner'][] = '_%[a-z0-9]+%__inner')) { function _%[a-z0-9]+%__inner($_b, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v; $_control->redrawControl('inner', FALSE)
17-
?> <p>Inner</p>
18-
<?php
19-
}}
20-
21-
//
22-
// block _gallery
23-
//
24-
if (!function_exists($_b->blocks['_gallery'][] = '_%[a-z0-9]+%__gallery')) { function _%[a-z0-9]+%__gallery($_b, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v; $_control->redrawControl('gallery', FALSE)
25-
;
26-
}}
2+
// source: %A%
273

28-
//
29-
// end of blocks
30-
//
4+
class Template%a% extends Latte\Template
5+
{
6+
public $blocks = [
7+
'_outer' => ['blockOuter', 'html'],
8+
'_inner' => ['blockInner', 'html'],
9+
'_gallery' => ['blockGallery', 'html'],
10+
];
3111

32-
// template extending
33-
34-
$_l->extends = empty($_g->extended) && isset($_control) && $_control instanceof Nette\Application\UI\Presenter ? $_control->findLayoutTemplateFile() : NULL; $_g->extended = TRUE;
35-
36-
if ($_l->extends) { ob_start(function () {});}
3712

13+
function render()
14+
{
15+
%A%
3816
// prolog Nette\Bridges\ApplicationLatte\UIMacros
3917

4018
// snippets support
41-
if (empty($_l->extends) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
19+
if (empty($this->local->parentName) && !empty($_control->snippetMode) && empty($_g->includingBlock)) {
4220
return Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($_control, $_b, get_defined_vars());
4321
}
4422

45-
//
4623
// main template
47-
//
4824
?> <div class="test"<?php echo ' id="' . $_control->getSnippetId('outer') . '"' ?>>
49-
<?php if ($_l->extends) { ob_end_clean(); return $template->renderChildTemplate($_l->extends, get_defined_vars()); }
50-
call_user_func(reset($_b->blocks['_outer']), $_b, $template->getParameters()) ?>
25+
<?php if ($this->tryRenderParent(get_defined_vars())) return;call_user_func(reset($_b->blocks['_outer']), $_b, $this->params) ?>
5126
</div>
5227

5328
<div class="test"<?php echo ' id="' . $_control->getSnippetId('inner') . '"' ?>>
54-
<?php call_user_func(reset($_b->blocks['_inner']), $_b, $template->getParameters()) ?> </div>
29+
<?php call_user_func(reset($_b->blocks['_inner']), $_b, $this->params) ?> </div>
5530

56-
<div class="<?php echo Latte\Runtime\Filters::escapeHtml('class', ENT_COMPAT) ?>
57-
"<?php echo ' id="' . $_control->getSnippetId('gallery') . '"' ?>><?php call_user_func(reset($_b->blocks['_gallery']), $_b, $template->getParameters()) ?>
31+
<div class="<?php echo LFilters::escapeHtmlAttr('class') ?>"<?php echo ' id="' . $_control->getSnippetId('gallery') . '"' ?>
32+
><?php call_user_func(reset($_b->blocks['_gallery']), $_b, $this->params) ?>
5833
</div>
5934
<?php
60-
%A%
35+
}
36+
37+
38+
function blockOuter($_b, $_args)
39+
{
40+
extract($_args);
41+
$_control->redrawControl('outer', FALSE)
42+
?> <p>Outer</p>
43+
<?php
44+
}
45+
46+
47+
function blockInner($_b, $_args)
48+
{
49+
extract($_args);
50+
$_control->redrawControl('inner', FALSE)
51+
?> <p>Inner</p>
52+
<?php
53+
}
54+
55+
56+
function blockGallery($_b, $_args)
57+
{
58+
extract($_args);
59+
$_control->redrawControl('gallery', FALSE);
60+
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)