Search in sources :

Example 56 with Configuration

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");
    }
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 57 with Configuration

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());
    }
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 58 with Configuration

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());
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) Template(freemarker.template.Template)

Example 59 with Configuration

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());
    }
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 60 with Configuration

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());
    }
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Aggregations

Configuration (freemarker.template.Configuration)282 Template (freemarker.template.Template)106 Test (org.junit.Test)86 TemplateTest (freemarker.test.TemplateTest)52 HashMap (java.util.HashMap)51 IOException (java.io.IOException)41 StringWriter (java.io.StringWriter)39 File (java.io.File)33 TemplateException (freemarker.template.TemplateException)29 Writer (java.io.Writer)27 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)19 ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)19 StringTemplateLoader (freemarker.cache.StringTemplateLoader)18 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)18 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 TemplateLoader (freemarker.cache.TemplateLoader)16 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)15 FileWriter (java.io.FileWriter)13 ArrayList (java.util.ArrayList)13 User (org.vcell.util.document.User)13