Home » freemarker-2.3.13 » freemarker.cache » [javadoc | source]
public class: StringTemplateLoader [javadoc | source]

All Implemented Interfaces:

A TemplateLoader that uses a Map with Strings as its source of templates. In most case the regular way of loading templates from files will be fine. However, there can be situations where you don't want to or can't load a template from a file, e.g. if you have to deploy a single jar for JavaWebStart or if they are contained within a database. A single template can be created manually e.g.
String templateStr="Hello ${user}";
Template t = new Template("name", new StringReader(templateStr),
new Configuration());
If, however, you want to create templates from strings which import other templates this method doesn't work. In that case you can create a StringTemplateLoader and add each template to it:
StringTemplateLoader stringLoader = new StringTemplateLoader();
stringLoader.putTemplate("greetTemplate", "<#macro greet>Hello");
stringLoader.putTemplate("myTemplate", "<#include \"greetTemplate\"><@greet/> World!");
Then you tell your Configuration object to use it:
After that you should be able to use the templates as usual. Often you will want to combine a StringTemplateLoader with another loader. You can do so using a freemarker.cache.MultiTemplateLoader .
Method from freemarker.cache.StringTemplateLoader Summary:
closeTemplateSource,   findTemplateSource,   getLastModified,   getReader,   putTemplate,   putTemplate
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from freemarker.cache.StringTemplateLoader Detail:
 public  void closeTemplateSource(Object templateSource) 
 public Object findTemplateSource(String name) 
 public long getLastModified(Object templateSource) 
 public Reader getReader(Object templateSource,
    String encoding) 
 public  void putTemplate(String name,
    String templateSource) 
    Puts a template into the loader. A call to this method is identical to the call to the three-arg #putTemplate(String, String, long) passing System.currentTimeMillis() as the third argument.
 public  void putTemplate(String name,
    String templateSource,
    long lastModified) 
    Puts a template into the loader. The name can contain slashes to denote logical directory structure, but must not start with a slash. If the method is called multiple times for the same name and with different last modified time, the configuration's template cache will reload the template according to its own refresh settings (note that if the refresh is disabled in the template cache, the template will not be reloaded). Also, since the cache uses lastModified to trigger reloads, calling the method with different source and identical timestamp won't trigger reloading.