简单灵活的代码生成模板引擎
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
Built Distribution
File details
Details for the file simpletemplate-0.0.1b1.tar.gz
.
File metadata
- Download URL: simpletemplate-0.0.1b1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b16048435cf77102405892c20b13f112606de0de30f810c3b1fa89a26bc01263 |
|
MD5 | 51f1b2f664625a6bbe1a6d768d43ea2d |
|
BLAKE2b-256 | b552c3551999d5c3ec2ef656ac4c67b171f84d79d1cb4fb47aefc005e2dd286c |
File details
Details for the file simpletemplate-0.0.1b1-py3-none-any.whl
.
File metadata
- Download URL: simpletemplate-0.0.1b1-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26034edd5ad1b28c0549648917ed46f674dba7beb98778a094f33e66199a74e0 |
|
MD5 | ed5dfd24bf9f1fad800df33179fdd81e |
|
BLAKE2b-256 | 30b35b98360e86d16a836ff1d9a520629d419276da8ff621bba92f11f61e3b23 |