Search code examples
pythonflaskjinja2

Conditional rendering of HTML segment using render_template


I am calling render_template from a couple of different places, and I'd like to control whether I render certain HTML segments, depending on where I'm calling from.

For example:

render_template('index.html', form=form, show_results=1)

I intended to use the show_results bool to flag whether the optional segment should be rendered or not. However, I'm missing what wrapper I should have in the optional delimited portion of the HTML code to control whether the segment should be rendered or not. How can I accomplish this?


Solution

  • Use an if block:

    {% if show_results %}
        show the results
    {% endif %}