Java Code Examples for org.beetl.core.Template#renderTo()

The following examples show how to use org.beetl.core.Template#renderTo() . 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 File: AlbianBeetlTemplateService.java    From Albianj2 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void internalRenderTemplate(String templatePath,
                                      View page,
                                      Map<String, ?> model,
                                      Map<String, Class<?>> funcs,
                                      Writer writer)
        throws IOException, TemplateException {
    if (!Validate.isNullOrEmpty(funcs)) {
        for (Map.Entry<String, Class<?>> e : funcs.entrySet()) {
            if (!functions.containsKey(e.getKey())) {
                gt.registerFunctionPackage(e.getKey(), e.getValue());
                sgt.registerFunctionPackage(e.getKey(), e.getValue());
                functions.put(e.getKey(), e.getValue());
            }
        }
    }
    Template t = gt.getTemplate(templatePath);
    t.binding(model);
    t.renderTo(writer);
}
 
Example 2
Source File: GroupTemplateTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		String home = System.getProperty("user.dir") + File.separator
				+ "template" + File.separator;
		Configuration cf = Configuration.defaultConfiguration();
		cf.setStatementStart("<!--:");
		cf.setStatementEnd("-->");
		FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
		GroupTemplate gt = new GroupTemplate(rs, cf);

		List<StockModel> list = StockModel.dummyItems();

		Template t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		StringWriter sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

		// 第二次
		t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

	}
 
Example 3
Source File: GroupTemplateTest.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		String home = System.getProperty("user.dir") + File.separator
				+ "template" + File.separator;
		Configuration cf = Configuration.defaultConfiguration();
		cf.setStatementStart("<!--:");
		cf.setStatementEnd("-->");
		FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
		GroupTemplate gt = new GroupTemplate(rs, cf);

		List<StockModel> list = StockModel.dummyItems();

		Template t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		StringWriter sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

		// 第二次
		t = gt.getTemplate("/helloworld.html");
		t.binding("items", list);
		sw = new StringWriter();
		t.renderTo(sw);
		System.out.println(sw.toString());

	}
 
Example 4
Source File: HTMLTagSupportWrapper.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void callHtmlTag(String path)
{
	Template t = null;

	t = gt.getHtmlFunctionOrTagTemplate(path, this.ctx.getResourceId());

	t.binding(ctx.globalVar);
	t.dynamic(ctx.objectKeys);

	if (args.length == 2)
	{
		Map<String, Object> map = (Map<String, Object>) args[1];
		for (Entry<String, Object> entry : map.entrySet())
		{
			t.binding(entry.getKey(), entry.getValue());

		}
	}

	BodyContent bodyContent = super.getBodyContent();
	t.binding("tagBody", bodyContent);

	t.renderTo(ctx.byteWriter);
}
 
Example 5
Source File: SQLBeetlRender.java    From weed3 with Apache License 2.0 5 votes vote down vote up
@Override
public String render(String path, Map<String, Object> args) throws Exception {
    if (path == null) {
        return null;
    }

    StringWriter writer = new StringWriter();

    Template template = gt.getTemplate(path);
    template.binding(args);
    template.renderTo(writer);

    return writer.toString();
}
 
Example 6
Source File: BeetlTemplate.java    From DBMetadata with MIT License 5 votes vote down vote up
/**
 * ���浽ָ��λ��
 *
 * @param t
 * @param path
 * @throws IOException
 */
private static void render(Template t, String path) throws IOException {
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), "UTF-8");

    t.renderTo(writer);

    writer.close();
}
 
Example 7
Source File: LayoutTag.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void render()
{
	if (args.length == 0 || args.length > 3)
	{
		throw new RuntimeException("参数错误,期望child,map");
	}
	String layoutFile = getRelResourceId();
	Template t = this.gt.getTemplate(layoutFile, this.ctx.getResourceId());

	t.binding(ctx.globalVar);
	t.dynamic(ctx.objectKeys);

	if (args.length >= 2)
	{
		Map<String, Object> map = (Map<String, Object>) args[1];
		for (Entry<String, Object> entry : map.entrySet())
		{
			t.binding(entry.getKey(), entry.getValue());
		}
	}

	BodyContent content = this.getBodyContent();
	if (args.length == 3)
	{

		t.binding((String) args[2], content);
	}
	else
	{
		t.binding(defaultLayoutName, content);
	}
	t.renderTo(ctx.byteWriter);

}
 
Example 8
Source File: IncludeTag.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void render()
{
	String resourceId = getRelResourceId();
	;

	Template t = gt.getTemplate(resourceId, this.ctx.getResourceId());
	//快速复制父模板的变量
	t.binding(this.ctx.globalVar);
	if (ctx.objectKeys != null && ctx.objectKeys.size() != 0)
	{
		t.dynamic(ctx.objectKeys);
	}

	if (this.args.length == 2)
	{
		Map<String, Object> map = (Map<String, Object>) this.args[1];
		for (Entry<String, Object> entry : map.entrySet())
		{
			Object value = entry.getValue();
			if (value instanceof Map || value instanceof Collection)
			{
				t.binding((String) entry.getKey(), value, true);
			}
			else
			{
				t.binding((String) entry.getKey(), value);
			}

		}

	}

	ByteWriter bw = ctx.byteWriter;
	t.renderTo(bw);

}
 
Example 9
Source File: HTMLTagSupportWrapper2.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void callHtmlTag(String path) {
    Template t = null;

    t = gt.getHtmlFunctionOrTagTemplate(path, this.ctx.getResourceId());

    t.binding(ctx.globalVar);
    t.dynamic(ctx.objectKeys);

    if (args.length == 2) {
        Map<String, Object> map = (Map<String, Object>)args[1];
        for (Entry<String, Object> entry : map.entrySet()) {
            t.binding(entry.getKey(), entry.getValue());

        }
    }
    /*
       模板需要调用方法 与默认实现不同,并没有先渲染body体,而是延迟处理, 等待调用的时候在获取tag体内容
      
     */
    t.binding("tagBody", new Object() {
    	public String toString() {
    		try {
    			return HTMLTagSupportWrapper2.super.getBodyContent().toString();
    		}catch(BeetlException ex){
    			ex.inTagBody = true;
    			throw ex;
    		}
    		
    		
    	}
    });
    t.renderTo(ctx.byteWriter);
}
 
Example 10
Source File: GenerateJmx.java    From testing_platform with Apache License 2.0 4 votes vote down vote up
public static void generate(RunPlanBean rpb) {
    try {
        System.out.println(rpb.toString());

        String root = TPController.class.getClassLoader().getResource("").getPath() + "beetl";

        FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
        Configuration cfg = Configuration.defaultConfiguration();
        GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
        Template t = gt.getTemplate("/http_request.jmx");

        t.binding("v_duration", rpb.getDuration());
        t.binding("v_users_num", rpb.getUsersNum());
        t.binding("v_ramp_up", rpb.getRampUp());
        TestPlanBean tpb = rpb.getTestPlanBean(); // TODO: wangc@2017/3/13  不够优雅,改成解析成map,直接binding map 
        if (null != tpb) {
            t.binding("v_server_name", tpb.getServerNameIp());
            t.binding("v_port", tpb.getPortNum());
            t.binding("v_prol", tpb.getProtocol());
            t.binding("v_path", tpb.getPath());
        } else {
            t.binding("v_server_name", "111");
            t.binding("v_port", "222");
            t.binding("v_prol", "333");
            t.binding("v_path", "444");
        }

        String jmxRoot = System.getProperty("user.dir")+JMX_PATH;//系统(项目)路径,结尾无斜杠
        //jmx在系统(项目)中的存在路径,斜杠开头,结尾无斜杠
        
        String fullPath = jmxRoot+StringUtils.creAndGetDir(jmxRoot); //返回的应该是  c:/jmx/2017/03  ,并创建这个目录
        
        //文件名,斜杠开头
        String fileName = new StringBuilder(File.separator)
                .append(JMX_NAME_PREFIX)
                .append(StringUtils.getDate("yyyyMMddHHmmssSSS"))
                .append(JMX_NAME_SUFFIX)
                .toString();

        String jmxFilePath = fullPath+fileName;
        rpb.setJmxPath(jmxFilePath);
        OutputStream ops = new FileOutputStream(jmxFilePath);
        t.renderTo(ops);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: BeetlView.java    From ymate-platform-v2 with Apache License 2.0 4 votes vote down vote up
private void __doRender(OutputStream outputStream) {
    Template _tmpl = __groupTemplate.getTemplate(__path);
    _tmpl.binding(__attributes);
    _tmpl.renderTo(outputStream);
}