Is there a way to convert a byte array to base64 in a Freemarker template? Something like:
<img src="data:image/jpeg;base64,${photo.data?base64}" />
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s no built-in way in the current release. (But of course you can call your own method to do it.)
The object whose methods you want to call must be put into the data-model (i.e. the template context). (For statics: freemarker.org/docs/pgui_misc_beanwrapper.html#autoid_55)
But, for easier maintenance, I prefer this: create class
com.foo.Base64EncoderMethod implements TemplateMethodModelEx(that callsBase64.encodeBase64internally), then create a templatelib/utils.ftl(if you don’t already have something like that) and in that, call<#assign base64Encode = 'com.foo.Base64EncoderMethod'?new()>. Then in the templates, call<#import '/lib/utils.ftl' as u>and${u.base64Encode(photo.data)}