skip to Main Content

I have no idea how, but would like to know if it is possible to change the file in the jinja2 include statement.

such as:
{% include file.name %} to {% include file1.name %}

Since the file.name is in include parentheses, I could not use {{ file.name }} to achieve this.

Thought maybe I could use something like jquery,

$(".btnt").click(function(){
    $("section:fourth").replaceWith("{% include 'file1.name' %}");}

maybe initiate with button click, would this have to be on same page. I tend to use flask python for most projects.

2

Answers


  1. Chosen as BEST ANSWER

    Actually used jinja2 to read variable to decide which file.name to use

    {% if var %}
    {% include file.name %}
    {% else %}
    {% include file1.name %}
    {% endif %}
    

    This checks var for true, set from flask render template arguments. If true uses file.name, if false uses file1.name


  2. Hey there is a technique which i use to transfer server side rendering to client side. it is basically returing json to the template itself directly. Like this:

    app.py

    from flask import Flask,render_template
    import json
    app = Flask(__name__)
    
    @app.route("/")
    def index():
        return render_template("a.html",dumps=json.dumps,data={"hello":"hello"})
    

    a.html

    <script>
        var jsonString = '{{dumps(data)}}'
        var data = JSON.parse(jsonString)
    </script>
    

    This code should help you to use jinja in your scripts. But I recommend you to use APIs that return json data instead of this. For that you can use $.getJSON method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search