Fluid: Directly access value of array returned by view helper - typo3

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.

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.

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.

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');

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

Related

AEM Slightly - How to call java method whose name is stored in variable - expression inside expression doesn’t work in sightly

In sightly I have initialized the sling model class. Now, I want to call the method from that class. But method name is read from some variable (basically I read method name from other location). When I use that variable it doesn’t work. I know sightly doesn’t allow expression inside an expression, so would like to know if there is an alternative to fit this need.
<sly data-sly-use.detailsModel="org.svc.core.model.DetailsModel"/>
${detailsModel.{methodNameVariable}} - doesn’t work (if the method name is read from some variable)
${detailsModel.methodName} - works (if put the method name directly there)
That really dependes on how you structured your data model and what you want to do with it.
If you want to do something like an if ... else that can be achieved with:
<sly data-sly-test="${condition}">${model.someMethod}</sly>
<sly data-sly-test="${!condition}">${model.otherMethod}</sly>
If you want to do something like a switch ... case:
<sly data-sly-test="${condition1}">${model.method1}</sly>
<sly data-sly-test="${condition2}">${model.method2}</sly>
...
If you want to have a trully dynamic method names then you’re better off with a model that returns a map of values instead of having a number of properties/methods and use the dynamic method name as a key:
${model.details[detailName]}

dynamic setting and getting values from Swift class

I'd like to copy all properties from a NSManagedObject over to a "regular" Swift class. I don't want to do this manually, i.e. make a regular class with all the properties for every NSManagedObject and then manually copy all those values.
I do know how to read property names and values dynamically from my managed object, but how to set them on a Swift class in a way that I can then use those values like
mySwiftObject.name
which returns a String or
mySwiftObject.age
which returns a Number (as those are the types on the Managed Object). Custom subscripting and stuff like that came to my mind, but I didn't manage to achieve this... Is there a nice way to do exactly that?

get values of multiple checkboxes in extbase/fluid TYPO3

How to get values of multiple checkboxes checked in frontend template with fluid and use these values in action extbase?
Declare the argument in your controller action and give it a type array or an array-compatible type which can be constructed by the PropertyMapper.
Name all fields the same as this argument.
Post the data and use the argument in the controller action.
This is the correct way of receiving an array as value of a controller argument. Accessing it directly from the request is not recommended, unless you also declared the argument on the controller action. Not doing so will bypass important argument processing.
save them in a form and retrieve the POST data trough:
$this->request->getArgument('variable')

how to get NPObject from NPObject JS wrapper class

Function in NP API plugin creates NPObject and returns into javascript. Then javascript variable with returned NPObject is used as parameter for some other function of plugin. e.g.
var obj = plugin.GetObject()
plugin.UseObject( obj )
But in second function (UseObject) value of parameter is not original NPObject but NPObject JS wrapper class.
Is there way to get original NPObject from instance of NPObject JS wrapper class?
Short answer: you can't.
More involved answer: Some browsers will give you the originating object, but most these days won't, and there is no way to dereference past their opaque NPObject interface to get back to the underlying object.
Alternate solution: Instead of trying to get it that way, add a unique id to your NPObject and a global map to the pointer. Then when you get an NPObject that you think might be the object, call a method (or get a property) to get the unique ID and then you can look up the pointer.
this is the only method that I've found that works consistently across all browsers.

What Zend View Partial setObjectKey do?

i am reading zend framework docs on zend view partials
If your model is an object, you may
want to have it passed as an object to
the partial script, instead of
serializing it to an array of
variables. You can do this by setting
the 'objectKey' property of the
appropriate helper:
// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');
but what does this do. when do i use it and how.
I'm not 100% positive on this, but from what I can tell by looking at the source and documentation is that standard behavior for rendering a partial is that values are passed into it in the form of an associative array. This allows the values to be bound to variables using array keys.
echo $this->partial('partial.phtml', array ('person' => 'joe');
// in my partial..
<h1><?php echo $this->person; ?></h1> //<h1>Joe</h1>
If you pass an object as the third parameter, (ie, partial('partial.phtml', $myobject);), Zend_View_Partial will automatically serialize that object in an associative array, either by a custom implementation of toArray() or it will just grab the public properties via get_object_vars().
However, if you want to pass the whole object, as an object, you need to set the array key that gets transformed into a variable for the partial to reference.
$this->partial()->setObjectKey('myobject');
echo $this->partial('partial.phtml', $myobject);
What benefits this approach has over partial('partial.phtml', array( 'myobject' => $myobject), I'm not sure. Or I could be interpreting the documentation wrong.
Key as in array(key => value)?