Page 1 of 1

How to display image from Media in a Velocity template?

PostPosted: Mon Aug 02, 2021 6:32 am
by roddy
I'm unable to figure out how to display an image stored in the Servoy Media of a solution within a report generated by Velocity; what actually goes into the HTML template? There is reference to using plugins.getMedia() (which doesn't seem to exist, although solutionMode.getMedia() does) but not what the syntax is within the template itself to display the image.

The FAQ references getImageTag() but I also cannot figure out what the
Code: Select all
<img src="theinternalURLOfYourBlobObject"
represents...

Anything would be much appreciated as I'm sure it must be quite simple.

Thanks in advance.

Re: How to display image from Media in a Velocity template?

PostPosted: Mon Aug 02, 2021 7:25 am
by roddy
For anyone else that has come across this; the variable is passed directly into the context object. As an example:
Code: Select all
{ foundset: myFoundSet, Logo: myLogo, currency: "$#,##0.00" }


This can then be referenced in the HTML as:
$myLogo

The tag syntax I found confusing is that the theinternalURLOfYourBlobObject is what the variable is translated into, not what you should be using.

Hope this helps.

Re: How to display image from Media in a Velocity template?

PostPosted: Mon Aug 02, 2021 9:53 am
by ROCLASI
Hi Roddy,

You can embed the whole image in the template like so:

Code: Select all
var template = '<html><head></head><body><p>$myLogo</p></body></html>';
// no img tag needed, it will be generated for you

var context = {myLogo: plugins.VelocityReport.getByteMedia(foundset.logo)};
// parameter options are getByteMedia(bytes, [id], [width], [height], [cssClass]):

application.output(plugins.VelocityReport.evaluateWithContext(template, context));


Hope this helps.

Re: How to display image from Media in a Velocity template?

PostPosted: Mon Aug 02, 2021 11:24 am
by ROCLASI
Reading your post again I see you actually wanted to use an image that is part of the solution. You can use the same code with a slight change:
Use solutionModel.getMedia('myLogo.png').bytes instead of foundset.logo.

So the code would look like this:
Code: Select all
var template = '<html><head></head><body><p>$myLogo</p></body></html>';
// no img tag needed, it will be generated for you

var context = {myLogo: plugins.VelocityReport.getByteMedia(solutionModel.getMedia('myLogo.png').bytes)};
// parameter options are getByteMedia(bytes, [id], [width], [height], [cssClass]):

application.output(plugins.VelocityReport.evaluateWithContext(template, context));


Hope this helps.

Re: How to display image from Media in a Velocity template?

PostPosted: Tue Aug 03, 2021 2:46 am
by roddy
Thanks for that, much appreciated :-)