| java.lang.Object freemarker.cache.StringTemplateLoader
StringTemplateLoader | public class StringTemplateLoader implements TemplateLoader(Code) | | 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#macro>");
stringLoader.putTemplate("myTemplate", "<#include \"greetTemplate\"><@greet/> World!");
Then you tell your Configuration object to use it:
cfg.setTemplateLoader(stringLoader);
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 .
version: $Id: v 1.0 2005/04/01 author: Meikel Bisping author: Attila Szegedi version: $Id: StringTemplateLoader.java,v 1.1 2005/04/08 11:47:53 szegedia Exp $ |
closeTemplateSource | public void closeTemplateSource(Object templateSource)(Code) | | |
getLastModified | public long getLastModified(Object templateSource)(Code) | | |
putTemplate | public void putTemplate(String name, String templateSource)(Code) | | Puts a template into the loader. A call to this method is identical to
the call to the three-arg
StringTemplateLoader.putTemplate(String,String,long)
passing System.currentTimeMillis() as the third argument.
Parameters: name - the name of the template. Parameters: templateSource - the source code of the template. |
putTemplate | public void putTemplate(String name, String templateSource, long lastModified)(Code) | | 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.
Parameters: name - the name of the template. Parameters: templateSource - the source code of the template. Parameters: lastModified - the time of last modification of the template in terms of System.currentTimeMillis() |
|
|