Java Code Examples for org.beetl.core.resource.StringTemplateResourceLoader

The following examples show how to use org.beetl.core.resource.StringTemplateResourceLoader. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: seed   Source File: BeetlTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void buildFromString() throws IOException {
    GroupTemplate groupTemplate = new GroupTemplate(new StringTemplateResourceLoader(), Configuration.defaultConfiguration());
    //设置模板字符串
    Template template = groupTemplate.getTemplate("<!DOCTYPE HTML>\n" +
            "<html>\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "    <title>beetl demo</title>\n" +
            "</head>\n" +
            "<body>\n" +
            "    长篇架空武侠小说:${book},真的忍心烂尾么??\n" +
            "</body>\n" +
            "</html>");
    //设置模板所需的共享变量
    Map<String, Object> sharedVars = new HashMap<>();
    sharedVars.put("book", "英雄志");
    template.binding(sharedVars);
    //template.binding("book", "野狗志");
    //解析模板字符串
    String data = template.render();
    System.out.println(data);
}
 
Example 2
public void loading() throws AlbianParserException {
    try {
        FileResourceLoader resourceLoader = new FileResourceLoader(c.getRootPath(), c.getCharset());
        Configuration cfg = Configuration.defaultConfiguration();
        cfg.setHtmlTagSupport(true);
        gt = new GroupTemplate(resourceLoader, cfg);
        StringTemplateResourceLoader rl = new StringTemplateResourceLoader();
        sgt = new GroupTemplate(rl, cfg);
        Map<String, CustomTagConfigurtion> ctcs = c.getCustomTags();
        for (Map.Entry<String, CustomTagConfigurtion> entry : ctcs.entrySet()) {
            CustomTagConfigurtion ctc = entry.getValue();
            Class<? extends Tag> cla = (Class<? extends Tag>) AlbianClassLoader.getInstance().loadClass(ctc.getFullClassname());
            if (null != cla) {
                gt.registerTag(ctc.getName(), cla);
                sgt.registerTag(ctc.getName(), cla);
            }
        }

    } catch (Exception e) {
        throw new AlbianParserException(e);
    }

}
 
Example 3
Source Project: beetl2.0   Source File: NativeTest.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSecurity() throws Exception
{

	StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
	Configuration cfg = Configuration.defaultConfiguration();
	GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
	Template t = gt.getTemplate("hello,${@java.lang.System.currentTimeMillis()}");

	String str = t.render();
	AssertJUnit.assertEquals("hello,", str);

}
 
Example 4
Source Project: beetl2.0   Source File: HelloBeetl.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();		
	Configuration cfg = Configuration.defaultConfiguration();
	GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
	Template t = gt.getTemplate("hello,${name}");
	t.binding("name", "beetl");		
	String str = t.render();
	System.out.println(str);

}