Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Scoped variables #1011

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/ViewHelpers/ScopeViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
* Declares a new variable scope which can be used to add locally scoped variables.
*
* Takes a "variables"-Parameter which is an associative array that defines the variables
* that are initially available within this scope.
*
* These variables are only declared inside the ``<f:scope>...</f:scope>`` tag.
*
* After the closing tag, all initially and locally declared variables are removed respectively restored again.
*
* Examples
* ========
*
* ::
* <f:variable name="foo" value="World" />
* <f:scope variables="{foo: 'Fluid', bar: 'Lorem'}">
* <f:variable name="baz" scope="local" value="Ipsum" />
* {bar} {baz} {foo}!
* </f:scope>
* Hello {foo}!
*
* Output::
*
* Lorem Ipsum Fluid!
* Hello World!
*
* After the scope ``{foo}`` is restored, ``{bar}`` and ``{baz}`` is removed.
*
* @see \TYPO3Fluid\Fluid\ViewHelpers\VariableViewHelper
* @api
*/
final class ScopeViewHelper extends AbstractViewHelper
{
/**
* @var bool
*/
protected $escapeOutput = false;

public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('variables', 'array', 'Array of variables that will be initially declared within this scope', false, []);
}

/**
* @return mixed
*/
public function render(): mixed
{
$globalVariableProvider = $this->renderingContext->getVariableProvider();
$localVariableProvider = new StandardVariableProvider($this->arguments['variables']);
$scopedVariableProvider = new ScopedVariableProvider($globalVariableProvider, $localVariableProvider);
$this->renderingContext->setVariableProvider($scopedVariableProvider);
$output = $this->renderChildren();
$this->renderingContext->setVariableProvider($globalVariableProvider);
return $output;
}
}
8 changes: 7 additions & 1 deletion src/ViewHelpers/VariableViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace TYPO3Fluid\Fluid\ViewHelpers;

use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
Expand Down Expand Up @@ -38,12 +39,17 @@ public function initializeArguments()
{
$this->registerArgument('value', 'mixed', 'Value to assign. If not in arguments then taken from tag content');
$this->registerArgument('name', 'string', 'Name of variable to create', true);
$this->registerArgument('scope', 'string', 'The scope of this variable', defaultValue: 'global');
}

public function render()
{
$value = $this->renderChildren();
$this->renderingContext->getVariableProvider()->add($this->arguments['name'], $value);
$variableProvider = $this->renderingContext->getVariableProvider();
if ($this->arguments['scope'] === 'local' && $variableProvider instanceof ScopedVariableProvider) {
$variableProvider = $variableProvider->getLocalVariableProvider();
}
$variableProvider->add($this->arguments['name'], $value);
}

/**
Expand Down