Skip to content

Commit

Permalink
It is now possible to set the template a cell uses using the ViewBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Feb 4, 2016
1 parent 225e8dd commit 77df8b4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/View/Cell.php
Expand Up @@ -194,7 +194,7 @@ public function render($template = null)
$template = Inflector::underscore($template);
}
if ($template === null) {
$template = $this->template;
$template = $this->viewBuilder()->template() ?: $this->template;
}

$builder = $this->viewBuilder();
Expand Down
46 changes: 45 additions & 1 deletion tests/TestCase/View/CellTest.php
Expand Up @@ -133,7 +133,7 @@ public function testDefaultCellAction()
}

/**
* Tests that cell action setting the template renders the correct template
* Tests that cell action setting the template using the property renders the correct template
*
* @return void
*/
Expand All @@ -143,11 +143,31 @@ public function testSettingCellTemplateFromAction()

$this->assertContains('This is the alternate template', "{$appCell}");
$this->assertEquals('alternate_teaser_list', $appCell->template);
$this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());

$appCell = $this->View->cell('Articles::customTemplate');

$this->assertContains('This is the alternate template', $appCell->render());
$this->assertEquals('alternate_teaser_list', $appCell->template);
$this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
}

/**
* Tests that cell action setting the template using the ViewBuilder renders the correct template
*
* @return void
*/
public function testSettingCellTemplateFromActionViewBuilder()
{
$appCell = $this->View->cell('Articles::customTemplateViewBuilder');

$this->assertContains('This is the alternate template', "{$appCell}");
$this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());

$appCell = $this->View->cell('Articles::customTemplateViewBuilder');

$this->assertContains('This is the alternate template', $appCell->render());
$this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
}

/**
Expand Down Expand Up @@ -403,4 +423,28 @@ public function testCachedRenderSimpleCustomTemplate()

Cache::drop('default');
}

/**
* Test cached render.
*
* @return void
*/
public function testCachedRenderSimpleCustomTemplateViewBuilder()
{
$mock = $this->getMock('Cake\Cache\CacheEngine');
$mock->method('init')
->will($this->returnValue(true));
$mock->method('read')
->will($this->returnValue(false));
$mock->expects($this->once())
->method('write')
->with('cell_test_app_view_cell_articles_cell_customTemplateViewBuilder', "<h1>This is the alternate template</h1>\n");
Cache::config('default', $mock);

$cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]);
$result = $cell->render();
$this->assertContains('This is the alternate template', $result);

Cache::drop('default');
}
}
21 changes: 14 additions & 7 deletions tests/test_app/TestApp/View/Cell/ArticlesCell.php
Expand Up @@ -52,19 +52,26 @@ public function teaserList()
}

/**
* Renders articles in teaser view mode.
* Renders a view using a different template than the action name
* The template is set using the ``Cell::$template`` property
*
* @return void
*/
public function customTemplate()
{
$this->template = 'alternate_teaser_list';
$this->set('articles', [
['title' => 'Lorem ipsum', 'body' => 'dolorem sit amet'],
['title' => 'Usectetur adipiscing eli', 'body' => 'tortor, in tincidunt sem dictum vel'],
['title' => 'Topis semper blandit eu non', 'body' => 'alvinar diam convallis non. Nullam pu'],
['title' => 'Suspendisse gravida neque', 'body' => 'pellentesque sed scelerisque libero'],
]);
}

/**
* Renders a view using a different template than the action name
* The template is set using the ViewBuilder bound to the Cell
*
* @return void
*/
public function customTemplateViewBuilder()
{
$this->template = 'derp';
$this->viewBuilder()->template('alternate_teaser_list');
}

/**
Expand Down

0 comments on commit 77df8b4

Please sign in to comment.