render.py 758 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : render.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2023/4/19
  6. import re
  7. from jinja2 import Environment, Template
  8. def to_lower_camel_case(x):
  9. """转小驼峰法命名:下划线转驼峰且首字母小写"""
  10. s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
  11. return s[0].lower() + s[1:]
  12. def render_template_string(source: str, **context):
  13. # 构造环境
  14. env = Environment()
  15. # 添加一个过滤器
  16. env.filters['to_lower_camel_case'] = to_lower_camel_case
  17. # 获取模板
  18. template: Template = env.from_string(source)
  19. # 渲染
  20. view = template.render(**context)
  21. return view