Search in sources :

Example 61 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class IncludeAndImportConfigurableLayersTest method test3LayerImportClashes.

@Test
public void test3LayerImportClashes() throws Exception {
    Configuration cfg = getConfiguration();
    cfg.addAutoImport("t1", "t1.ftl");
    cfg.addAutoImport("t2", "t2.ftl");
    cfg.addAutoImport("t3", "t3.ftl");
    TemplateConfiguration tc = new TemplateConfiguration();
    tc.addAutoImport("t2", "t2b.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", "t3b.ftl");
        env.process();
        assertEquals("In main: t1;t2b;t3b;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main2.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.addAutoImport("t3", "t3b.ftl");
        env.process();
        assertEquals("In main2: t1;t2;t3b;", sw.toString());
    }
    {
        Template t = cfg.getTemplate("main.ftl");
        StringWriter sw = new StringWriter();
        Environment env = t.createProcessingEnvironment(null, sw);
        env.process();
        assertEquals("In main: t1;t3;t2b;", 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 62 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class IncludeAndImportTest method lazyAutoImportSettings.

@Test
public void lazyAutoImportSettings() throws IOException, TemplateException {
    Configuration cfg = getConfiguration();
    cfg.addAutoImport("l1", "lib1.ftl");
    cfg.addAutoImport("l2", "lib2.ftl");
    cfg.addAutoImport("l3", "lib3.ftl");
    String ftl = "<@l2.m/>, <@l1.m/>; ${history}";
    String expectedEagerOutput = "In lib2, In lib1; L1L2L3";
    String expecedLazyOutput = "In lib2, In lib1; L2L1";
    assertOutput(ftl, expectedEagerOutput);
    cfg.setLazyImports(true);
    assertOutput(ftl, expecedLazyOutput);
    cfg.setLazyImports(false);
    assertOutput(ftl, expectedEagerOutput);
    cfg.setLazyAutoImports(true);
    assertOutput(ftl, expecedLazyOutput);
    cfg.setLazyAutoImports(null);
    assertOutput(ftl, expectedEagerOutput);
    cfg.setLazyImports(true);
    cfg.setLazyAutoImports(false);
    assertOutput(ftl, expectedEagerOutput);
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 63 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class IncludeAndImportTest method createConfiguration.

@Override
protected Configuration createConfiguration() throws Exception {
    Configuration cfg = super.createConfiguration();
    cfg.setTemplateLoader(new StringTemplateLoader());
    return cfg;
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration)

Example 64 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class InterpretSettingInheritanceTest method tagSyntaxTest.

@Test
public void tagSyntaxTest() throws IOException, TemplateException {
    Configuration cfg = getConfiguration();
    cfg.setTagSyntax(Configuration.ANGLE_BRACKET_TAG_SYNTAX);
    assertOutput(FTL_S_A_S, OUT_A_S_WHEN_SYNTAX_IS_A);
    assertOutput(FTL_S_S_A, OUT_S_A_WHEN_SYNTAX_IS_A);
    assertOutput(FTL_A_A_S, OUT_A_S_WHEN_SYNTAX_IS_A);
    assertOutput(FTL_A_S_A, OUT_S_A_WHEN_SYNTAX_IS_A);
    cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
    assertOutput(FTL_S_A_S, OUT_A_S_WHEN_SYNTAX_IS_S);
    assertOutput(FTL_S_S_A, OUT_S_A_WHEN_SYNTAX_IS_S);
    assertOutput(FTL_A_A_S, OUT_A_S_WHEN_SYNTAX_IS_S);
    assertOutput(FTL_A_S_A, OUT_S_A_WHEN_SYNTAX_IS_S);
    cfg.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
    assertOutput(FTL_S_A_S, OUT_A_S_WHEN_SYNTAX_IS_A);
    assertOutput(FTL_S_S_A, OUT_S_A_WHEN_SYNTAX_IS_S);
    assertOutput(FTL_A_A_S, OUT_A_S_WHEN_SYNTAX_IS_A);
    assertOutput(FTL_A_S_A, OUT_S_A_WHEN_SYNTAX_IS_S);
    assertOutput("<@'[#ftl]x'?interpret />[#if true]y[/#if]", "x[#if true]y[/#if]");
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 65 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class InterpretSettingInheritanceTest method evalTest.

@Test
public void evalTest() throws IOException, TemplateException {
    Configuration cfg = getConfiguration();
    cfg.setTagSyntax(Configuration.ANGLE_BRACKET_TAG_SYNTAX);
    assertOutput("<@'\"[#if true]s[/#if]<#if true>a</#if>\"?interpret'?eval />", OUT_S_A_WHEN_SYNTAX_IS_A);
    assertOutput("[#ftl][@'\"[#if true]s[/#if]<#if true>a</#if>\"?interpret'?eval /]", OUT_S_A_WHEN_SYNTAX_IS_A);
    cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
    assertOutput("[@'\"[#if true]s[/#if]<#if true>a</#if>\"?interpret'?eval /]", OUT_S_A_WHEN_SYNTAX_IS_S);
    assertOutput("<#ftl><@'\"[#if true]s[/#if]<#if true>a</#if>\"?interpret'?eval />", OUT_S_A_WHEN_SYNTAX_IS_S);
}
Also used : Configuration(freemarker.template.Configuration) 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