๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

[Step by Step Tutorial] 09. Collections

Collections

๋งŒ์•ฝ์— ๋ธ”๋กœ๊ทธ์˜ ์ €์ž๋“ค ๊ฐ๊ฐ์„ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ์†Œ๊ฐœ๋ง๊ณผ ๊ทธ๋“ค์˜ ํฌ์ŠคํŠธ๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ๋Š”์ €์ž๋“ค๋งŒ์˜ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

์ด๊ฒƒ์„ ์œ„ํ•ด์„œ ์ปฌ๋ ‰์…˜(Collection)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ปฌ๋ ‰์…˜์€ ์ฝ˜ํ…์ธ ๋ฅผ ๋‚ ์งœ๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๋ฉด ํฌ์ŠคํŠธ์™€ ์œ ์‚ฌํ•˜๋‹ค.


๊ตฌ์„ฑ#


์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ Jekyll์—๊ฒŒ _config.yml ์„ ํ†ตํ•ด ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๋จผ์ € _config.yml ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

collections:    authors

_config.yml ํŒŒ์ผ์„ ์ˆ˜์ •ํ•œ ํ›„์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Jekyll ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค. Ctrl + C ๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋ฅผ ๋ฉˆ์ถ”๊ณ  jekyll serve ๋กœ ์‹œ์ž‘ํ•  ์ˆ˜์žˆ๋‹ค.


์ €์ž ์ถ”๊ฐ€ํ•˜๊ธฐ#


์ปฌ๋ ‰์…˜์— ์žˆ๋Š” ์•„์ดํ…œ๋“ค(๋ฌธ์„œ๋“ค)์€ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” _*collection_name* ๋””๋ ‰ํ† ๋ฆฌ์— ๋งŒ๋“ค๋ฉด ๋œ๋‹ค. ์ €์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ปฌ๋ ‰์…˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” _authors ๋กœ ๋งŒ๋“ค๋ฉด ์ ์ ˆํ•  ๊ฒƒ์ด๋‹ค.

์ €์ž์— ๊ด€ํ•œ ๋ฌธ์„œ๋“ค์„ ๋งŒ๋“ค์–ด๋ณด์ž.

_authors/jill.md

---short_name: jillname: Jill Smithposition: Chief Editor---Jill is an avid fruit grower based in the south of France.

_authors/ted.md:

---short_name: tedname: Ted Doeposition: Writer---Ted has been eating fruit since he was baby.

Staff ํŽ˜์ด์ง€#


๋ชจ๋“  ์ €์ž๋“ค์„ ๋ฆฌ์ŠคํŠธํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด์ง€๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž. Jekyll์€ ์ปฌ๋ ‰์…˜์„ site.authors ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— staff.html ์„ ๋งŒ๋“ ๋‹ค.

---layout: defaulttitle: Staff---
<h1>Staff</h1>
<ul>    {% for author in site.authors %}    <li>        <h2>{{ author.name }}</h2>        <h3>{{ author.position }}</h3>        <p>{{ author.content | markdownify }}</p>    </li>    {% endfor %}</ul>

content๊ฐ€ Markdown์œผ๋กœ ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— markdownify filter๋ฅผ ์ ์šฉํ•ด์„œ ์‹คํ–‰ํ•ด์•ผํ•œ๋‹ค. ์ด๊ฒƒ์€ ๋ ˆ์ด์•„์›ƒ์˜ {{ content }} ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ž๋™์ ์œผ๋กœ ๋œ๋‹ค. (์•„์ง ์ดํ•ดํ•˜์ง€ ๋ชปํ•จ)

์ด์ œ Staff ํŽ˜์ด์ง€ ์—ญ์‹œ ๋„ค๋น„๊ฒŒ์ด์…˜์— ๋‚˜ํƒ€๋‚˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— _data/navigation.yml ์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

- name: Home  link: /- name: About  link: /about.html- name: Blog  link: /blog.html- name: Staff  link: /staff.html

๊ธฐ๋ณธ์ ์œผ๋กœ(default ๊ฐ’์œผ๋กœ) ์ปฌ๋ ‰์…˜์€ ๋ฌธ์„œ์˜ ํŽ˜์ด์ง€๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋งŒ์•ฝ ๊ฐ ์ €์ž ๋งˆ๋‹ค ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด _config.yml ํŒŒ์ผ์„ ์‚ด์ง ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

collections:    authors:        output: true

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด author.url ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŽ˜์ด์ง€์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

staff.html ์— ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž

---layout: defaulttitle: Staff---
<h1>Staff</h1>
<ul>    {% for author in site.authors %}    <li>        <h2><a href="{{ author.url }}">{{ author.name }}</a></h2>        <h3>{{ author.position }}</h3>        <p>{{ author.content | markdownify }}</p>    </li>    {% endfor %}</ul>

ํฌ์ŠคํŠธ์™€ ๊ฐ™์ด authors ๋ฅผ ์œ„ํ•œ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

_layouts/author.html ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งŒ๋“ ๋‹ค.

---layout: default---
<h1>{{ page.name }}</h1><h2>{{ page.position }}</h2>
{{ content }}

Front matter defaults#


์ด๋ ‡๊ฒŒ ๋งŒ๋“  author ๋ ˆ์ด์•„์›ƒ์„ author๋“ค, jill.md ์™€ ted.md ์˜ front matter ์— ํ‘œ์‹œํ•ด์ฃผ์–ด์•ผ ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ ๊ณ„์† ๋ฐ˜๋ณต๋˜๋Š” ์ž‘์—…์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ์ž๋™์ ์œผ๋กœ ํฌ์ŠคํŠธ๋“ค์€ post ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜๊ณ , author๋“ค์€ author ๋ ˆ์ด์•„์›ƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๊ฒƒ์€ _config.yml ์—์„œ front matter defaults๋ฅผ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์„ ์ ์šฉํ•  scope ๋ฅผ ์ •ํ•˜๊ณ , ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋“ค์–ด๊ฐˆ front matter๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

๋‹ค์Œ์€ _config.yml ์˜ˆ์‹œ์ด๋‹ค.

collections:  authors:    output: true
defaults:  - scope:      path: ''      type: 'authors'    values:      layout: 'author'  - scope:      path: ''      type: 'posts'    values:      layout: 'post'  - scope:      path: ''    values:      layout: 'default'

์ด์ œ ๋ชจ๋“  page ๋“ค๊ณผ post ๋“ค์˜ front matter์—์„œ layout ์— ๊ด€ํ•œ ๋ถ€๋ถ„์„ ๋‹ค ์ง€์›Œ๋„ ๋œ๋‹ค.

_config.yml ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๋ฉด Jekyll ์„œ๋ฒ„๋ฅผ ์žฌ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹ฌํ•˜๋ผ.


List author's posts#


๊ฐ author๊ฐ€ ์“ด ํฌ์ŠคํŠธ๋“ค์„ ๊ทธ๋“ค์˜ ํŽ˜์ด์ง€์— ํ•„ํ„ฐ๋งํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด๊ฒƒ์„ํ•˜๊ธฐ ์œ„ํ•ด์„œ, author์˜ short_name ๊ณผ post์˜ author ๊ฐ€ ๋งค์น˜๋  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

_layouts/author.html ์„ ๋ณด์ž.

---layout: default---
<h1>{{ page.name }}</h1><h2>{{ page.position }}</h2>
{{ content }}
<h2>Posts</h2><ul>    {% assign filtered_posts = site.posts | where: 'author', page.short_name %} {%    for post in filtered_posts %}    <li><a href="{{ post.url }}">{{ post.title }}</a></li>    {% endfor %}</ul>

| where: 'author', page.short_name ์ด ํ•„ํ„ฐ. (์ดํ›„์— ๋” ๊ณต๋ถ€)


Link to authors page#


๊ฐ๊ฐ์˜ ํฌ์ŠคํŠธ๋Š” author์˜ ํŽ˜์ด์ง€๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งํฌ๋ฅผ ๊ฐ€์ ธ์•ผํ•  ๊ฒƒ์ด๋‹ค.

์œ„์—์„œ ํ•œ ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ํ•„ํ„ฐ๋ง ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•ด์„œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

_layouts/post.html

---layout: default---
<h1>{{ page.title }}</h1>
<p>    {{ page.date | date_to_string }} {% assign author = site.authors | where:    'short_name', page.author | first %} {% if author %} -    <a href="{{ author.url }}">{{ author.name }}</a>    {% endif %}</p>
{{ content }}

http://localhost:4000์— ์ ‘์†ํ•ด์„œ staff page๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ํฌ์ŠคํŠธ์˜ author๋กœ ๊ฐ€๋Š” ๋งํฌ๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.