Using Velocity in Servoy

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Using Velocity in Servoy

Postby roddy » Mon May 24, 2021 7:01 am

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...
roddy
 
Posts: 82
Joined: Mon Oct 26, 2020 12:32 am

Re: Using Velocity in Servoy

Postby roddy » Mon May 24, 2021 7:52 am

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 });
roddy
 
Posts: 82
Joined: Mon Oct 26, 2020 12:32 am

Re: Using Velocity in Servoy

Postby ptalbot » Mon May 24, 2021 9:20 pm

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
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1636
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Using Velocity in Servoy

Postby roddy » Tue May 25, 2021 12:44 am

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.
roddy
 
Posts: 82
Joined: Mon Oct 26, 2020 12:32 am

Re: Using Velocity in Servoy

Postby ptalbot » Wed May 26, 2021 12:32 am

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
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1636
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Using Velocity in Servoy

Postby roddy » Thu May 27, 2021 1:01 am

Thanks again for passing that on :-)
roddy
 
Posts: 82
Joined: Mon Oct 26, 2020 12:32 am


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 4 guests