Skip to content

Commit

Permalink
[FEATURE] Allow default content on optional f:render
Browse files Browse the repository at this point in the history
Whenever `f:render` is called and the `optional` flag is set to `TRUE`, rendering a missing section results in an empty output. Instead of rendering this empty output, a new attribute `default` (mixed) is added and can be filled with a fallback-type default value. Alternatively, the tag content can be used to define this default value like so many other content/attribute-flexible ViewHelpers.
  • Loading branch information
NamelessCoder committed Aug 6, 2015
1 parent fbf343b commit cd67f9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/ViewHelpers/RenderViewHelper.php
Expand Up @@ -82,6 +82,7 @@ public function initializeArguments() {
$this->registerArgument('partial', 'string', 'Partial to render, with or without section', FALSE, NULL);
$this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', FALSE, array());
$this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', FALSE, FALSE);
$this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist', FALSE, NULL);
}

/**
Expand All @@ -95,12 +96,22 @@ public function render() {
$partial = $this->arguments['partial'];
$arguments = (array) $this->arguments['arguments'];
$optional = (boolean) $this->arguments['optional'];
$content = '';
if ($partial !== NULL) {
return $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
$content = $this->viewHelperVariableContainer->getView()->renderPartial($partial, $section, $arguments, $optional);
} elseif ($section !== NULL) {
return $this->viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
$content = $this->viewHelperVariableContainer->getView()->renderSection($section, $arguments, $optional);
}
return '';
// Replace empty content with default value. If default is
// not set, NULL is returned and cast to a new, empty string
// outside of this ViewHelper.
if ('' === $content) {
$content = $this->arguments['default'];
if (NULL === $content) {
$content = $this->renderChildren();
}
}
return $content;
}

}
16 changes: 10 additions & 6 deletions tests/Unit/ViewHelpers/RenderViewHelperTest.php
Expand Up @@ -24,6 +24,7 @@ public function testInitializeArgumentsRegistersExpectedArguments() {
$instance->expects($this->at(1))->method('registerArgument')->with('partial', 'string', $this->anything(), FALSE, NULL);
$instance->expects($this->at(2))->method('registerArgument')->with('arguments', 'array', $this->anything(), FALSE, array());
$instance->expects($this->at(3))->method('registerArgument')->with('optional', 'boolean', $this->anything(), FALSE, FALSE);
$instance->expects($this->at(4))->method('registerArgument')->with('default', 'mixed', $this->anything(), FALSE, FALSE);
$instance->initializeArguments();
}

Expand All @@ -39,10 +40,13 @@ public function testRender(array $arguments, $expectedViewMethod) {
} else {
$methods = array('renderPartial', 'renderSection');
}
$instance = new RenderViewHelper();
$instance = $this->getMock('NamelessCoder\\Fluid\\ViewHelpers\\RenderViewHelper', array('renderChildren'));
$instance->expects($this->any())->method('renderChildren')->willReturn(NULL);
$renderingContext = new RenderingContext();
$paths = $this->getMock('NamelessCoder\\Fluid\\View\\TemplatePaths', array('sanitizePath'));
$paths->expects($this->any())->method('sanitizePath')->willReturnArgument(0);
$viewHelperVariableContainer = new ViewHelperVariableContainer();
$view = $this->getMock('NamelessCoder\\Fluid\\View\\TemplateView', $methods, array(), '', FALSE);
$view = $this->getMock('NamelessCoder\\Fluid\\View\\TemplateView', $methods, array($paths, $renderingContext));
$viewHelperVariableContainer->setView($view);
$renderingContext->injectViewHelperVariableContainer($viewHelperVariableContainer);
$instance->setArguments($arguments);
Expand All @@ -57,19 +61,19 @@ public function testRender(array $arguments, $expectedViewMethod) {
public function getRenderTestValues() {
return array(
array(
array('partial' => NULL, 'section' => NULL, 'arguments' => array(), 'optional' => FALSE),
array('partial' => NULL, 'section' => NULL, 'arguments' => array(), 'optional' => FALSE, 'default' => NULL),
NULL
),
array(
array('partial' => 'foo-partial', 'section' => NULL, 'arguments' => array(), 'optional' => FALSE),
array('partial' => 'foo-partial', 'section' => NULL, 'arguments' => array(), 'optional' => FALSE, 'default' => NULL),
'renderPartial'
),
array(
array('partial' => 'foo-partial', 'section' => 'foo-section', 'arguments' => array(), 'optional' => FALSE),
array('partial' => 'foo-partial', 'section' => 'foo-section', 'arguments' => array(), 'optional' => FALSE, 'default' => NULL),
'renderPartial'
),
array(
array('partial' => NULL, 'section' => 'foo-section', 'arguments' => array(), 'optional' => FALSE),
array('partial' => NULL, 'section' => 'foo-section', 'arguments' => array(), 'optional' => FALSE, 'default' => NULL),
'renderSection'
),
);
Expand Down

0 comments on commit cd67f9d

Please sign in to comment.