Beetl 模板引擎实践

Beetl是一款模板模板引擎,其号称性能是freemarker的6倍。所以值得尝试一下。详细参考http://ibeetl.com/guide/#beetl

模板引擎的作用:模板+数据===》预定义结构   用其来替换复杂字串的拼接则再好不过。

模板1:

<%
var cn=syntaxhelper.captial(attr);//调用自定义类方法captial,首字母大写。如name->Name  gender->Gender
var isList=null;
var listStr="List";
if(has(list))isList=list;
if(has(listClass))listStr=listClass;
%>
<%     //<%%>包含的是执行代码
if(isList!=null&&isList){
%>
    //template attribute
    private ${listStr}<${type}> ${attr};
<%}else{%>
    //template attribute
    private ${type} ${attr};
<%}%>

上面的模板中使用了syntaxhelper.captial方法,所以需要将该方法的定义告知模板引擎,通过增加配置文件

beetl.properties

并增加配置项:值为包名

FNP.syntaxhelper = com.a.b.c.d.code.utils.SyntaxHelper

至此在模板文件中使用syntaxhelper.captial则可以直接索引到对应的方法

下面为代码

package com.a.b.c.d.code.utils;

/**
 * Created by xx on 2017/3/24.
 */
public class SyntaxHelper {
    public static String captial(String tag) {
        tag = tag.substring(0, 1).toUpperCase() + tag.substring(1);
        return restrictConsecutiveCapitalCase(tag);
    }

    private static String restrictConsecutiveCapitalCase(String tag) {
        for (int k = 0; k < tag.length(); k++) {
            if (k + 1 < tag.length()) {
                if (Character.isUpperCase(tag.charAt(k))) {
                    if (Character.isUpperCase(tag.charAt(k + 1))) {
                        tag = tag.substring(0, k + 1)
                                + tag.substring(k + 1, k + 2).toLowerCase()
                                + tag.substring(k + 2);
                    }
                }
            }
        }
        return tag;
    }
}


执行测试:获取模板+传入数据

单例工具类

package com.a.b.c.d.code;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.FileResourceLoader;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * Created by x on 2017/3/24.
 */
public class CodeTemplateManager {

    private static CodeTemplateManager instance;

    private GroupTemplate groupTemplate;
    private String templatePath = null;


    private CodeTemplateManager() {
    }

    public static CodeTemplateManager getInstance() {
        if (instance == null) {
            synchronized (CodeTemplateManager.class) {
                if (instance == null) {
                    instance = new CodeTemplateManager();
                    instance.load();
                }
            }
        }
        return instance;
    }

    private void load() {
        File file = new File("");
        try {
            ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("template/");
//            templatePath = file.getCanonicalPath()/*System.getProperty("user.dir")*/ + File.separator + "target\\classes\\template";
//            System.out.println("Template files directory:" + templatePath);
//            FileResourceLoader resourceLoader = new FileResourceLoader(templatePath, "utf-8");
            Configuration cfg = Configuration.defaultConfiguration();
            groupTemplate = new GroupTemplate(resourceLoader, cfg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Template getTemplate(String templateName) {
        Objects.requireNonNull(groupTemplate);
        Template template = groupTemplate.getTemplate("/" + templateName);
        return template;
    }

}

核心为创建GroupTemplate

package com.a.b.c.d.code;

import com.sun.javafx.util.TempState;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.FileResourceLoader;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * Created by xx on 2017/3/24.
 */
public class MainTest {
    @Test
    public void test1() throws IOException {
        Template t=CodeTemplateManager.getInstance().getTemplate("hello,${attr}");
        t.binding("attr", "beetl");
        String str = t.render();
        System.out.println(str);
    }
    @Test
    public void test2() throws IOException{
        Template t=CodeTemplateManager.getInstance().getTemplate("/temp.txt");
        t.binding("attr", "gender");
        t.binding("type", "int");
        String str = t.render();
        System.out.println(str);
    }
    @Test
    public void test3() throws IOException{
        Template t=CodeTemplateManager.getInstance().getTemplate("/temp.txt");
        t.binding("attr", "persons");
        t.binding("type", "List<String>");
        String str = t.render();
        System.out.println(str);
    }
    @Test
    public void test4() throws IOException{
        Template t=CodeTemplateManager.getInstance().getTemplate("/temp.txt");
        t.binding("attr", "persons");
        t.binding("type", "List<String>");
        t.binding("returnType","ABuilder");
        t.binding("impl",true);
        String str = t.render();
        System.out.println(str);
    }
}

可以直接传入模板引擎的表达式或模板文件获取模型对象,再绑定数据,最终输出需要的文本字串。





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值