Discussion:
[TYPO3-english] Extbase/Fluid extension: viewhelper to return associative array (T3 6.2.4)
Hagen Gebauer
2015-02-20 06:53:19 UTC
Permalink
Hi,

I'd like to have a viewhelper return an associative array like
class ArrayreturnViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
$array['valueOne'] = 'abc';
$array['valueTwo'] = 'def';
return $array;
}

I want to call these values directly, not in a loop. This way did not work:
<namespace:arrayreturn.valueOne /> (TYPO3 will be looking for ViewHelpers/Arrayreturn/ValueOneViewHelper.php)

Is there a way to do it or do I really have to add a ViewHelper for each of the values?

Thanks a lot in advance!

Cheers,
Hagen.
Anja Leichsenring
2015-02-20 07:19:23 UTC
Permalink
Hi Hagen,

create a ViewHelper class, that builds up your array like this:

class MyViewHelper extends AbstractViewHelper {

public function render() {
$returnArray = array('a' => 17, 'b' => 42);
$this->templateVariableContainer->add('returnArray', $returnArray);
$output = $this->renderChildren();
$this->templateVariableContainer->remove('returnArray');
return $output;
}
}

With this, you can do in your FluidTemplate:
<namespace:My>
{returnArray.a} some HTML {returnArray.b}
</namespace:My>

Hope this helps
Anja
Post by Hagen Gebauer
Hi,
I'd like to have a viewhelper return an associative array like
class ArrayreturnViewHelper extends
\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
$array['valueOne'] = 'abc';
$array['valueTwo'] = 'def';
return $array;
}
<namespace:arrayreturn.valueOne /> (TYPO3 will be looking for
ViewHelpers/Arrayreturn/ValueOneViewHelper.php)
Is there a way to do it or do I really have to add a ViewHelper for each of the values?
Thanks a lot in advance!
Cheers,
Hagen.
Hagen Gebauer
2015-02-20 08:40:56 UTC
Permalink
Hi Anja,

thanks a lot for your reply. But somehow it doesn't work although there isn't a parsing error at least :)
calling {returnArray.a} returns an empty string
{namespace:returnArray.a} or {namespace:test.returnArray.a} instead are not being parsed but displayed as is.
(the class is called TestViewHelper that's why I tried to call namespace:test...)

Here is the full class:

class TestViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
*
* @return array
*/
public function render() {
$returnArray = array('a' => 17, 'b' => 42);
$this->templateVariableContainer->add('returnArray', $returnArray);
$output = $this->renderChildren();
$this->templateVariableContainer->remove('returnArray');
return $output;
}
}

Cheers,
Hagen.
Stephan Schuler
2015-02-20 17:45:08 UTC
Permalink
Hey there.

"{returnValue.a}" would be the correct call, since the view helper Anja suggested introduces that exact variable name.

You would need to call it like this
<namespace:test>
<h1>Test: {returnValue.a}</h1>
{returnValue -> f:debug()}
</namespace:test>
That's basically what the forViewHelper does for each iteration, it introduces a new variable which is named according to the string given as "as" argument of the forViewHelper. You skipped that "as" argument and used always "returnValue".

Another idea would be to create that "return $array" view helper you posted in your first mail and use this one inside an "alias" view helper
<f:alias map="{returnValue: '{namespace:test()}'}">
<h1>Test: {returnValue.a}</h1>
{returnValue -> f:debug()}
</f:alias>
Regards,


Stephan Schuler
Web-Entwickler | netlogix Media

Telefon: +49 (911) 539909 - 0
E-Mail: ***@netlogix.de
Web: media.netlogix.de




netlogix GmbH & Co. KG
IT-Services | IT-Training | Media
Neuwieder Straße 10 | 90411 Nürnberg
Telefon: +49 (911) 539909 - 0 | Fax: +49 (911) 539909 - 99
E-Mail: ***@netlogix.de | Web: http://www.netlogix.de

netlogix GmbH & Co. KG ist eingetragen am Amtsgericht Nürnberg (HRA 13338)
Persönlich haftende Gesellschafterin: netlogix Verwaltungs GmbH (HRB 20634)
Umsatzsteuer-Identifikationsnummer: DE 233472254
Geschäftsführer: Stefan Buchta, Matthias Schmidt



- -----Ursprüngliche Nachricht-----
Von: typo3-english-***@lists.typo3.org [mailto:typo3-english-***@lists.typo3.org] Im Auftrag von Hagen Gebauer
Gesendet: Freitag, 20. Februar 2015 09:41
An: typo3-***@lists.typo3.org
Betreff: [TYPO3-english] Re: Extbase/Fluid extension: viewhelper to return associative array (T3 6.2.4)

Hi Anja,

thanks a lot for your reply. But somehow it doesn't work although there isn't a parsing error at least :) calling {returnArray.a} returns an empty string {namespace:returnArray.a} or {namespace:test.returnArray.a} instead are not being parsed but displayed as is.
(the class is called TestViewHelper that's why I tried to call namespace:test...)

Here is the full class:

class TestViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
*
* @return array
*/
public function render() {
$returnArray = array('a' => 17, 'b' => 42);
$this->templateVariableContainer->add('returnArray', $returnArray);
$output = $this->renderChildren();
$this->templateVariableContainer->remove('returnArray');
return $output;
}
}

Cheers,
Hagen.
_______________________________________________
TYPO3-english mailing list
TYPO3-***@lists.typo3.org
http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-english
Hagen Gebauer
2015-02-23 07:38:38 UTC
Permalink
Hi Stephan,

thanks a lot. But also these suggestions would not work. Since the Fluid tags you posted are not being html-encoded and thus not visible in the forum, I post your ideas again:

1st idea -- returned NULL:
<namespace:test>
<h1>Test: {returnValue.a}</h1>
{returnValue -> f:debug()}
</namespace:test>

2nd idea -- threw an exception ("Namespace could not be resolved. This exception should never be thrown!"):
<f:alias map="{returnValue: '{namespace:test()}'}">
<h1>Test: {returnValue.a}</h1>
{returnValue -> f:debug()}
</f:alias>

What does the definition of the namespace do anyway in these examples? How is TYPO3 supposed to know which ViewHelper object to call? As I understand it you usually define a namespace for all ViewHelpers of the extension and not by the name of a specific ViewHelper:
{namespace whateverName = YourVendorName\YourExtension\ViewHelpers}
You then call the ViewHelpers by their name preceded by their common namespace:
<whateverName:yourViewHelper />

Thanks a lot in advance!

Cheers,
Hagen.
Jo Hasenau
2015-02-23 18:46:18 UTC
Permalink
Post by Hagen Gebauer
I'd like to have a viewhelper return an associative array like
class ArrayreturnViewHelper extends
\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
$array['valueOne'] = 'abc';
$array['valueTwo'] = 'def';
return $array;
}
<namespace:arrayreturn.valueOne /> (TYPO3 will be looking for
ViewHelpers/Arrayreturn/ValueOneViewHelper.php)
Is there a way to do it or do I really have to add a ViewHelper for each of the values?
We are using a similar view helper for the THEMES project:

https://github.com/typo3-themes/themes/blob/master/Classes/ViewHelpers/ArrayIndexViewHelper.php

And here is an example of that view helper in themes_gridelements:

https://github.com/typo3-themes/themes_gridelements/blob/master/Resources/Private/Templates/Elements/Carousel.html

HTH

Joey
--
Diversity:
Die Kunst zusammen unabhängig zu denken
The art of thinking independently together.
--
Facebook: https://www.facebook.com/johasenau
Twitter: http://twitter.com/bunnyfield
Xing: http://contact.cybercraft.de
TYPO3 cookbook (2nd edition): http://www.typo3experts.com
Hagen Gebauer
2015-02-24 07:48:42 UTC
Permalink
Hi Joey,

thanks a lot! That's working brilliantly! Sometimes it's that simple :) Here's a short summary for everybody else to read:

The ViewHelper class:

namespace VendorName\ExtensionName\ViewHelpers;
class TestViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @param $arrayIndex string
*
* @return string
*/
public function render($arrayIndex) {
$returnArray = array('a' => 'abcdefg', 'b' => 'hijklmn');
return $returnArray[$arrayIndex];
}
}

calling an array element by its index in the Fluid template:
{namespace abc = VendorName\ExtensionName\ViewHelpers}
{abc:test(arrayIndex: 'b')} (calling by {namespace:viewhelper} and passing the array index)

will return:
hijklmn

Loading...