Skip to content

Commit

Permalink
Merge pull request #28 from cakephp/revert-21
Browse files Browse the repository at this point in the history
View instance variable
  • Loading branch information
ADmad committed Apr 3, 2020
2 parents 5c0fb9f + 9fd9cf0 commit 3f5b104
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Twig/Node/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function compile(Compiler $compiler)
} else {
$compiler->raw('echo ');
}
$compiler->raw('$context[\'this\']->cell(');
$compiler->raw('$context[\'_view\']->cell(');
$compiler->subcompile($this->getNode('name'));

$data = $this->getNode('data');
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/Node/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function compile(Compiler $compiler)
{
$compiler->addDebugInfo($this);

$compiler->raw('echo $context[\'this\']->element(');
$compiler->raw('echo $context[\'_view\']->element(');
$compiler->subcompile($this->getNode('name'));

$data = $this->getNode('data');
Expand Down
22 changes: 10 additions & 12 deletions src/View/TwigView.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class TwigView extends View
* - `markdown` - Config for MarkdownExtension. Array must contain `engine` key which is
* an instance of Twig\Extra\Markdown\MarkdownInterface,
* see https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html
* - 'viewVar' - Twig template variable name to access View. Defaults to `this`.
*
* @var array
*/
Expand All @@ -67,7 +66,6 @@ class TwigView extends View
'markdown' => [
'engine' => null,
],
'viewVar' => 'this',
];

/**
Expand Down Expand Up @@ -103,7 +101,8 @@ public function initialize(): void
{
parent::initialize();

$this->twig = new Environment($this->createLoader(), $this->createEnvironmentConfig());
$this->twig = $this->createEnvironment();

$this->initializeTokenParser();
$this->initializeExtensions();

Expand Down Expand Up @@ -143,11 +142,11 @@ protected function createLoader(): LoaderInterface
}

/**
* Creates the Twig Environment configuration.
* Creates the Twig Environment.
*
* @return array
* @return \Twig\Environment
*/
protected function createEnvironmentConfig(): array
protected function createEnvironment(): Environment
{
$debug = Configure::read('debug', false);

Expand All @@ -161,7 +160,10 @@ protected function createEnvironmentConfig(): array
$config['cache'] = CACHE . 'twigView' . DS;
}

return $config;
$env = new Environment($this->createLoader(), $config);
$env->addGlobal('_view', $this);

return $env;
}

/**
Expand Down Expand Up @@ -258,13 +260,9 @@ protected function initializeProfiler(): void
*/
protected function _render(string $templateFile, array $data = []): string
{
$viewVar = $this->getConfig('viewVar');
$data = array_merge(
empty($data) ? $this->viewVars : $data,
iterator_to_array($this->helpers()->getIterator()),
[
$viewVar => $this,
]
iterator_to_array($this->helpers()->getIterator())
);

return $this->getTwig()->load($templateFile)->render($data);
Expand Down
12 changes: 1 addition & 11 deletions tests/TestCase/View/TwigViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testRenderLayoutWithElements()
{
$output = $this->view->render('Blog/index');

$this->assertSame('blog_entry', $output);
$this->assertSame("blog_entry", $output);
}

/**
Expand All @@ -88,16 +88,6 @@ public function testRenderLayoutWithViewBlockAssignment()
$this->assertSame("main content\nextra content", $output);
}

public function testCustomViewVariable()
{
$view = new AppView(null, null, null, ['viewVar' => 'myView']);

$view->assign('title', 'my title');
$output = $view->render('custom_variable', false);

$this->assertSame('my title', $output);
}

/**
* Tests a twig file that throws internal exception throw a Twig exception with message.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/templates/Blog/with_extra_block.twig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
main content
{% do this.assign('block', 'extra content') %}
{% do _view.assign('block', 'extra content') %}
1 change: 0 additions & 1 deletion tests/test_app/templates/custom_variable.twig

This file was deleted.

2 changes: 1 addition & 1 deletion tests/test_app/templates/layout/default.twig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{ this.fetch('content')|raw }}
{{ _view.fetch('content')|raw }}
6 changes: 3 additions & 3 deletions tests/test_app/templates/layout/with_extra_block.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ this.fetch('content')|raw }}
{{ _view.fetch('content')|raw }}

{%- if this.exists('block') %}
{{ this.fetch('block')|raw }}
{%- if _view.exists('block') %}
{{ _view.fetch('block')|raw }}
{%- endif -%}

0 comments on commit 3f5b104

Please sign in to comment.