Skip to content

Commit fbdd49c

Browse files
committed
UIMacros: own handler for {extends}
1 parent c9f5119 commit fbdd49c

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public static function install(Latte\Compiler $compiler)
3636
$me->addMacro('plink', [$me, 'macroLink']);
3737
$me->addMacro('link', [$me, 'macroLink']);
3838
$me->addMacro('ifCurrent', [$me, 'macroIfCurrent'], '}'); // deprecated; use n:class="$presenter->linkCurrent ? ..."
39+
$me->addMacro('extends', [$me, 'macroExtends']);
40+
$me->addMacro('layout', [$me, 'macroExtends']);
41+
}
42+
43+
44+
/**
45+
* Initializes before template parsing.
46+
* @return void
47+
*/
48+
public function initialize()
49+
{
50+
$this->getCompiler()->addMethod('getParentName', '
51+
return !$this->getReferrerTemplate() && $this->params["_control"] instanceof Nette\Application\UI\Presenter
52+
? $this->params["_control"]->findLayoutTemplateFile() : NULL;
53+
');
3954
}
4055

4156

@@ -103,6 +118,18 @@ public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
103118
}
104119

105120

121+
/**
122+
* {extends auto}
123+
*/
124+
public function macroExtends(MacroNode $node, PhpWriter $writer)
125+
{
126+
if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
127+
return FALSE;
128+
}
129+
$this->getCompiler()->addMethod('getParentName', 'return $this->params["_presenter"]->findLayoutTemplateFile();');
130+
}
131+
132+
106133
/** @deprecated */
107134
public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
108135
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use Nette\Bridges\ApplicationLatte\UIMacros;
4+
use Tester\Assert;
5+
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
class MockPresenter extends Nette\Application\UI\Presenter
11+
{
12+
function findLayoutTemplateFile()
13+
{
14+
return 'layout.latte';
15+
}
16+
}
17+
18+
19+
$latte = new Latte\Engine;
20+
$latte->setLoader(new Latte\Loaders\StringLoader);
21+
UIMacros::install($latte->getCompiler());
22+
23+
$template = $latte->createTemplate(
24+
'',
25+
['_control' => new MockPresenter]
26+
);
27+
Assert::same('layout.latte', $template->getParentName());
28+
29+
$template = $latte->createTemplate(
30+
'{block}...{/block}',
31+
['_control' => new MockPresenter]
32+
);
33+
Assert::same('layout.latte', $template->getParentName());
34+
35+
$template = $latte->createTemplate(
36+
'{block name}...{/block}',
37+
['_control' => new MockPresenter]
38+
);
39+
Assert::same('layout.latte', $template->getParentName());
40+
41+
$template = $latte->createTemplate(
42+
'{extends "file.latte"} {block name}...{/block}',
43+
['_control' => new MockPresenter]
44+
);
45+
Assert::same('file.latte', $template->getParentName());
46+
47+
$template = $latte->createTemplate(
48+
'{extends "file.latte"}',
49+
['_control' => new MockPresenter]
50+
);
51+
Assert::same('file.latte', $template->getParentName());
52+
53+
$template = $latte->createTemplate(
54+
'{extends $file} {block name}...{/block}',
55+
['_control' => new MockPresenter, 'file' => 'file.latte']
56+
);
57+
Assert::same('file.latte', $template->getParentName());
58+
59+
$template = $latte->createTemplate(
60+
'{extends none}',
61+
['_control' => new MockPresenter]
62+
);
63+
Assert::null($template->getParentName());
64+
65+
$template = $latte->createTemplate(
66+
'{extends auto}',
67+
['_presenter' => new MockPresenter]
68+
);
69+
Assert::same('layout.latte', $template->getParentName());

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ if ($this->tryRenderParent(get_defined_vars())) return ?>
2525
}
2626

2727

28+
function getParentName()
29+
{
30+
%A%
31+
}
32+
33+
2834
function blockOuter1($_args)
2935
{
3036
extract($_args);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ if ($this->tryRenderParent(get_defined_vars())) return ?>
2121
}
2222

2323

24+
function getParentName()
25+
{
26+
%A%
27+
}
28+
29+
2430
function blockOuter($_args)
2531
{
2632
extract($_args);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueu
3131
}
3232

3333

34+
function getParentName()
35+
{
36+
%A%
37+
}
38+
39+
3440
function blockOuter($_args)
3541
{
3642
extract($_args);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueu
4040
}
4141

4242

43+
function getParentName()
44+
{
45+
%A%
46+
}
47+
48+
4349
function block_%h%($_args)
4450
{
4551
extract($_args);

0 commit comments

Comments
 (0)