Skip to content

Commit

Permalink
Added cell() and element() twig functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Apr 3, 2020
1 parent 19d687d commit 293b9c6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/Twig/Extension/ViewExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ class ViewExtension extends AbstractExtension
public function getFunctions(): array
{
return [
new TwigFunction(
'cell',
function ($context, string $name, array $data = [], array $options = []) {
$view = $context['_view'];
$cell = $view->cell($name, $data, $options);

$original = [];
foreach ($view->getDefaultConfig() as $config => $default) {
$original[$config] = $view->getConfig($config, $default);
}
$cell->viewBuilder()->setOptions($options);

return $cell;
},
['needs_context' => true]
),
new TwigFunction(
'element',
function ($context, string $name, array $data = [], array $options = []) {
return $context['_view']->element($name, $data, $options);
},
['needs_context' => true]
),
new TwigFunction(
'helper_*_*',
function ($context, $helper, $method, array $args = []) {
Expand Down
10 changes: 10 additions & 0 deletions src/View/TwigView.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public function initialize(): void
}
}

/**
* Gets the default View config for TwigView.
*
* @return array
*/
public function getDefaultConfig(): array
{
return $this->_defaultConfig;
}

/**
* Get Twig Environment instance.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/View/TwigViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public function testRenderLayoutWithViewBlockAssignment()
$this->assertSame("main content\nextra content", $output);
}

public function testRenderCell()
{
$output = $this->view->render('cell', false);
$this->assertSame('10', $output);
}

/**
* Tests a twig file that throws internal exception throw a Twig exception with message.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/test_app/src/View/Cell/TestCell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* Copyright (c) 2014 Cees-Jan Kiewiet
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

namespace TestApp\View\Cell;

use Cake\View\Cell;

class TestCell extends Cell
{
public function display($number)
{
$this->set('testNumber', $number);
}
}
2 changes: 1 addition & 1 deletion tests/test_app/templates/Blog/index.twig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% element 'blog_entry' %}
{{ element('blog_entry') }}
1 change: 1 addition & 0 deletions tests/test_app/templates/cell.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ cell('Test', data=[10]) }}
1 change: 1 addition & 0 deletions tests/test_app/templates/cell/Test/display.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ testNumber }}

0 comments on commit 293b9c6

Please sign in to comment.