简单灵活的代码生成模板引擎
Project description
基于代码生成的简单模板引擎
install
pip3 install simpletemplate
from simpletemplate import Template
Template("Hello, {{name}}!").render({'name': 'world'})
模板语法
%{fields = jclass.fields}%
private {{jclass.name}}(Builder builder) {
%{for field in fields:}%
this.{{field.name}} = builder.{{field.name}};
%{end}%
}
public static class Builder {
%{for field in fields:}%
private {{field.jtype}} {{field.name}} = null;
%{end}%
%{for field in fields:}%
%{if field.comment:}%
/**
* {{field.comment}}
*/
%{end}%
public Builder set{{field.name.title()}}({{field.jtype}} {{field.name}}) {
this.{{field.name}} = {{field.name}};
return this;
}
%{end}%
public {{jclass.name}} build() {
return new {{jclass.name}}(this);
}
}
测试代码
'''
interface
'''
def test_template():
class Obj:
pass
def jfield(jtype: str, name: str, *, modifier: str = '', init_value: str = '', comment: str = ''):
field = Obj()
field.modifier = modifier
field.jtype = jtype
field.name = name
field.initial_value = init_value
field.comment = comment
return field
data = Obj()
data.name = 'ShareConfig'
data.fields = []
data.fields.append(jfield('Tencent', 'tencent', modifier='private final'))
data.fields.append(
jfield('IWXApi', 'wxApi', modifier='private final', comment='wechat share'))
print(Template(TMPL_BUILDER).render({'jclass': data}))
if __name__ == '__main__':
test_template()
generated Java Code
private ShareConfig(Builder builder) {
this.tencent = builder.tencent;
this.wxApi = builder.wxApi;
}
public static class Builder {
private Tencent tencent = null;
private IWXApi wxApi = null;
public Builder setTencent(Tencent tencent) {
this.tencent = tencent;
return this;
}
/**
* wechat share
*/
public Builder setWxapi(IWXApi wxApi) {
this.wxApi = wxApi;
return this;
}
public ShareConfig build() {
return new ShareConfig(this);
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
simpletemplate-0.0.1b1.tar.gz
(3.3 kB
view hashes)
Built Distribution
Close
Hashes for simpletemplate-0.0.1b1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26034edd5ad1b28c0549648917ed46f674dba7beb98778a094f33e66199a74e0 |
|
MD5 | ed5dfd24bf9f1fad800df33179fdd81e |
|
BLAKE2b-256 | 30b35b98360e86d16a836ff1d9a520629d419276da8ff621bba92f11f61e3b23 |