Creating your own custom helpers with Freemarker?

From my controller, I installed my model and looked like:

ModelAndView mav = new ModelAndView();

mav.setView("index");

mav.addObject("user", user);
mav.addObject("someCollection", someCollection);

return mav;

Now I want to create a helper object that will accept someCollection and a custom object as parameters.

My helper function outputs some HTML, etc., is this possible?

+4
source share
3 answers

You can write macros and directives using FTL or Java , open them for your templates and call them the same way you do with the built-in macros / directives.

+5
source

- Java, . , , , , : ${helper.myMethod(arg)}.

+2
/**
 * Add logotype logotype1AsBase64.
 * @return
 * @throws IOException 
@ModelAttribute("logotype1AsBase64")
public String getLogotype() 
        throws IOException {
    return logotypeService.getLogotype();
}
*/

@ModelAttribute
public void addAttributes(Model model) throws IOException {
    //...
    model.addAttribute("logotype1AsBase64", logotypeCacheServ.getImage("logotypeInEnglish1From20190101.png"));
    //...
}

:

<img src="<#if locale == 'specificLocale'>${logotype1AsBase64}></#if>" alt="description for picture">

And the model attribute may contain more html than base64 for the logo, if the setting is so.

0
source

Source: https://habr.com/ru/post/1752107/


All Articles