Passing HTML to template using Flask/Jinja2

后端 未结 5 1436
灰色年华
灰色年华 2020-11-21 07:44

I\'m building an admin for Flask and SQLAlchemy, and I want to pass the HTML for the different inputs to my view using render_template. The templating framework

5条回答
  •  忘掉有多难
    2020-11-21 08:05

    Some people seem to turn autoescape off which carries security risks to manipulate the string display.

    If you only want to insert some linebreaks into a string and convert the linebreaks into
    , then you could take a jinja macro like:

    {% macro linebreaks_for_string( the_string ) -%}
    {% if the_string %}
    {% for line in the_string.split('\n') %}
    
    {{ line }} {% endfor %} {% else %} {{ the_string }} {% endif %} {%- endmacro %}

    and in your template just call this with

    {{ linebreaks_for_string( my_string_in_a_variable ) }}
    

提交回复
热议问题