Skip to content

Commit

Permalink
Use Twig function to call CakePHP helpers.
Browse files Browse the repository at this point in the history
This avoids having to preload helpers as with current usage and
also avoids having to add |raw at the end of calls to prevent escaping.
  • Loading branch information
ADmad committed Apr 2, 2020
1 parent 6a4c73f commit 799488a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Twig/Extension/ViewExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* 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 Cake\TwigView\Twig\Extension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Class ViewExtension.
*/
class ViewExtension extends AbstractExtension
{
/**
* Get declared functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions(): array
{
return [
new TwigFunction(
'helper_*',
function ($context, $name, array $args = []) {
[$helper, $method] = explode('_', $name, 2);

return $context['this']->{$helper}->{$method}(...$args);
},
['needs_context' => true, 'is_variadic' => true, 'is_safe' => ['all']]
),
];
}

/**
* Get extension name.
*
* @return string
*/
public function getName(): string
{
return 'twigview-view';
}
}
1 change: 1 addition & 0 deletions src/View/TwigView.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ protected function initializeExtensions(): void
$this->twig->addExtension(new Extension\StringsExtension());
$this->twig->addExtension(new Extension\TimeExtension());
$this->twig->addExtension(new Extension\UtilsExtension());
$this->twig->addExtension(new Extension\ViewExtension());

// Markdown extension
$markdownEngine = $this->getConfig('markdown.engine');
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/View/TwigViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,17 @@ public function testThrowSyntaxError()

$this->view->render('syntaxerror', false);
}

public function testHelperCall()
{
$view = new AppView(null, null, null, [
'viewVars' => ['elementVar' => 'var echoed inside element'],
]);
$view->loadHelper('Text');

$output = $view->render('helper_test', false);

$expected = "var echoed inside element\n<p>I love CakePHP</p>\n";
$this->assertSame($expected, $output);
}
}
5 changes: 5 additions & 0 deletions tests/test_app/src/View/Helper/TestSecondHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function bogus()
{
throw new MissingSomethingException('Something is missing');
}

public function useElement()
{
return $this->_View->element('element_with_var');
}
}
1 change: 1 addition & 0 deletions tests/test_app/templates/element/element_with_var.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ elementVar }}
2 changes: 2 additions & 0 deletions tests/test_app/templates/helper_test.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ helper_TestSecond_useElement() }}
{{ helper_Text_autoParagraph('I love CakePHP') }}

0 comments on commit 799488a

Please sign in to comment.