Skip to content

Commit 225e8dd

Browse files
It should be possible to set the template from the action of the cell
1 parent 7cb7c1a commit 225e8dd

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

src/View/Cell.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ public function render($template = null)
176176
}
177177

178178
$render = function () use ($template) {
179+
try {
180+
$reflect = new ReflectionMethod($this, $this->action);
181+
$reflect->invokeArgs($this, $this->args);
182+
} catch (ReflectionException $e) {
183+
throw new BadMethodCallException(sprintf(
184+
'Class %s does not have a "%s" method.',
185+
get_class($this),
186+
$this->action
187+
));
188+
}
189+
179190
if ($template !== null &&
180191
strpos($template, '/') === false &&
181192
strpos($template, '.') === false
@@ -194,17 +205,6 @@ public function render($template = null)
194205
$name = substr($className, 0, -4);
195206
$builder->templatePath('Cell' . DS . $name);
196207

197-
try {
198-
$reflect = new ReflectionMethod($this, $this->action);
199-
$reflect->invokeArgs($this, $this->args);
200-
} catch (ReflectionException $e) {
201-
throw new BadMethodCallException(sprintf(
202-
'Class %s does not have a "%s" method.',
203-
get_class($this),
204-
$this->action
205-
));
206-
}
207-
208208
$this->View = $this->createView();
209209
try {
210210
return $this->View->render($template);

tests/TestCase/View/CellTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ public function testDefaultCellAction()
132132
$this->assertEquals('display', $pluginCell->template);
133133
}
134134

135+
/**
136+
* Tests that cell action setting the template renders the correct template
137+
*
138+
* @return void
139+
*/
140+
public function testSettingCellTemplateFromAction()
141+
{
142+
$appCell = $this->View->cell('Articles::customTemplate');
143+
144+
$this->assertContains('This is the alternate template', "{$appCell}");
145+
$this->assertEquals('alternate_teaser_list', $appCell->template);
146+
147+
$appCell = $this->View->cell('Articles::customTemplate');
148+
149+
$this->assertContains('This is the alternate template', $appCell->render());
150+
$this->assertEquals('alternate_teaser_list', $appCell->template);
151+
}
152+
135153
/**
136154
* Tests manual render() invocation.
137155
*
@@ -361,4 +379,28 @@ public function testCachedRenderArrayConfig()
361379
$this->assertEquals("dummy\n", $result);
362380
Cache::drop('cell');
363381
}
382+
383+
/**
384+
* Test cached render.
385+
*
386+
* @return void
387+
*/
388+
public function testCachedRenderSimpleCustomTemplate()
389+
{
390+
$mock = $this->getMock('Cake\Cache\CacheEngine');
391+
$mock->method('init')
392+
->will($this->returnValue(true));
393+
$mock->method('read')
394+
->will($this->returnValue(false));
395+
$mock->expects($this->once())
396+
->method('write')
397+
->with('cell_test_app_view_cell_articles_cell_customTemplate', "<h1>This is the alternate template</h1>\n");
398+
Cache::config('default', $mock);
399+
400+
$cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
401+
$result = $cell->render();
402+
$this->assertContains('This is the alternate template', $result);
403+
404+
Cache::drop('default');
405+
}
364406
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>This is the alternate template</h1>

tests/test_app/TestApp/View/Cell/ArticlesCell.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function teaserList()
5151
]);
5252
}
5353

54+
/**
55+
* Renders articles in teaser view mode.
56+
*
57+
* @return void
58+
*/
59+
public function customTemplate()
60+
{
61+
$this->template = 'alternate_teaser_list';
62+
$this->set('articles', [
63+
['title' => 'Lorem ipsum', 'body' => 'dolorem sit amet'],
64+
['title' => 'Usectetur adipiscing eli', 'body' => 'tortor, in tincidunt sem dictum vel'],
65+
['title' => 'Topis semper blandit eu non', 'body' => 'alvinar diam convallis non. Nullam pu'],
66+
['title' => 'Suspendisse gravida neque', 'body' => 'pellentesque sed scelerisque libero'],
67+
]);
68+
}
69+
5470
/**
5571
* Simple echo.
5672
*

0 commit comments

Comments
 (0)