5

I've got custom fluid ViewHelper that returns an array, and I'd like to access a value of this array directly in one command.

Currently I'm using two commands:

{vendor:helper() -> v:variable.set(name: 'data')}
Value of foo: {data.foo}

Is there a way to do this in a single command? v:variable.get does not seem suited for this task.

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • 1
    I don't know a pure fluid solution. An alternative: You could give the view helper give an additional parameter that takes the property of the array which should be returned, so the command looks like this: `{vendor:helper(property: 'foo')}`. Not as elegant as a pure fluid solution could be, though. – Jost Nov 14 '15 at 08:04

4 Answers4

1

As @Jpsy said, there is the VHS Variable / GetViewHelper.

But the usage should be {v:variable.get(name: '{vendor:helper()}.foo')}.

If you need the returned array of your viewhelper multiple times in your template, it's better to use it the way you already did. Because otherwise you would call the PHP method behind the viewhelper to build and return the array each time you want to access an index of an already previously built array again.

Community
  • 1
  • 1
Seika85
  • 1,981
  • 2
  • 18
  • 29
0

v:variable.get of VHS viewhelpers does exactly what you want:

{v:variable.get(name: 'data.{foo}')}

This returns the item with index {foo} from array data.

Jpsy
  • 20,077
  • 7
  • 118
  • 115
0

You write that you're using a custom ViewHelper. Can you modify it?

Inside the ViewHelper you can easy assign a Variable with:

$this->templateVariableContainer->add('variable', 'content');

Naderio
  • 1,306
  • 11
  • 26
-1

It depends also on what you're really trying to reach. If you want to cicle the array, you should create a different viewhelper If you want to create the array, and then access all the data in different position, you're looking for the f:alias fluid helper

Int he last situation, where you look a direct access of a property immediately after the helper call and no more about it, you have to change your viewhelper, with an optional value. If the helper recive the value, you return the element, otherwise it returns the entire array

RiccardoC
  • 876
  • 5
  • 10