Skip to content

Commit

Permalink
Share variables shared via View::share() with Twig templates (#969)
Browse files Browse the repository at this point in the history
Fixes #630

This allows global variables populated by the View::share('name', 'value') method to be used in Twig templates. The values will only be populated if they have not been specified as a global previously, to prevent overriding system globals such as this.page. It will also allow the pages themselves to still override the values if need be.
  • Loading branch information
bennothommo authored Sep 7, 2023
1 parent 0aaefeb commit 090562a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
13 changes: 13 additions & 0 deletions modules/cms/classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ public function runPage($page, $useAjax = true)
'session' => App::make('session'),
]);

/*
* Add global vars defined by View::share() into Twig, only if they have yet to be specified.
*/
$globalVars = ViewHelper::getGlobalVars();
if (!empty($globalVars)) {
$existingGlobals = array_keys($this->getTwig()->getGlobals());
foreach ($globalVars as $key => $value) {
if (!in_array($key, $existingGlobals)) {
$this->getTwig()->addGlobal($key, $value);
}
}
}

/*
* Check for the presence of validation errors in the session.
*/
Expand Down
2 changes: 2 additions & 0 deletions modules/cms/tests/classes/CmsObjectQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function testLists()
"no-soft-component-class",
"optional-full-php-tags",
"optional-short-php-tags",
"shared-variable",
"shared-variable-override",
"throw-php",
"with-component",
"with-components",
Expand Down
69 changes: 68 additions & 1 deletion modules/cms/tests/classes/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Cms\Tests\Classes;

use Cms;
use Request;
use System\Tests\Bootstrap\TestCase;
use Cms\Classes\Theme;
use Cms\Classes\Controller;
use Request;
use System\Helpers\View;
use Winter\Storm\Halcyon\Model;
use Winter\Storm\Support\Facades\Config;

Expand All @@ -25,6 +26,8 @@ public function setUp(): void
Model::clearBootedModels();
Model::flushEventListeners();

View::clearVarCache();

include_once base_path() . '/modules/system/tests/fixtures/plugins/winter/tester/components/Archive.php';
include_once base_path() . '/modules/system/tests/fixtures/plugins/winter/tester/components/Post.php';
include_once base_path() . '/modules/system/tests/fixtures/plugins/winter/tester/components/MainMenu.php';
Expand Down Expand Up @@ -682,4 +685,68 @@ public function testMacro()
$response
);
}

public function testSharedVariable()
{
$this->app['view']->share('winterStatus', 'Is Awesome');

$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/shared-variable')->getContent();

$this->assertStringContainsString(
'<p>Winter Is Awesome</p>',
$response
);

$this->assertStringContainsString(
'<p>/shared-variable</p>',
$response
);
}

public function testSharedVariableCannotOverrideSystemGlobals()
{
$this->app['view']->share('winterStatus', 'Is Awesome');

// This override should not apply and change the page URL in the fixture template
$this->app['view']->share('this', [
'page' => [
'url' => '/overriden',
],
]);

$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/shared-variable')->getContent();

$this->assertStringContainsString(
'<p>Winter Is Awesome</p>',
$response
);

$this->assertStringContainsString(
'<p>/shared-variable</p>',
$response
);
}

public function testSharedVariableCanBeOverriddenLocally()
{
$this->app['view']->share('winterStatus', 'Is Awesome');

$theme = Theme::load('test');
$controller = new Controller($theme);
$response = $controller->run('/shared-variable-override')->getContent();

$this->assertStringContainsString(
'<p>Winter Is Coming</p>',
$response
);

$this->assertStringContainsString(
'<p>/shared-variable-override</p>',
$response
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
url = '/shared-variable-override'
==
<?php
function onStart() {
$this['winterStatus'] = 'Is Coming';
}
==
<p>Winter {{ winterStatus }}</p>

<p>{{ this.page.url }}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
url = '/shared-variable'
==
<p>Winter {{ winterStatus }}</p>

<p>{{ this.page.url }}</p>
10 changes: 10 additions & 0 deletions modules/system/helpers/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ public static function getGlobalVars()

return static::$globalVarCache = $vars;
}

/**
* Clears the static cache for global variables.
*
* @return void
*/
public static function clearVarCache()
{
static::$globalVarCache = null;
}
}

0 comments on commit 090562a

Please sign in to comment.