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?
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:
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:
Something constant in the report
$foundset.column_name
Something else constand in the report
You call the ‘renderTemplate’ function with the context above:
var mergedHTMLAsString = plugins.VelocityReport.renderTemplate("template.html", { foundset: name });
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
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?
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:
#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