Shared view vars now passed to content blocks and mail templates

This commit is contained in:
Samuel Georges 2016-06-04 07:26:16 +10:00
parent 649e1cfaa7
commit 0111100990
3 changed files with 48 additions and 0 deletions

View File

@ -20,6 +20,7 @@ use Cms\Twig\DebugExtension;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Models\MaintenanceSetting;
use System\Models\RequestLog;
use System\Helpers\View as ViewHelper;
use System\Classes\ErrorHandler;
use System\Classes\CombineAssets;
use System\Twig\Extension as SystemTwigExtension;
@ -940,6 +941,14 @@ class Controller
$fileContent = $content->parsedMarkup;
/*
* Inject global view variables
*/
$globalVars = ViewHelper::getGlobalVars();
if (!empty($globalVars)) {
$parameters = (array) $parameters + $globalVars;
}
/*
* Parse basic template variables
*/

View File

@ -0,0 +1,30 @@
<?php namespace System\Helpers;
use View as ViewFacade;
class View
{
/**
* @var array Cache for global variables.
*/
protected static $globalVarCache;
/**
* Returns shared view variables, this should be used for simple rendering cycles.
* Such as content blocks and mail templates.
*
* @return array
*/
public static function getGlobalVars()
{
if (static::$globalVarCache !== null) {
return static::$globalVarCache;
}
$vars = array_filter(ViewFacade::getShared(), function($var) {
return is_scalar($var) || is_array($var);
});
return static::$globalVarCache = $vars;
}
}

View File

@ -7,6 +7,7 @@ use View;
use Model;
use October\Rain\Mail\MailParser;
use System\Classes\PluginManager;
use System\Helpers\View as ViewHelper;
/**
* Mail template
@ -141,6 +142,14 @@ class MailTemplate extends Model
self::$cache[$code] = $template = self::findOrMakeTemplate($code);
}
/*
* Inject global view variables
*/
$globalVars = ViewHelper::getGlobalVars();
if (!empty($globalVars)) {
$data = (array) $data + $globalVars;
}
/*
* Subject
*/