Source code for inspirehep.utils.jinja2
# -*- coding: utf-8 -*-
#
# This file is part of INSPIRE.
# Copyright (C) 2014-2017 CERN.
#
# INSPIRE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# INSPIRE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with INSPIRE. If not, see <http://www.gnu.org/licenses/>.
#
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization
# or submit itself to any jurisdiction.
#
from __future__ import absolute_import, division, print_function
from flask import _request_ctx_stack
[docs]def render_template_to_string(input, _from_string=False, **context):
"""Render a template from the template folder with the given context.
Code based on
`<https://github.com/mitsuhiko/flask/blob/master/flask/templating.py>`_
:param input: the string template, or name of the template to be
rendered, or an iterable with template names the first one existing will be rendered
:param context: the variables that should be available in the context of the template.
:return: a string
"""
ctx = _request_ctx_stack.top
ctx.app.update_template_context(context)
if _from_string:
template = ctx.app.jinja_env.from_string(input)
else:
template = ctx.app.jinja_env.get_or_select_template(input)
return template.render(context)