How to display image from Media in a Velocity template?

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

How to display image from Media in a Velocity template?

Postby roddy » Mon Aug 02, 2021 6:32 am

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

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

Postby roddy » Mon Aug 02, 2021 7:25 am

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

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

Postby ROCLASI » Mon Aug 02, 2021 9:53 am

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

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

Postby ROCLASI » Mon Aug 02, 2021 11:24 am

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

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

Postby roddy » Tue Aug 03, 2021 2:46 am

Thanks for that, much appreciated :-)
roddy
 
Posts: 100
Joined: Mon Oct 26, 2020 12:32 am


Return to Programming with Servoy

Who is online

Users browsing this forum: Bing [Bot] and 11 guests