Jinja is a flask template engine used for dynamic control of html.

{ { } }

Separator representing a variable.

app.py


from flask import Flask, render_template

app = Flask(__name__)

@app.route('/hello/<user>')                               # 127.0.0.1/hello/ input u want
def hello_name(user):
    return render_template('hello.html', name = user)

    
if __name__ == '__main__':
    app.run(debug=True)

If Python sends it as above, you can receive it as below in html.

hello.html


<!doctype html>
<html>
  <body>
    <h1>Hello { { name } }!</h1>
  </body>
</html>

It is expressed as { { value } }.

Run and Result

image

{ % % }

Separators that can use for, if

Example) if

add.py


from flask import Flask, render_template

app = Flask(__name__)

@app.route('/marks/<int:score>')
def marks(score):
   return render_template('marks.html', marks = score)  # Send Value
    
if __name__ == '__main__':
    app.run(debug=True)


marks.html


<!doctype html>
<html>
  <body>
    { % if marks > 50 % }
    <h1> 다음 단계로 이동할 수 있음 </h1>
    { % else % }
    <h1> 다음 단계로 이동할 수 없음 </h1>
    { % endif % }
  </body>
</html>

Run and Result

image

Example) for

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/result')
def result():
    dict = {'phy': 50, 'che': 60, 'maths': 70}
    return render_template('result.html', result=dict)

if __name__ == '__main__':
    app.run(debug=True)

result.html


<!doctype html>
<html>
<body>
  <table border=1>
    { % for key, value in result.items() % }
    <tr>
      <th> { { key } } </th>
      <td> { { value } } </td>
    </tr>
    { % endfor % }
  </table>
</body>
</html>

Run and Result

image