๐ Template Language
๐ฅ Template ํ๊ทธ : {% ๋ฌธ๋ฒ %}
๐ฅ Template ๋ณ์ : {{ ๋ณ์ }}
๐ฅ Template ํํฐ : {{ ๋ณ์|์ต์
 }}
- Template Language๋ก๋ Template ํ๊ทธ, Template ๋ณ์, Template ํํฐ๊ฐ์๋๋ฐ์,, ์ด Template Language ํตํด python ์ฝ๋๋ฅผ html ํฌํ๋ฆฟ ์์์ ํ์ฉํ  ์ ์์ด์!
 
- Template ํ๊ทธ๋ html๋ฌธ์์์ python code๋ฅผ ์ฌ์ฉํ  ์ ์๊ฒํ๋ ๋ฌธ๋ฒ์ด๊ณ , Template ๋ณ์๋ก๋ view๋ฅผ ํตํด ์ ๋ฌํ ๋ณ์๋ฅผ ํ
ํ๋ฆฟ ์์์ ๋ค๋ฃฐ ์ ์์ด์.
 
- Template ํํฐ๋ ํ
ํ๋ฆฟ ๋ณ์ ์์ ๋ด๊ธด ๋ฌธ์์ด์ด๋ ๋ฆฌ์คํธ ๋ฑ์ ์ฝ๊ฒ ๋ค๋ฃฐ ์ ์๊ฒํด์ฃผ๋ ๊ธฐ๋ฅ์ ํ๊ณ  ์์ด์.
 
- ์ฐธ๊ณ  : https://docs.djangoproject.com/en/3.2/ref/templates/builtins/
 
2. Template ํ๊ทธ : {% ๋ฌธ๋ฒ %}
1) {% for  in  %} ~ {% endfor %}
- ํฌํ๋ฆฟ ํ๊ทธ์ ํตํด python ๋ฌธ๋ฒ์ if, else, for๋ฌธ์ ์ฌ์ฉํ  ์ ์๊ณ ,, extends, block, include ๋ฑ์ ํตํด ๋ค๋ฅธ ํ
ํ๋ฆฟ์ ์์๋ฐ๊ฑฐ๋ ํฌํจ์ํฌ ์ ์๋ต๋๋ค.
 
- {{forloop.counter}}๋ ๋ฐ๋ณต๋ฌธ์์์ ์ฌ์ฉํ๋ฉด ๋ฐ๋ณต ํ์๋งํผ countํด์ค๋๋ค.
- ๐ {{forloop.counter}} : 1๋ถํฐ ๋ฐ๋ณตํ์๋งํผ count
 
- ๐ {{forloop.counter0}} : 0๋ถํฐ ๋ฐ๋ณตํ์๋งํผ count
 
- ๐ {{forloop.revcounter}} : ์ ์ฒด ๊ธธ์ด๋ก ์์ํด์ 1๊น์ง count
 
- ๐ {{forloop.revcounter0}} : ์ ์ฒด ๊ธธ์ด๋ก ์์ํด์ 0๊น์ง count  
 
 
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from .models import Notice 
def sample(request):
    data = Notice.objects.all() 
    return render(request, 'sample.html', {'value':data}) ๐ ๋์
๋๋ฆฌ ํํ๋ก ์ ๋ฌ
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    <p>{{value}}</p>
    {% for i in value %}
    <div>๋ฒํธ : {{forloop.counter}} ๋ฒ์งธ data</div>
    <div>์ ๋ชฉ : {{i.title}}</div>
    <div>์ถ์ฒ์ : {{i.likeCount}}</div>
    <div>์กฐํ์ : {{i.viewCount}}</div>
    <div>๋ด์ฉ : {{i.contents}}</div>
    <br />
    {% endfor %}
  </body>
</html>

2) {% if %} .. {% elif %} .. {% else %} .. {% endif %}
- ํฌํ๋ฆฟ ํ๊ทธ๋ฅผ ํตํด if, elif, else๋ฌธ๋ ์๋ ์ฒ๋ผ ์ฌ์ฉํ  ์ ์์ด์!
 
{%load static%}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    <p>{{value}}</p>
    {% for i in value %}
    <div>{{forloop.counter}} ๋ฒ์งธ data</div>
    <div>์ ๋ชฉ : {{i.title}}</div>
    <div>์ถ์ฒ์ : {{i.likeCount}}</div>
    <div>์กฐํ์ : {{i.viewCount}}</div>
    <div>๋ด์ฉ : {{i.contents}}</div>
    {% if i.viewCount > 10 %}
    <p>์กฐํ์๊ฐ 10์ด์ ์
๋๋ค.</p>
    {% elif i.viewCount > 5 %}
    <p>์กฐํ์๊ฐ 5์ด์ 10๋ฏธ๋ง ์
๋๋ค.</p>
    {% else %}
    <p>์กฐํ์๊ฐ 5๋ฏธ๋ง ์
๋๋ค.</p>
    {% endif %}
    <br />
    {% endfor %}
  </body>
</html>
3) {% with ๋ณ์๋ช
 = '๊ฐ' %}
- ํ
ํ๋ฆฟ ํ๊ทธ๋ฅผ ์ฌ์ฉํด html์์์ ๋ณ์๋ฅผ ์ ์ธํ ๋ค ์ฌ์ฉํ  ์๋ ์๋ต๋๋ค!
 
{%load static%}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    {% with value='hello Django'%}  ๐ "hello Django"๋ฅผ value๋ผ๋ ๋ณ์์ ๋ด์์ด์:)
    <h4>{{value}}</h4>
    {% endwith %}
  </body>
</html>

3. Template ๋ณ์ : {{ ๋ณ์ }}
- view์์ ๋ณ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด ํ
ํ๋ฆฟ์ผ๋ก ์ ๋ฌํ๋ฉด html ํ์ผ ์์์ ํด๋น ๋ณ์๋ฅผ ์ฝ๊ฒ ์ฌ์ฉํ  ์ ์์ด์. Dictํ, listํ ๋ชจ๋ ์ ๋ฌ ๊ฐ๋ฅ ํฉ๋๋ค.
 
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
def sample(request):
    d = {'name':'jaewon', 'age':99, 'location':'seoul', 'isman':True}
    l = [100,200,300]
    return render(request, 'sample.html', {'d':d, 'l':l})
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    <<p>
      <div>l ์ถ๋ ฅ : {{l}}</div>
      <div>l์ 0๋ฒ์งธ ๊ฐ์ : {{l.0}}</div>
      <div>l์ 1๋ฒ์งธ ๊ฐ์ : {{l.1}}</div>
      <div>l์ 2๋ฒ์งธ ๊ฐ์ : {{l.2}}</div>
    </p>
    <p>
      <div>d ์ถ๋ ฅ : {{d}}</div>
      <div>d์ key(name) ๊ฐ์ : {{d.name}}</div>
      <div>d์ key(age)) ๊ฐ์ : {{d.age}}</div>
      <div>d์ key(location) ๊ฐ์ : {{d.location}}</div>
      <div>d์ key(isman) ๊ฐ์ : {{d.isman}}</div>
    </p>
  </body>
</html>

4. Template ํํฐ : {{ ๋ณ์|์ต์
 }}
- ํ
ํ๋ฆฟ ํํฐ๋ ํ
ํ๋ฆฟ ๋ณ์์ ๊ฐ์ ๋ณํ์ํค๊ธฐ ์ํ ์ต์
์ผ๋ก ํ
ํ๋ฆฟ ๋ณ์์ ํ์ดํ(|)๋ฅผ ๋ถ์ธ ๋ค ์ฌ์ฉํด์.
 
- ์ฃผ๋ก ๋ฌธ์์ด์ด๋ ๋ฆฌ์คํธ ๋ฑ์ ๊ธธ์ด๊ตฌํ๊ธฐ, ์ฌ๋ผ์ด์ฑ, ํฉ์น๊ธฐ ๋ฑ์ ํ์ฉ๋ฉ๋๋ค.
- ๊ธธ์ด ๊ตฌํ๊ธฐ : ๐ {{ ํ
ํ๋ฆฟ๋ณ์ | length }}
 
- ๋ฆฌ์คํธ๋ฅผ ๋ฌธ์์ด๋ก ํฉ์น๊ธฐ : ๐ {{ ๋ณ์|join:"์ต์
" }}
 
- ์๋ฌธ์ & ๋๋ฌธ์๋ก ๋ณํ : ๐ {{ ๋ณ์|lower }} / ๐ {{ ๋ณ์|upper }}
 
- ๋ฆฌ์คํธ์์ randomํ๊ฒ ์ถ์ถ : ๐ {{ ๋ฆฌ์คํธํ ๋ณ์|random }}
 
- ๋ฌธ์์ด๋ก ์ฌ๋ผ์ด์ฑ : ๐ {{ ๋ณ์|slice:"n:n" }}  
 
 
- Django Templates์์ lorem ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ์์:)
- ๐ {{ lorem [count][mehod][random] }}
 
 
{%load static%}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Document</title>
  </head>
  <body>
    <h1>Template Language!</h1>
    {% with value='hello Django'%}
    <h4>{{value}}</h4>
    {% endwith %}
    {% lorem %}    
	{% lorem 3 p%} 
    {% lorem 2 w random %} {% lorem 5 w random %} 
  </body>
</html>