skip to Main Content

I found out that these {{}} can directly display variables inside of html, no js required, when built up like the following:

given the URL https://myurl.com/link?key=HFEQ67fWE2

<p>{{ key }}</p> will set the <p>.textContent to the value of the arg key HFEQ67fWE2

Can someone please help me how to use this feature consistently?

I was trying to understand how exactly to use this feature because after a bit more work on my project it suddenly stopped working.

2

Answers


  1. They have no meaning in HTML.

    Various template languages (which can be used to output HTML) use them, including Jinja2 (the default template language for Flask, which you tagged).

    The documentation is here.

    {{ ... }} for Expressions to print to the template output

    Login or Signup to reply.
  2. The {{ }} in Flask are used for template variable interpolation.

    Flask uses the Jinja2 template engine under the hood. Jinja2 uses {{ }} as the syntax for variables and expressions that get replaced with values when a template is rendered.

    In a Flask view function, you can pass variables to a template using the render_template() method. For example:

    @app.route('/') 
    def index():
      name = 'John'
      return render_template('index.html', name=name)
    

    Then in the template (index.html), you can use {{ name }} to insert the value of the name variable:

    <p>Hello {{ name }}!</p>
    

    When the template is rendered, {{ name }} will be replaced with ‘John’ in this case.

    However, double curly braces allow you to insert any variables, expressions, filters etc in a template.

    Check this out:

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