개미들을 위한 퀀트 자동 매매 프로그램 개발자 블로그입니다. 프로그래밍과 퀀트 투자를 합니다.

방구석퀀트

네이버카페 : 방구석 퀀트 놀러오세요^^ https://cafe.naver.com/conerquant 가입해 주세요.

2019년 9월 3일 화요일

python flask jinja2 if 문에서 변수 다루기

jinja2 에서 if 문을 사용할 때 변수에 대한 조건문을 설정하고 싶을 때는 아래와 같은 문법으로 설정하면 된다.
(원문: https://www.shellhacks.com/jinja2-check-if-variable-empty-exists-defined-true/ )

1. 변수가 선언 되어 있는지(True) 안되어 있는지(False)

{% if variable is defined %}
    variable is defined
{% else %}
    variable is not defined
{% endif %}

2. 선언된 변수에 값이 있는지 없는지

{% if variable|length %}
    variable is not empty
{% else %}
    variable is empty
{% endif %}

3. 선언된 변수가 참인지 거짓인지

{% if variable is sameas true %}
    variable is true
{% else %}
    variable is not true
{% endif %}

4. 변수가 선언되고 값이 있는지

{% if variable is defined and variable|length %}
    variable is defined and not empty
{% else %}
    variable is not defined or empty
{% endif %}

5. 변수가 선언되고 값이 참인지

{% if variable is defined and variable is sameas true %}
    variable is defined and true
{% else %}
    variable is not defined or not set to true
{% endif %}

댓글 1개:

가장 많이 본 글