• Jinja2.template渲染两种常见用法


    template是字符串

    >>> import os, jinja2
    >>> html = u'''<h1>username:</h1>{{ name }}
    ... <h1>age:</h1>{{ age }}'''
    >>> _t = jinja2.Template(html, trim_blocks=True)
    >>> _text = _t.render(name='wyett',age=20)
    >>> _text = _text.encode('utf-8')
    >>> print _text
    <h1>username:</h1>wyett
    <h1>age:</h1>20
    >>> 

    template存放在json文本中

    <h1>username:</h1>{{ name }}
    <h1>age:</h1>{{ age }}

    然后我们需要定义一个dict

    ds_conf = {"name" : "wyett",
                     "age" : 20
                    }

    渲染脚本

    TemplateLoader = jinja2.FileSystemLoader(os.path.abspath('.'))
    TemplateEnv = jinja2.Environment(loader=TemplateLoader)
    template = TemplateEnv.get_template('html.json.j2')
    dsconf = template.render(ds_conf)
    print dsconf
  • 相关阅读:
    API之绘图函数
    DotNetCharting
    Rule engine
    股票
    PHP
    获取Excel.dll
    Excel develop
    几个不错的开源的.net界面控件[转贴]
    关于EXCEL文件双击打不开的问题
    Regular expression
  • 原文地址:https://www.cnblogs.com/wyett/p/10045950.html
Copyright © 2020-2023  润新知