templates.py 文件源码

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

项目:foremast 作者: gogoair 项目源码 文件源码
def get_template_object(template_file=''):
    """Retrieve template.

    Args:
        template_file (str): Name of template file.

    Returns:
        jinja2.Template: Template ready to render.

    Raises:
        AssertionError: Configured path for templates does not exist.
        :obj:`foremast.exceptions.ForemastTemplateNotFound`: Requested template
            is not available.

    """
    jinja_lst = []

    if TEMPLATES_PATH:
        external_templates = os.path.expanduser(TEMPLATES_PATH)
        assert os.path.isdir(external_templates), 'External template path "{0}" not found'.format(external_templates)
        jinja_lst.append(external_templates)

    jinja_lst.append(LOCAL_TEMPLATES)

    jinjaenv = jinja2.Environment(loader=jinja2.FileSystemLoader(jinja_lst))

    try:
        template = jinjaenv.get_template(template_file)
    except jinja2.TemplateNotFound:
        message = 'Unable to find template "{template_file}" in paths {paths}'.format(
            template_file=template_file, paths=jinjaenv.loader.searchpath)
        LOG.error(message)
        raise ForemastTemplateNotFound(message)

    return template
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号