Passing variables with include in Jinja2 template

I have an html file containing the following piece of code in the Jinja2 template language.

{% block head %}
    {{ super() }}
    {% with textarea="txtMessageContent" %}
        {% include "vvv/vvv/TinyMCE.html" %}
    {% endwith %}
{% endblock %}

I am trying to pass the textarea variable to the TinyMCE.html template which contains the following code.

{% block InitTinyMCE %}
<script src="https://x.x.x/x/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="/static/js/tinymce/tinymce.min.js"></script>
<script type="application/javascript">
alert(textarea);
    $(document).ready(function (textarea){
        tinymce.init({
            selector: textarea,
            menubar: true,
            plugins: "hr code preview",
            content_css:"https://x.x.x/x/css/backend-boostrap.css",
            remove_linebreaks: false,
            convert_newlines_to_brs: true
        });
    });
</script>
{% endblock %}

When I load the page, I see that the variable does not go through t. I can confirm this due to the following error.

textarea not defined.

+4
source share
1 answer

You need to add double curly braces around variables in Jinja

{% block InitTinyMCE %}
<script src="https://x.x.x/x/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript" src="/static/js/tinymce/tinymce.min.js"></script>
<script type="application/javascript">
    $(document).ready(function (){
        tinymce.init({
            selector: "{{ textarea }}",
            menubar: true,
            plugins: "hr code preview",
            content_css:"https://x.x.x/x/css/backend-boostrap.css",
            remove_linebreaks: false,
            convert_newlines_to_brs: true
        });
    });
</script>
{% endblock %}
0
source

Source: https://habr.com/ru/post/1625301/


All Articles