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 조금더 공부해서 블로그에 더 채워넣을 예정 ㅠ
반응형
'study' 카테고리의 다른 글
[KPT 회고] 6조 KPT 회고 - 오코완 (0) | 2023.08.14 |
---|---|
TIL 2일차 - GIT으로 버전 관리 (깃, 소스트리, 깃허브) (0) | 2023.05.16 |
TIL 1일차 - HTTP요청 방법 (0) | 2023.05.15 |
TIL - [multiple modal] (0) | 2023.05.09 |