Page 1 of 1

Using Velocity in Servoy

PostPosted: Mon May 24, 2021 7:01 am
by roddy
Is there anything, anywhere that will detail how to access the XHTML template for a Velocity report in Servoy? I have read through the documentation, I have looked through the many layers and objects of the Velocity sample projects and I just want to see how I transfer a merge a foundset with a template to produce a report.

It looks like the renderTemplate() is the one, which simply refers to the evaluateWithContext(), which states:
This method will allow you to use the power of Velocity to build some text or html using a String template and a Context object.
The signature is:
String evaluateWithContext(String templateContent, JSObject contextObject, [REPORT.resolution])

Basically, you pass it a String, containing placeholders ($variable) and then pass it a JSObject (new Object() or {}) filled with any kind of Servoy object to replace the placeholders.

Is any kind of Servoy object a foundset/dataset/datasource? How does it differentiate and thence how do I refer to the data within those differing (internally described) Servoy objects?

Any help would be appreciated...

Re: Using Velocity in Servoy

PostPosted: Mon May 24, 2021 7:52 am
by roddy
For anyone else that might be finding the same issue:

You need to create a context variable, which is an object with the following where 'name' could be a foundset or record:
Code: Select all
var context = { foundset: name }


You can get the merge in velocity to work by stating the constant 'foundset' as the merge variable with a column. So in the template document you could have:
Code: Select all
Something constant in the report
$foundset.column_name
Something else constand in the report


You call the 'renderTemplate' function with the context above:
Code: Select all
var mergedHTMLAsString = plugins.VelocityReport.renderTemplate("template.html", { foundset: name });

Re: Using Velocity in Servoy

PostPosted: Mon May 24, 2021 9:20 pm
by ptalbot
FYI, you can put anything in your context parameter object: foundsets, datasets, records, arrays, objects, booleans, strings, dates, numbers, integers...

You name them whatever you want then you access it via $nameOfYourContextPropertyVariable

The convenient thing about the Velocity template language (amongst other things) is that it allows you to iterate on foundset, dataset, record, array and object the same way, with a simple #foreach loop

Re: Using Velocity in Servoy

PostPosted: Tue May 25, 2021 12:44 am
by roddy
Thanks for passing that on; the tricky thing is when the documentation says it can be anything, then it is difficult to know which parts are required and what the naming scopes of the ranges are. If you don't have to name it 'foundset', then does it determine the format of the object (or field type) passed in automatically? If so, does that entail a requirement of any types passed in to make sure they are interpreted correctly?

Thanks again.

Re: Using Velocity in Servoy

PostPosted: Wed May 26, 2021 12:32 am
by ptalbot
You can name your variables as you want. A foundset doesn't need to be called foundset, it can be called "fs", "myBeautifulFoundset", whatever.
Foundsets, datasets, objects, records and arrays are all wrapped as Velocity compatible objects which all have the same interface, meaning you can use them the same way within #foreach loops, for example:

Code: Select all
#foreach($record in $foundset)
   $record.aField
#end

#foreach($row in $dataset)
   $row.aField
#end

#foreach($obj in $array)
   #foreach($prop in $obj.properties)
      $prop
   #end
#end


You get record values using $record.aColumnName, same for an object: $obj.aProperty
Of course record can have relations, so you can do: $record.arelation.anotherrelation.acolumnName

Re: Using Velocity in Servoy

PostPosted: Thu May 27, 2021 1:01 am
by roddy
Thanks again for passing that on :-)