use of freemarker.template.Configuration in project freemarker by apache.
the class TemplateNameSpecialVariablesTest method testArgumentBugWithMacro.
@Test
public void testArgumentBugWithMacro() throws TemplateException, IOException {
StringTemplateLoader tl = new StringTemplateLoader();
tl.putTemplate("main.ftl", "" + "<#include 'inc.ftl'>" + "Before: ${.currentTemplateName}\n" + "<@m p1=.currentTemplateName; x>" + "Loop var: ${x}\n" + "In nested: ${.currentTemplateName}\n" + "</@>" + "After: ${.currentTemplateName}");
tl.putTemplate("inc.ftl", "" + "<#macro m p1 p2=.currentTemplateName>" + "p1: ${p1}\n" + "p2: ${p2}\n" + "Inside: ${.currentTemplateName}\n" + "<#nested .currentTemplateName>" + "</#macro>");
Configuration cfg = getConfiguration();
cfg.setTemplateLoader(tl);
for (boolean fixed : new boolean[] { false, true }) {
cfg.setIncompatibleImprovements(fixed ? Configuration.VERSION_2_3_28 : Configuration.VERSION_2_3_27);
assertOutputForNamed("main.ftl", "" + "Before: main.ftl\n" + "p1: " + (fixed ? "main.ftl" : "inc.ftl") + "\n" + "p2: inc.ftl\n" + "Inside: inc.ftl\n" + "Loop var: inc.ftl\n" + "In nested: main.ftl\n" + "After: main.ftl");
}
}
use of freemarker.template.Configuration in project freemarker by apache.
the class IncludeAndImportConfigurableLayersTest method test3LayerIncludesNoClashes.
@Test
public void test3LayerIncludesNoClashes() throws Exception {
Configuration cfg = getConfiguration();
cfg.addAutoInclude("t1.ftl");
TemplateConfiguration tc = new TemplateConfiguration();
tc.addAutoInclude("t2.ftl");
cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("main.ftl"), tc));
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoInclude("t3.ftl");
env.process();
assertEquals("T1;T2;T3;In main: t1;t2;t3;", sw.toString());
}
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.process();
assertEquals("T1;T2;In main: t1;t2;", sw.toString());
}
{
Template t = cfg.getTemplate("main2.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoInclude("t3.ftl");
env.process();
assertEquals("T1;T3;In main2: t1;t3;", sw.toString());
}
cfg.removeAutoInclude("t1.ftl");
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoInclude("t3.ftl");
env.process();
assertEquals("T2;T3;In main: t2;t3;", sw.toString());
}
}
use of freemarker.template.Configuration in project freemarker by apache.
the class IncludeAndImportConfigurableLayersTest method test3LayerLazyness.
private void test3LayerLazyness(Class<?> layer, Boolean lazyImports, Boolean lazyAutoImports, boolean setLazyAutoImports, String expectedOutput) throws Exception {
dropConfiguration();
Configuration cfg = getConfiguration();
cfg.addAutoImport("t1", "t1.ftl");
Template t = new Template(null, "<#import 't2.ftl' as t2>${loaded!}", cfg);
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
if (layer == Configuration.class) {
setLazynessOfConfigurable(cfg, lazyImports, lazyAutoImports, setLazyAutoImports);
} else if (layer == Template.class) {
setLazynessOfConfigurable(t, lazyImports, lazyAutoImports, setLazyAutoImports);
} else if (layer == Environment.class) {
setLazynessOfConfigurable(env, lazyImports, lazyAutoImports, setLazyAutoImports);
} else {
throw new IllegalArgumentException();
}
env.process();
assertEquals(expectedOutput, sw.toString());
}
use of freemarker.template.Configuration in project freemarker by apache.
the class IncludeAndImportConfigurableLayersTest method test3LayerIncludesClashes2.
@Test
public void test3LayerIncludesClashes2() throws Exception {
Configuration cfg = getConfiguration();
cfg.addAutoInclude("t1.ftl");
cfg.addAutoInclude("t1.ftl");
TemplateConfiguration tc = new TemplateConfiguration();
tc.addAutoInclude("t2.ftl");
tc.addAutoInclude("t2.ftl");
cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("main.ftl"), tc));
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoInclude("t3.ftl");
env.addAutoInclude("t3.ftl");
env.addAutoInclude("t1.ftl");
env.addAutoInclude("t1.ftl");
env.process();
assertEquals("T2;T3;T1;In main: t2;t3;t1;", sw.toString());
}
}
use of freemarker.template.Configuration in project freemarker by apache.
the class IncludeAndImportConfigurableLayersTest method test3LayerImportNoClashes.
@Test
public void test3LayerImportNoClashes() throws Exception {
Configuration cfg = getConfiguration();
cfg.addAutoImport("t1", "t1.ftl");
TemplateConfiguration tc = new TemplateConfiguration();
tc.addAutoImport("t2", "t2.ftl");
cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("main.ftl"), tc));
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoImport("t3", "t3.ftl");
env.process();
assertEquals("In main: t1;t2;t3;", sw.toString());
}
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.process();
assertEquals("In main: t1;t2;", sw.toString());
}
{
Template t = cfg.getTemplate("main2.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoImport("t3", "t3.ftl");
env.process();
assertEquals("In main2: t1;t3;", sw.toString());
}
cfg.removeAutoImport("t1");
{
Template t = cfg.getTemplate("main.ftl");
StringWriter sw = new StringWriter();
Environment env = t.createProcessingEnvironment(null, sw);
env.addAutoImport("t3", "t3.ftl");
env.process();
assertEquals("In main: t2;t3;", sw.toString());
}
}
Aggregations