PebbleTemplates 模版引擎使用

PebbleTemplates 是一个类似jinja 的java 模版引擎(受twig启发)支持的能力还是很多的,而且扩展性很不错,比如模版资源加载,模版缓存
自定义扩展。。。。,是一个值得使用的模版引擎

一个s3 loader

  • 参考代码
    基于了minio client 通过配置指定
 
public class S3Loader implements Loader<String> {
 
    private MinioClient minioClient;
    private String bucket;
    public S3Loader(MinioClient minioClient,String bucket) {
        this.minioClient = minioClient;
        this.bucket=bucket;
    }
 
    @Override
    public Reader getReader(String cacheKey) {
        Reader targetReader = null;
        try {
            GetObjectResponse response = this.minioClient.getObject(GetObjectArgs.builder().bucket(this.bucket).object(cacheKey).build());
            targetReader = new InputStreamReader(response);
        } catch (ErrorResponseException e) {
            throw new RuntimeException(e);
        } catch (InsufficientDataException e) {
            throw new RuntimeException(e);
        } catch (InternalException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (InvalidResponseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (ServerException e) {
            throw new RuntimeException(e);
        } catch (XmlParserException e) {
            throw new RuntimeException(e);
        }
        return targetReader;
    }
 
    @Override
    public void setCharset(String charset) {
 
    }
 
    @Override
    public void setPrefix(String prefix) {
 
    }
 
    @Override
    public void setSuffix(String suffix) {
 
    }
 
    @Override
    public String resolveRelativePath(String relativePath, String anchorPath) {
        return null;
    }
 
    @Override
    public String createCacheKey(String templateName) {
        return templateName;
    }
 
    @Override
    public boolean resourceExists(String templateName) {
        return true;
    }
}
集成s3 loader
  • 入口代码
public class App {
    public static void main(String[] args) throws IOException {
        MinioClient minioClient =
                MinioClient.builder()
                        .endpoint("http://127.0.0.1:9000")
                        .credentials("minio", "minio123")
                        .build();
        // 推荐基于DelegatingLoader 进行加载,可以支持多种loader
        DelegatingLoader delegatingLoader = new DelegatingLoader(Arrays.asList(new S3Loader(minioClient,"demoapp")));
        PebbleEngine engine = new PebbleEngine.Builder().loader(delegatingLoader).build();
        PebbleTemplate compiledTemplate = engine.getTemplate("home.html");
        Map<String, Object> context = new HashMap<>();
        context.put("name", "Mitchell");
        Writer writer = new StringWriter();
        compiledTemplate.evaluate(writer, context);
        String output = writer.toString();
        System.out.println(output);
    }
}
  • 参考模版内容

 

 

 


home.html

 
{% extends "100/base.html" %}
 
{% block title %} Home {% endblock %}
 
{% block content %}
<h1> Home </h1>
<p> Welcome to my home page. My name is {{ name }}.</p>
{% endblock %}

base.html

<html>
<head>
    <title> 自定义web page {% block title %}{% endblock %}</title>
</head>
<body>
<div id="content">
    {% block content %}{% endblock %}
</div>
 
<div id="footer">
    {% block footer %}
    Copyright 2018
    {% endblock %}
</div>
</body>
</html>
 
 
  • 运行效果

 

 

 

s3 参考玩法

 

 

 


简单说明: 通过提供模版管理能力(基于s3 api + 包装),可以将自定义模版以及公共模版访问s3 bucket 不同目录中,模版管理中进行模版片段的组合(可以支持不同的模版入口定义),对于业务为了提升性能,可以添加cache 能力(可以基于redis 以及 local cache 进行多级处理)

说明

PebbleTemplates 的扩展能力很方便,利用PebbleTemplates 灵活的扩展能力,我们可以很好的设计一个基于模版的系统,对于jinja 社区也包含了一个开源的实现(jinjava)当然也包括了支持自定义扩展

参考资料

https://min.io/docs/minio/linux/developers/java/API.html
https://github.com/PebbleTemplates/pebble
https://github.com/rongfengliang/pebbletemplates-learning
https://palletsprojects.com/p/jinja/
https://twig.symfony.com/
https://pebbletemplates.io/
https://github.com/HubSpot/jinjava

posted on 2023-03-16 13:06  荣锋亮  阅读(155)  评论(0编辑  收藏  举报

导航