Re: Class StringTemplateLoader
Daniel Dekany <[email protected]> Thu, 12 Mar 2015 21:46:42 +0100
| Newsgroups | gmane.comp.web.freemarker.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
If you want to "push" the template content, like you do, then you
don't need any TemplateLoader for this, nor you need
Configuration.getTemplate. Just use the Template constructor that
takes the template content as a String (or as a Reader). You may still
want to cache the resulting Template for speed, but that's up to you
in this scenario.
Another approach is implementing a TemplateLoader that loads from the
DB, then set that as the TemplateLoader in the Configuration, and then
using Configuration.getTemplate will "pull" the template, and the
Template cache will work too of course.
Thursday, March 12, 2015, 11:39:10 AM, Vaibhav wrote:
> Hello devs,
>
> This is with reference to class freemarker.cache.StringTemplateLoader
>
> I have a requirement where I want to create templates on the fly from strings.
> The templates will be stored in String fromat in database.
>
> For this I am first loading the custom template in TemplateLoader
> and then retreiving it by passing the datamodel map.
>
> Below is the sample code for this.
>
> After I am done, I want to remove the loaded template from the TemplateLoader.
>
> I tried extending the StringTemplateLoader class for this purpose
> but there is no getter method specified for the template map in this class.
>
> Maybe we can add a remove method to this class.
> If the remove method has not been deliberately added, can you please enlighten me on this.
>
> public String freemarkerDo(Map datamodel, CustomEmailTemplate cet){
> StringBuffer content = new StringBuffer();
> StringBuilder sb = new StringBuilder();
>
> sb.append(cet.getEmailTemplateType().getName()).append(cet.getCommunity().getId());
> if(CommonUtil.isNotNull(cet)){
> StringTemplateLoader loader = (StringTemplateLoader)
> cfg.getConfiguration().getTemplateLoader();
> String templateSource =
> cet.getEmailContent().getEmailContent();
> loader.putTemplate(sb.toString(), templateSource);
> cfg.getConfiguration().setTemplateLoader(loader);
> try {
> Template tpl =
> cfg.getConfiguration().getTemplate(sb.toString());
>
> content.append(FreeMarkerTemplateUtils.processTemplateIntoString(tpl, datamodel));
> } catch (Exception e) {
> log.error("Error retreiving custom template for
> community:{} of type
> {}",cet.getCommunity().getId(),cet.getEmailTemplateType().getName());
> }
> return content.toString();
> }
> return null;
> }
>
> Thanks
> Vaibhav
>
--
Thanks,
Daniel Dekany
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/