Skip to content

Commit 5b60e95

Browse files
committed
UIMacros: {control} prints line number, whitespace, compatibility with Latte master
1 parent 48a26df commit 5b60e95

File tree

8 files changed

+66
-42
lines changed

8 files changed

+66
-42
lines changed

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function finalize()
6363
return [
6464
'if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return;',
6565
'',
66-
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
66+
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate()
67+
&& isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
6768
? $this->global->uiControl->findLayoutTemplateFile() : NULL);',
6869
];
6970
}
@@ -98,10 +99,14 @@ public function macroControl(MacroNode $node, PhpWriter $writer)
9899
if (empty($wrap)) {
99100
$param = substr($param, 1, -1); // removes array() or []
100101
}
101-
return ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
102+
return "/* line $node->startLine */ "
103+
. ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
102104
. '$_tmp = $this->global->uiControl->getComponent(' . $name . '); '
103105
. 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(NULL, FALSE); '
104-
. ($node->modifiers === '' ? "\$_tmp->$method($param)" : $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean())"));
106+
. ($node->modifiers === ''
107+
? "\$_tmp->$method($param);"
108+
: $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());")
109+
);
105110
}
106111

107112

@@ -114,7 +119,10 @@ public function macroLink(MacroNode $node, PhpWriter $writer)
114119
{
115120
$node->modifiers = preg_replace('#\|safeurl\s*(?=\||\z)#i', '', $node->modifiers);
116121
return $writer->using($node, $this->getCompiler())
117-
->write('echo %escape(%modify(' . ($node->name === 'plink' ? '$this->global->uiPresenter' : '$this->global->uiControl') . '->link(%node.word, %node.array?)))');
122+
->write('echo %escape(%modify('
123+
. ($node->name === 'plink' ? '$this->global->uiPresenter' : '$this->global->uiControl')
124+
. '->link(%node.word, %node.array?)))'
125+
);
118126
}
119127

120128

@@ -124,7 +132,7 @@ public function macroLink(MacroNode $node, PhpWriter $writer)
124132
public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
125133
{
126134
if ($node->modifiers) {
127-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
135+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
128136
}
129137
return $writer->write($node->args
130138
? 'if ($this->global->uiPresenter->isLinkCurrent(%node.word, %node.array?)) {'

tests/Bridges.Latte/UIMacros.control.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ $compiler = new Latte\Compiler;
1515
UIMacros::install($compiler);
1616

1717
// {control ...}
18-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render() ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
19-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo call_user_func($this->filters->filter, %a%) ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode);
20-
Assert::match('<?php if (is_object($form)) %a% else %a% $this->global->uiControl->getComponent($form); %a%->render() ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
21-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType() ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
22-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->{"render$type"}() ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
23-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(\'param\') ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
24-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
25-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]) ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
26-
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo call_user_func($this->filters->striptags, %a%) ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode);
18+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); ?>', $compiler->expandMacro('control', 'form', '')->openingCode);
19+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo call_user_func($this->filters->filter, %a%); ?>', $compiler->expandMacro('control', 'form', 'filter')->openingCode);
20+
Assert::match('<?php %a% if (is_object($form)) %a% else %a% $this->global->uiControl->getComponent($form); %a%->render(); ?>', $compiler->expandMacro('control', '$form', '')->openingCode);
21+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(); ?>', $compiler->expandMacro('control', 'form:type', '')->openingCode);
22+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->{"render$type"}(); ?>', $compiler->expandMacro('control', 'form:$type', '')->openingCode);
23+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType(\'param\'); ?>', $compiler->expandMacro('control', 'form:type param', '')->openingCode);
24+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]); ?>', $compiler->expandMacro('control', 'form:type param => 123', '')->openingCode);
25+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->renderType([\'param\' => 123]); ?>', $compiler->expandMacro('control', 'form:type, param => 123', '')->openingCode);
26+
Assert::match('<?php %a% $this->global->uiControl->getComponent("form"); %a%->render(); echo call_user_func($this->filters->striptags, %a%); ?>', $compiler->expandMacro('control', 'form', 'striptags')->openingCode);

tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.alt.phtml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class Template%a% extends Latte\Template
3131
function prepare()
3232
{
3333
extract($this->params);
34-
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
34+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate()
35+
&& isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
3536
? $this->global->uiControl->findLayoutTemplateFile() : NULL);
3637
return get_defined_vars();
3738
}
@@ -43,12 +44,15 @@ class Template%a% extends Latte\Template
4344
$this->global->uiControl->redrawControl('outer1', FALSE);
4445

4546
$iterations = 0;
46-
%a%
4747
foreach (array(1,2,3) as $id) {
4848
?> <div<?php echo ' id="' . ($this->global->dynSnippetId = $this->global->uiControl->getSnippetId("inner-$id")) . '"' ?>>
49-
<?php ob_start() ?> #<?php echo LR\Filters::escapeHtml($id) ?>
49+
<?php
50+
ob_start();
51+
?> #<?php echo LR\Filters::escapeHtml($id) /* line 4 */ ?>
5052

51-
<?php $this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush() ?> </div>
53+
<?php
54+
$this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush();
55+
?> </div>
5256
<?php
5357
$iterations++;
5458
}
@@ -64,12 +68,15 @@ class Template%a% extends Latte\Template
6468
$this->global->uiControl->redrawControl('outer2', FALSE);
6569

6670
$iterations = 0;
67-
%a%
6871
foreach (array(1,2,3) as $id) {
6972
?> <div<?php echo ' id="' . ($this->global->dynSnippetId = $this->global->uiControl->getSnippetId("inner-$id")) . '"' ?>>
70-
<?php ob_start() ?> #<?php echo LR\Filters::escapeHtml($id) ?>
73+
<?php
74+
ob_start();
75+
?> #<?php echo LR\Filters::escapeHtml($id) /* line 13 */ ?>
7176

72-
<?php $this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush() ?> </div>
77+
<?php
78+
$this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush();
79+
?> </div>
7380
<?php
7481
$iterations++;
7582
}

tests/Bridges.Latte/expected/UIMacros.dynamicsnippets.phtml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class Template%a% extends Latte\Template
2727
function prepare()
2828
{
2929
extract($this->params);
30-
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
30+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate()
31+
&& isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
3132
? $this->global->uiControl->findLayoutTemplateFile() : NULL);
3233
return get_defined_vars();
3334
}
@@ -39,14 +40,16 @@ class Template%a% extends Latte\Template
3940
$this->global->uiControl->redrawControl('outer', FALSE);
4041

4142
$iterations = 0;
42-
%a%
4343
foreach (array(1,2,3) as $id) {
4444
?> <div id="<?php echo $this->global->dynSnippetId = $this->global->uiControl->getSnippetId("inner-$id") ?>"><?php
45-
ob_start() ?>
45+
ob_start();
46+
?>
4647

47-
#<?php echo LR\Filters::escapeHtml($id) ?>
48+
#<?php echo LR\Filters::escapeHtml($id) /* line 4 */ ?>
4849

49-
<?php $this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush() ?></div><?php
50+
<?php
51+
$this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush();
52+
?></div><?php
5053
$iterations++;
5154
}
5255
?> <?php

tests/Bridges.Latte/expected/UIMacros.isLinkCurrent.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
if ($this->global->uiPresenter->getLastCreatedRequestFlag("current")) {
1111
?>empty<?php
1212
}
13-
?>
13+
?>
1414

1515

1616
<?php

tests/Bridges.Latte/expected/UIMacros.snippet.alt.phtml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Template%a% extends Latte\Template
3030
<div class="test"<?php echo ' id="' . $this->global->uiControl->getSnippetId('inner') . '"' ?>>
3131
<?php
3232
call_user_func(reset($this->blockQueue['_inner']), $this->params);
33-
?> </div>
33+
?> </div>
3434

35-
<div class="<?php echo LR\Filters::escapeHtmlAttr('class') ?>"<?php echo ' id="' . $this->global->uiControl->getSnippetId('gallery') . '"' ?>><?php
35+
<div class="<?php echo LR\Filters::escapeHtmlAttr('class') /* line 9 */ ?>"<?php echo ' id="' . $this->global->uiControl->getSnippetId('gallery') . '"' ?>><?php
3636
call_user_func(reset($this->blockQueue['_gallery']), $this->params) ?>
3737
</div>
3838
<?php
@@ -43,7 +43,8 @@ class Template%a% extends Latte\Template
4343
function prepare()
4444
{
4545
extract($this->params);
46-
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
46+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate()
47+
&& isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
4748
? $this->global->uiControl->findLayoutTemplateFile() : NULL);
4849
return get_defined_vars();
4950
}
@@ -54,7 +55,7 @@ class Template%a% extends Latte\Template
5455
extract($_args);
5556
$this->global->uiControl->redrawControl('outer', FALSE);
5657

57-
?> <p>Outer</p>
58+
?> <p>Outer</p>
5859
<?php
5960
}
6061

@@ -64,7 +65,7 @@ class Template%a% extends Latte\Template
6465
extract($_args);
6566
$this->global->uiControl->redrawControl('inner', FALSE);
6667

67-
?> <p>Inner</p>
68+
?> <p>Inner</p>
6869
<?php
6970
}
7071

tests/Bridges.Latte/expected/UIMacros.snippet.phtml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Template%a% extends Latte\Template
3737
if (true) {
3838
?> Hello World @<?php
3939
}
40-
?>
40+
?>
4141

4242

4343
<h2 id="<?php echo $this->global->uiControl->getSnippetId('title') ?>"><?php call_user_func(reset($this->blockQueue['_title']), $this->params) ?></h2>
@@ -49,7 +49,8 @@ class Template%a% extends Latte\Template
4949
function prepare()
5050
{
5151
extract($this->params);
52-
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate() && isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
52+
$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate()
53+
&& isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter
5354
? $this->global->uiControl->findLayoutTemplateFile() : NULL);
5455
return get_defined_vars();
5556
}
@@ -60,7 +61,7 @@ class Template%a% extends Latte\Template
6061
extract($_args);
6162
$this->global->uiControl->redrawControl('', FALSE);
6263

63-
?>
64+
?>
6465

6566
<?php
6667
}
@@ -71,7 +72,7 @@ class Template%a% extends Latte\Template
7172
extract($_args);
7273
$this->global->uiControl->redrawControl('outer', FALSE);
7374

74-
?>
75+
?>
7576
Outer
7677
<div id="<?php echo $this->global->uiControl->getSnippetId('inner') ?>"><?php call_user_func(reset($this->blockQueue['_inner']), $this->params) ?></div> /Outer
7778
<?php

tests/Bridges.Latte/expected/UIMacros.snippet4.phtml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Template%a% extends Latte\Template
2323
%A%
2424
// main template
2525
call_user_func(reset($this->blockQueue['block1']), get_defined_vars());
26-
?>
26+
?>
2727

2828

2929
<div id="<?php echo $this->global->uiControl->getSnippetId('outer') ?>"><?php call_user_func(reset($this->blockQueue['_outer']), $this->params) ?></div><?php
@@ -47,7 +47,7 @@ class Template%a% extends Latte\Template
4747
extract($_args);
4848
$this->global->uiControl->redrawControl('snippet', FALSE);
4949

50-
?> static
50+
?> static
5151
<?php
5252
}
5353

@@ -57,11 +57,11 @@ class Template%a% extends Latte\Template
5757
extract($_args);
5858
$this->global->uiControl->redrawControl('outer', FALSE);
5959

60-
?>
60+
?>
6161
begin
6262
<?php
6363
call_user_func(reset($this->blockQueue['block2']), get_defined_vars());
64-
?>
64+
?>
6565
end
6666
<?php
6767
if (isset($this->global->dynSnippets)) return $this->global->dynSnippets;
@@ -73,8 +73,12 @@ end
7373
{
7474
extract($_args);
7575
?><div<?php echo ' id="' . ($this->global->dynSnippetId = $this->global->uiControl->getSnippetId("inner-$id")) . '"' ?>>
76-
<?php ob_start() ?> dynamic
77-
<?php $this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush() ?></div>
76+
<?php
77+
ob_start();
78+
?> dynamic
79+
<?php
80+
$this->global->dynSnippets[$this->global->dynSnippetId] = ob_get_flush();
81+
?></div>
7882
<?php
7983

8084
}

0 commit comments

Comments
 (0)