슬기로운슬기
Published 2023. 5. 17. 21:58
TIL3일차: Jinja2_template study

Jinja2? 

- 실재하는 파일에 어떤 변수들을 집어넣어서 약간 변형시켜서 html로 만들어주는 엔진이다.

- Jinja2 is a templating engine for Python 

Jinja2가 flask에서 하는 일

- flask로 웹서버를 구축

- /introDetail/<name>이라는 url로 들어오면, detail.html라는 template를 랜더링해서 보여준다.

@app.route('/introDetail/<name>', methods=['GET', 'POST'])
def introDetail(name):

    user = db.introduce.find_one({'name': name})

    return render_template('detail.html', temp=user)

<detail.html> 

- {% %}

- 해당 문서의 필요한 부분들을 변경해주고, 웹브라우저가 인식할 수 있는 html로 변환해주는 기능을 수행 

- 조건문과 반복문 작성방법:{%if 조건식 %} ....{% endif%}   {% for 요소 in 리스트 %} ... {% endfor %}

ex) {%if temp.picture %} ....{% endif%} 

- 변수 처리: {{변수}}

ex) {{temp.name}}

      <div class="row justify-content-center myintro" id="cards-box">
          {% if temp.picture %}
              <img class="myimg" src="{{ temp.picture }}" alt="..."
              style="width: 500px">
          {% endif %}
          <div class="card mb-3" style="width: 600px ">        
              {% if temp.name %}    
                  <div class="card-header bg-transparent my">{{ temp.name }}</div>
              {% endif %}
              <div class="card-body">
                  {% if temp.intro %} 
                      <p class="card-text">{{ temp.intro}}</p>
                  {% endif %}
                  {% if temp.mbti %} 
                      <p class="card-text">MBTI : {{ temp.mbti }}</p>
                  {% endif %}
                  {% if temp.blog %} 
                      <p class="card-text">blog 주소 : {{ temp.blog }}</p>
                  {% endif %}
                  {% if temp.good %} 
                      <p class="card-text">{{ temp.good }}</p>
                  {% endif %}
              </div>

일단은 우리 조장님이 쓰신 코드 가져와서 해석할려고 노력했다... Jinja2 조금더 공부해서 블로그에 더 채워넣을 예정 ㅠ 

 

반응형
profile

슬기로운슬기

@스를기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!