utils.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Zapper 作者: maxpoint 项目源码 文件源码
def render_template(template_name, **kwargs):
    """
    Simple utility function to render out a specified template, using
        **kwargs to fill in variables.

    Args:
        template_path (str): The directory where we can find the template.
        template_name (str): The actual name of the template we want to
                                render.
        **kwargs (dict):     Key Value pairs of any variables we want rendered
                                out into the template.

    Raises:
        AncillaryFileNotFound:      If we cannot find the template.
        AncillaryUndefinedError:    If we run across an undefined variable.

    """

    # Attempt to load a Tempalte file from within the 'Zapper' package
    #   and raise an IOError if I'm unable to find it.
    try:
        env = Environment(loader=PackageLoader('zapper', 'templates'))
        template = env.get_template(template_name)
    except TemplateNotFound:
        raise IOError('Unable to find template {} in zapper!'
                      .format(template_name))

    # Attempt to render our template, and raise a Value Error if we
    #   run into any undefined variables.
    try:
        template_data = template.render(**kwargs)
    except UndefinedError as e:
        raise ValueError('Undefined variable found in {}! Error: {}'
                         .format(template_name, e))

    return template_data
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号