Skip to content

Fix v:try viewhelper #1942

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

Open
wants to merge 3 commits into
base: development
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
12 changes: 9 additions & 3 deletions Classes/ViewHelpers/TryViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
* <!-- assume that the variable {badJson} contains the string "DontDecodeMe"
* which if course is invalid JSON and cannot be decoded. The default
* behavior is to simply output a simple "cannot decode" string. -->
* <v:variable.set name="decodedBadJson" value="{badJson -> v:format.json.decode()}" />
* <f:then>
* <v:variable.set name="decodedBadJson" value="{badJson -> v:format.json.decode()}" />
* </f:then>
* Displayed only if the JSON decode worked. Much more code and many more
* ViewHelpers can go here. Now, imagine that this block spans so much code
* that potentially there could come an Exception from many additional places
Expand Down Expand Up @@ -103,7 +105,11 @@ public static function renderStatic(
RenderingContextInterface $renderingContext
) {
try {
$content = $arguments['__then']();
if (isset($arguments['__then'])) {
$content = $arguments['__then']();
} else {
$content = '';
}
} catch (\Exception $error) {
$variableProvider = $renderingContext->getVariableProvider();
if (isset($arguments['__else'])) {
Expand All @@ -123,7 +129,7 @@ public static function renderStatic(
public function render()
{
try {
$content = $this->renderChildren();
$content = $this->renderThenChild();
} catch (\Exception $error) {
$this->renderingContext->getVariableProvider()->add('exception', $error);
$content = $this->renderElseChild();
Expand Down
4 changes: 3 additions & 1 deletion Documentation/ViewHelpers/Try.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ from this and imagine that a plain Exception happens on errors.
<!-- assume that the variable {badJson} contains the string "DontDecodeMe"
which if course is invalid JSON and cannot be decoded. The default
behavior is to simply output a simple "cannot decode" string. -->
<v:variable.set name="decodedBadJson" value="{badJson -> v:format.json.decode()}" />
<f:then>
<v:variable.set name="decodedBadJson" value="{badJson -> v:format.json.decode()}" />
</f:then>
Displayed only if the JSON decode worked. Much more code and many more
ViewHelpers can go here. Now, imagine that this block spans so much code
that potentially there could come an Exception from many additional places
Expand Down
Loading