Commit 3194169a authored by Frans Saris's avatar Frans Saris Committed by Benni Mack
Browse files

[FEATURE] Introduce ImplodeViewHelper

To be able to glue an array with classnames together in the FluidContent
rendering a ImplodeViewHelper is added.

Resolves: #68538
Releases: master
Change-Id: I85291cdc93374f99447a97da7f739ad8364b3644
Reviewed-on: http://review.typo3.org/41960

Reviewed-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters's avatarWouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 541c07bb
==============================================
Feature: #68538 - Introduced ImplodeViewHelper
==============================================
Description
===========
To be able to glue a array of strings together (f.i. to build a class attribute value) the ``f:format.implode`` ViewHelper is added.
ViewHelper parameters:
- array $values: array of elements to join
- string $glue: String used as glue between elements (default: space)
- bool $excludeEmptyValues: Remove empty elements (default: TRUE)
Examples
========
Some examples of using ``fe:format.implode``:
.. code-block:: html
<fe:format.implode values="{0: 'className-1', 1: 'className-2'}" />
Output: ``className-1 className-2``
.. code-block:: html
<div class="{fe:format.implode(values:'{0:\'className-1\', 1:\'className-2\'}')}">Foo</div>
Output: ``<div class="className-1 className-2">Foo</div>``
\ No newline at end of file
<?php
namespace TYPO3\CMS\Frontend\ViewHelpers\Format;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
/**
* A view helper which joins array elements with a glue string.
*
* = Examples =
*
* <code>
* class="<f:format.implode values="{0: 'className-1', 1: 'className-2'}" />"
* </code>
*
* <output>
* class="className-1 className-2"
* </output>
*
* <code>
* class="{f:format.implode(values:'{0:\'className-1\', 1:\'className-2\'}')}"
* </code>
*
* <output>
* class="className-1 className-2"
* </output>
*
* <code>
* class="<f:format.implode values="{0: 'prefix', 1: 'value1', 2: 'value2'}" glue="-" />"
* </code>
*
* <output>
* class="prefix-value1-value2"
* </output>
*/
class ImplodeViewHelper extends AbstractViewHelper implements CompilableInterface {
/**
* Render the view helper
*
* @param array $values array of elements to join
* @param string $glue String used as glue between elements
* @param bool $excludeEmptyValues Remove empty elements from $values
* @return string
*/
public function render(array $values, $glue = ', ', $excludeEmptyValues = TRUE) {
return self::renderStatic(
array(
'values' => $values,
'glue' => $glue,
'excludeEmptyValues' => $excludeEmptyValues
),
$this->buildRenderChildrenClosure(),
$this->renderingContext
);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
$values = (array)$arguments['values'];
if (empty($values)) {
$values =(array)$renderChildrenClosure();
}
$glue = $arguments['glue'];
$excludeEmptyValues = (bool)$arguments['excludeEmptyValues'];
$string = '';
if ($excludeEmptyValues) {
$values = array_filter($values);
}
if (!empty($values)) {
$string = implode($glue, $values);
}
return $string;
}
}
\ No newline at end of file
<?php
namespace TYPO3\CMS\Frontend\Tests\Functional\ViewHelpers\Format;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
use TYPO3\CMS\Frontend\ViewHelpers\Format\ImplodeViewHelper;
/**
* Class ImplodeViewHelperTest
*/
class ImplodeViewHelperTest extends ViewHelperBaseTestcase {
/**
* @var ImplodeViewHelper|\PHPUnit_Framework_MockObject_MockObject
*/
protected $subject;
protected function setUp() {
parent::setUp();
$this->subject = $this->getMock(ImplodeViewHelper::class, array('renderChildren'));
$this->injectDependenciesIntoViewHelper($this->subject);
}
/**
* @return array
*/
public function implodeDataProvider() {
return [
'Join elements with space' => [
['class-a', 'class-b', 'class-c'],
' ',
TRUE,
'class-a class-b class-c'
],
'Join elements with space excluding empty values' => [
['class-a', 'class-b', '', 'class-d'],
' ',
TRUE,
'class-a class-b class-d'
],
'Join elements with space including empty values' => [
['class-a', 'class-b', '', 'class-d'],
' ',
FALSE,
'class-a class-b class-d'
],
'Join elements with /' => [
['class-a', 'class-b', 'class-c'],
'/',
TRUE,
'class-a/class-b/class-c'
],
'Join elements with / excluding empty values' => [
['class-a', 'class-b', '', 'class-d'],
'/',
TRUE,
'class-a/class-b/class-d'
],
'Join elements with / including empty values' => [
['class-a', 'class-b', '', 'class-d'],
'/',
FALSE,
'class-a/class-b//class-d'
],
];
}
/**
* @test
* @dataProvider implodeDataProvider
*/
public function ImplodeViewHelperOutputTest($values, $glue, $excludeEmptyValues, $expectedResult) {
$actualResult = $this->subject->render($values, $glue, $excludeEmptyValues);
$this->assertEquals($expectedResult, $actualResult);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment