Search the FirstSpirit Knowledge Base
Hello,
I've developed an script in SiteArchitect to monitoring purposes that lists all projects in the server.
My approach was to loop through projects and add them to an array:
List<String> projects = new ArrayList<String>();
...
Then in a loop:
projects.add(project.getName());
And in the end I return the result:
result.setValue(projects);
But what I get when I call the script inside CMS_RENDER tags is the array converted to a string, not the structured data.
I'd like to pass multiple results from the script inside an structured data: array, object...
Is this possible for script results?
Thanks.
Hi Andres,
$CMS_RENDER$ will directly render the result into the output, you cannot use the renderscript's result as a variable in the template.
But there is a kind of workaround: Just provide some empty container as parameter to the script. In your case an empty list seems a good choice, empty sets or maps can also be used:
$CMS_SET(set_result,[])$
$CMS_RENDER(script:"....", resultContainer:set_result)$
In the script, you then have access to the variable "resultContainer" (or whatever you name it) and add the information to it:
resultContainer.add(project.getName());
To avoid any output by the script itself, you should also add
result.setValue("");
at the end because otherwise the value of the last expression in the script might get output.
Then, "back" in the template (=after the CMS_RENDER call), the variable "set_result" has been populated by the script and you could for example iterate over it:
$CMS_FOR(for_projectName, set_result)$
$CMS_VALUE(for_projectName)$
$CMS_END_FOR$
You can - of course - also use something simpler, the for loop was just for clarification. It's important that the "container" is defined in the template so that you can access it from there after the script has put some values into it. And, by the way, you could also out other objects there, not only strings.
Michael
Hello Andres,
do you need further help or did Michael's reply already help you? If so, it would be great if you marked his reply as "correct answer" so that other community users find the solution easily. If you have already found a solution by yourself, it would be very kind of you, if you posted it here.
Best regards
Rene
Sorry for the late answer, I wasn't able to try it till now.
But I must say it worked like a charm!
Thank you, Michael.