# Hugo
# Quick Start
hugo new site ${PROJECT_NAME}
cd ${PROJECT_NAME}
git init
# ããŒãã®è¿œå
git submodule add ${GIT_REPO_OF_THEME} themes/${THEME_NAME}
# configãã¡ã€ã«ã§äœ¿çšããããŒããæå®ãã
echo 'theme = "ananke"' >> config.toml
# ã³ã³ãã³ãã®äœæ
hugo new posts/my-first-post.md
# ãµãŒããèµ·å
hugo server -D
hugo new
ã§èšäºãäœæãããšãã©ããèšäºãšããŠäœæãããïŒfontmatter ã®draft
ã true
ã«ãªãïŒã
ãã©ããèšäºã¯ãhugo server
ã§-D
ãªãã·ã§ã³ãã€ããªããšè¡šç€ºãããªãã®ã§æ³šæããã
# Go Template
ãã¡ã (opens new window)ãåç §
# Config
baseURL = "https://yoursite.example.com/"
footnoteReturnLinkContents = "â©"
title = "My Hugo Site"
[params]
AuthorName = "Jon Doe"
GitHubUser = "spf13"
ListOfFoo = ["foo1", "foo2"]
SidebarRecentLimit = 5
Subtitle = "Hugo is Absurdly Fast!"
[permalinks]
post = "/:year/:month/:title/"
詳现ã¯å ¬åŒããã¥ã¡ã³ã (opens new window)ãåç §
# params
params ã®å
容ã¯ã.Site.Params
ãšããŠãã³ãã¬ãŒãå
ããå©çšã§ããã
# æ¥ä»ã®èª¿æŽ
æ¥ä»ãæçµç·šéæ¥ãå ¬ééå§æ¥ãå ¬éçµäºæ¥ãã©ã®ããã«èšå®ããããã«ã¹ã¿ãã€ãºã§ããã詳现ã¯ããã¥ã¡ã³ã (opens new window)ãåç §
# Blackfriday
ããã©ã«ãã® Markdown ããŒãµãŒã§ãã Blackfriday ã®èª¿æŽãã§ããã詳现ã¯ããã¥ã¡ã³ã (opens new window)ãåç §
# ç°å¢å€æ°ã§ Config ãèšå®ãã
env HUGO_TITLE="My Hugo Site" hugo
# CLI
# hugo
ãããžã§ã¯ãããã«ããpublic
ãã©ã«ãã«åºåããã
Hugo ã¯public
ãã©ã«ãå
ã®ãã¡ã€ã«ãåé€ããªãã®ã§ããã«ãã®åã«æåã§public
ãã©ã«ããåé€ããŠããããšã
äžèšã«è©²åœããã³ã³ãã³ãã¯ãã«ããããªãã®ã§æ³šæããã ãã®æåãå€æŽãããå Žåã¯ããã¥ã¡ã³ã (opens new window)ãåç §ããããšã
- front matter ã«
publishdate
ãèšå®ãããŠãã - front matter ã«
expirydate
ãèšå®ãããŠãã - front matter ã®
draft
ãtrue
ã«èšå®ãããŠãã
hugo
# hugo server
éçºçšã®ãµãŒããhttp://localhsot:1313
ã§ç«ã¡äžããã
hugo server -D
# Directory Structure
# archetypes
hugo new post/***
ãå®è¡ããéã® Front Matter ã®é圢ã管çããã
# assets
Hugo Pipes ã§åŠçããå¿
èŠã®ãããã¡ã€ã«ã管çããã.Permalink
åã¯.RelPermalink
ã䜿ãããŠãããã¡ã€ã«ã®ã¿ãpublic
ãã£ã¬ã¯ããªã«åºåãããïŒïŒ
# config.toml
Hugo ã® Config ãã¡ã€ã«
# content
å šãŠã®ã³ã³ãã³ãã管çããã
奜ããªã ããã¹ããããããã§ããããã ããcontent
ãã©ã«ãçŽäžã¯ç¹å¥ã§ãé
眮ã§ãããã¡ã€ã«ã¯æ¬¡ã®éãã
- _index.md ïŒãããããŒãžã«ãªãïŒãšé¢é£ããç»åãã¡ã€ã«ãªã©
- ãã©ã«ã
content
çŽäžã®ãã©ã«ãã¯ãconetnt section
ãšããŠèªèããããäŸãã°ãblog
,articles
, tutorials
ãšããã»ã¯ã·ã§ã³ãäœãããå Žåã¯ããã®ååã®ã€ãããã©ã«ããcontent
ãã©ã«ãçŽäžã«äœæããã
ããã©ã«ãã§ã¯ãåã»ã¯ã·ã§ã³ã®é
äžã«äœæãããèšäºã«ã¯ãã»ã¯ã·ã§ã³ã®ååïŒblog
ãªã©ïŒãContent Type
ãšããŠèªåçã«èšå®ããããContent Type
ã¯ãã©ã®ã¬ã€ã¢ãŠãã®éžæçã«äœ¿çšãããã
# data
ã³ã³ãã³ãã®äžã§ãjson, yaml, toml ãšãã£ãããŒã¿ãæ±ãå Žåãããã§ç®¡çããã Data templatesã䜿ãã°ãåçã«ããŒã¿ãååŸããããšãã§ããã
# layouts
ãã³ãã¬ãŒãã管çããããã³ãã¬ãŒãã®åœ¢åŒã¯ HTML ã§ãããäžèšã®çš®é¡ãããã
- list page templates
- homepage templates
- taxonomy templates
- partial templates
- single page templates
- ...and more
# static
images, CSS, Javascript ãšãã£ãéçã³ã³ãã³ãã管çããã
ãã®ãŸãŸpublic
ãã©ã«ãã®ã«ãŒãã«é
眮ãããã
# Resources
# Resouces ãšã¯
content
ãã©ã«ãå ã®å šãŠã®ãã¡ã€ã«ã¯Resources
ã«ãªããHugo ã MIME type ãèªèã§ããéããcontent
ãã©ã«ãçŽäžãsection
ã«ãªã
# å šãŠã® Resources ãäžèŠ§è¡šç€º
{{ range .Resources }}
<li><a href="{{ .RelPermalink }}">{{ .ResourceType }}</a></li>
{{ end }}
絶察ãã¹ãå¿
èŠãªã.Permalink
ãã€ããããšã
# Type ãæå®ããŠäžèŠ§è¡šç€º
ã¿ã€ãåã¯ãããŒãžã®å Žåã¯page
ããã®ä»ã®å Žå㯠image
ãjson
ãªã©ã® MIME ã¿ã€ãã§æå®ããã
{{ range .Resources.ByType "image" }}
{{ .ResourceType }}
{{ end }}
# ç¹å®ã®æ¡ä»¶ã«ããŠã¯ãŸã Resouces ã衚瀺
äŸïŒãã¡ã€ã«åãlogo
ã§å§ãŸããªãœãŒã¹ã衚瀺ãã
<!-- åœãŠã¯ãŸããã®å
šãŠãååŸ -->
{{ range .Resources.GetMatch "logo*" }}
{{ . }}
{{ end }}
<!-- åœãŠã¯ãŸãæåã®äžã€ã®ã¿ãååŸ -->
{{ $logo = .Resources.Match "logo*" }}
{{ $logo.ResourceType }}
# ã³ã³ãã³ãã衚瀺
Page Resources = ã«ã¬ã³ãããŒãžããèŠãä»ã® page, images, pdfs, etc...
{{ range .Resources.ByType "image" }}
{{ .Content }}
{{ end }}
# Themes
- ããŒãã®äžèŠ§ (opens new window)
- ããŒãããšã«èšå®æ¹æ³ãç°ãªããããREADME.md ãããèªãããš
# ã€ã³ã¹ããŒã«æ¹æ³
git ã® submodule æ©èœã䜿ã£ãŠã€ã³ã¹ããŒã«ãããšããã
git submodule add ${GIT_REPO_OF_THEME} themes/${THEME_NAME}
echo 'theme = "THEME_NAME"' >> config.toml
# ããŒãã® Config
ããŒãèªèº«ãconfig.toml
ãæã€ããšãã§ããããã ããèšå®ã§ããé
ç®ã¯äžèšã ãã
params
(global and per language)menu
(global and per language)outputformats
andmediatypes
# è€æ°ã®ããŒãã䜿ã
- è€æ°ã®ããŒããæå®ããå Žåã¯ãããŒãžããããå·ŠåŽãåªå
ãããã
i18n
,data
,config.toml
㯠deep merge ããããstatic
,layouts
,archetypes
ã¯ãã¡ã€ã«ããŒã¹ã§ merge ãããã
theme = ["my-shortcodes", "base-theme", "hyde"]
# Content Management
# Page Bundles
Page Resources ã管çããæ¹æ³ã®ããšãäžèšã®èµ€ãæ ã Page Bundles ãè¡šãã
# Leaf Bundle
content
ãã©ã«ãå ã®index.md
ãå«ããã©ã«ãã®ããšsingle
ã¬ã€ã¢ãŠãã䜿çšããã- ãã¹ãã§ããªã
- ã¬ã€ã¢ãŠããã¡ã€ã«ã«ãããŠãpage, non-page(images, PDFs, etc...) ã®ã©ã¡ãããªãœãŒã¹ãšããŠäœ¿çšã§ããã
# Branch Bundle
content
ãã©ã«ãå ã®_index.md
ãå«ããã©ã«ãã®ããšlist
ã¬ã€ã¢ãŠãã䜿çšããã- ä»ã®ã³ã³ãã³ããåæãããæã«äœ¿ã
- ã¬ã€ã¢ãŠããã¡ã€ã«ã«ãããŠãpage ã¯ãªãœãŒã¹ãšããŠäœ¿çšã§ããªãã
# Front Matter
# Pre-defined
ç¹ã«weight
ã¯äžŠã³æ¿ãã«ãã䜿ãã
# User-defined
ãŠãŒã¶å®çŸ©ã® Front Matter ãèšè¿°ããããšãã§ããããã®å Žåããã³ãã¬ãŒããã.Params.some_frontmatter_name
ã§ã¢ã¯ã»ã¹ã§ããã
# Page Resources
ã«ã¬ã³ãããŒãžããå©çšã§ãããç»åãä»ã®ããŒãžãPDF çããã¥ã¡ã³ãã®ããšã
.Resources
ã§ãªãœãŒã¹ã®é
åãååŸã§ããã
# Properties
- ResourceType
- Name
- Title
- Permalink
- RelPermalink
- Content
# Methods
- ByType
- Match
- GetMatch(
match
ãšåãã ããæåã®ã²ãšã€ã ããååŸããç¹ãç°ãªã)
# Image Processing
ç»ååŠçã«ã¯Resize
, Fit
, Fill
ã® 3 çš®é¡ãããããªãã·ã§ã³ã«ã€ããŠã¯ããã¥ã¡ã³ã (opens new window)ãåç
§ããããšã
# Resize
{{ $logo.Resize "200x" }} // ã¢ã¹ãã¯ãæ¯ã¯ç¶æããã
{{ $logo.Resize "200x100" }} // ã¢ã¹ãã¯ãæ¯ã¯å€æŽããã
# Fit
æå®ãããµã€ãºã§ããã€ãµã€ãºå ã«åãŸãããã«ç»åãçž®å°ãããã¢ã¹ãã¯ãæ¯ã¯ç¶æãããã
{{ $logo.Fit "200x100" }}
# Fill
æå®ãããµã€ãºã§ããã€ãµã€ãºå šäœãåãŸãããã«ç»åãåãåºããã¢ã¹ãã¯ãæ¯ã¯ç¶æãããã
{{ $logo.Fill "200x100" }} // çãäžãåºæºã«ããŠåãåºã
{{ $logo.Fill "200x100 right" }} // å³ç«¯ãåºæºã«ããŠåãåºã
{{ $logo.Fill "200x100 left" }} // 巊端ãåºæºã«ããŠåãåºã
# ç»åã®è¡šç€º
<img src="{{$logo.RelPermalink }}" />
# åŠçåŸã®ç»åã®åæ±ã
åŠçãããç»åã¯/resources
ãã©ã«ãã«æ ŒçŽãããã
Hugo ã®ããã©ãŒãã³ã¹ãåäžããããããã®ãã©ã«ãã¯ãgit ã®ç®¡ç察象ãšããã»ããããã
# Shortcode
Markdown ã®äžã§äœ¿ããã¹ããããã2 ã€ã®åŒã³åºãæ¹ãããã
<!-- äžã®ã³ã³ãã³ããMarkdownã®å Žå -->
{{% mdshortcode %}} **something** {{% /mdshortcode %}}
<!-- äžã®ã³ã³ãã³ãã®åŠçãå¿
èŠãªãå Žå -->
{{< highlight go >}} somethin {{< /highlight >}}
# Built-in Shortcode
- figure
- gist
- highlight(ã³ãŒããã€ã©ã€ã)
- ref, relref(ãªã³ã¯ã®äœæ)
- tweet
- vimeo
- youtube
# Sections
content
ãã©ã«ãå ã®ãã©ã«ãã section ã«ãªã- 第äžéå±€ã®ãã©ã«ãã root sections ã«ãªã
- section 㯠branch bundles ã§ããå¿
èŠãããããããã©ã«ãå
ã«
_index.md
ãäœæããããšãindex.md
ãäœæããå Žåã¯ãleaf bundles ã«ãªãã - section ã¯ãå
¥ãåã«ã§ããããã®éãæäžå±€ã® section ã¯å¿
ã
_index.md
ãæããªããã°ãªããªãã - section ã¯ãfront matter ã§äžæžãã§ããªã
# Section Page ã§äœ¿ãã Variables
.CurrentSection
çŸåšã®ããŒãžã®ã«ã¬ã³ãã»ã¯ã·ã§ã³ãååŸãããçŸåšã®ããŒãžãã»ã¯ã·ã§ã³ããŒãžãããŒã ããŒãžã ã£ãå Žåãèªåèªèº«ãè¿ãã.InSection $anogherPage
ããããŒãžãçŸåšã®ã»ã¯ã·ã§ã³ã«å±ãããå€å®ãã.IsAncestor $anotherPage
ããããŒãžãçŸåšã®ã»ã¯ã·ã§ã³ã®ç¥å ã«åœãããå€å®ãã.IsDescendant $anotherPage
çŸåšã®ããŒãžãæãããŒãžã®åå«ã«åœãããå€å®ãã.Parent
çŸåšã®ããŒãžãå±ãã root section ãååŸãã.Sections
çŸåšã®ã»ã¯ã·ã§ã³ã«å±ããåã»ã¯ã·ã§ã³ãååŸãã
# Content Type
# ã©ã®ããã«æ±ºãŸãã
ããã©ã«ãã§ã¯ãã»ã¯ã·ã§ã³åãèªåçã« Content Type ãšããŠèšå®ãããã
äŸãã°ãposts ã»ã¯ã·ã§ã³ã® Content Type ã¯posts
ã«ãªãã
front matter ã§äžæžãããããšãã§ããã
# Type ã䜿ãããå Žé¢ïŒarchetypesïŒ
Content Type 㯠archetype ã®å€å®ã«äœ¿çšãããŠããã
äŸãã°ãhugo new posts/my-first-post.md
ãå®è¡ãããšããarchetype ã¯äžèšã®é ã§äœ¿çšãããã
- archetypes/posts.md
- archetypes/default.md
- themes/my-theme/archetypes/posts.md
- themes/my-theme/archetypes/default.md
# Type ã䜿ãããå Žé¢ïŒã¬ã€ã¢ãŠãïŒ
Content Type ã«ããã䜿çšãããã¬ã€ã¢ãŠããã¡ã€ã«ã決ãŸãã
äŸãã°ãcontent/events/my-first-event.md
ã§äœ¿ãããã¬ã€ã¢ãŠãã¯äžèšã®é ã§æ±ºå®ãããã
- layouts/event/single.html (singular name ãåªå ããã)
- layouts/events/single.html
- layouts/_default/single.html
# Taxonomies
ããã©ã«ãã§ãtags
,categories
ã«ããåé¡ãã§ããããã«ãªã£ãŠããã
Taxonomies ã®æå®ã¯ãfront matter ã§è¡ãã
tags:
- nginx
- kubernetes
categories:
- development
ã«ã¹ã¿ã Taxonomies ãè¿œå ããå Žåã¯ãäžèšã®ããã« config ãèšå®ããã å·ŠåŽã¯åæ°åœ¢ã§èšèŒããããšã
[taxonomies]
category = "categories"
tag = "tags"
mytag = "mytags"
taxonomy templates ãååšããå ŽåãHugo ã¯äžèšã®ããŒãžãçæãããäŸãã°ãcategories ã®å Žåã
example.com/categories/
ã«ããŽãªã®äžèŠ§example.com/categories/nginx
ç¹å®ã«ããŽãªã«è©²åœããèšäºã®äžèŠ§
# Content Summaries
.Summary
ã§ããŒãžã®èŠçŽãååŸã§ãããèšäºã®æåã® 70 ã¯ãŒããèŠçŽãšããŠäœæãããã
æ¥æ¬èªã§äœ¿ãå Žåã¯hasCJKLanguage
ãªãã·ã§ã³ãæå¹ã«ããããšã
ã«ã¹ã¿ãã€ãº (opens new window)ãå¯èœã
{{ range first 10 .Pages }}
<div>{{ .Summary }}</div>
{{ if .Truncated }}
<div>Read MoreâŠ</div>
{{ end }}
{{ end }}
.Pages
ããŒãžã®äžèŠ§ãååŸ.Summary
èŠçŽãååŸ.Truncated
ç¶ããããã
# Links
ref
åã¯relref
ãšããã·ã§ãŒãã³ãŒãã䜿ãããšã§ãMarkdown ãã¡ã€ã«å
ã§ãªã³ã¯ãäœæã§ããã
{{< ref "document.md#anchor" >}}
{{< relref "document.md#anchor" >}}
<!-- ä»èšèªããŒãžã§ã³ãžã®ãªã³ã¯ -->
{{< relref path="document.md" lang="jp" >}}
<!-- åºå圢åŒã®æå® -->
{{< relref path="document.md" outputFormat="rss" >}}
# URL
# Permalinks
Permalink ã®åœ¢åŒãåž°ãå Žåã¯ãConfig ã«äžèšã®éãèšèŒããã section å㯠taxonomies ããšã«æå®ããã
[permalinks]
post = "/:year/:month/:title/"
䜿çšã§ããå€æ°ã®äžèŠ§ (opens new window)
# Aliases
å€ãããŒãžããæ°ããããŒãžã«ãªãã€ã¬ã¯ããããããšãã«äœ¿ãã æ°ããããŒãžåŽã® front matter ã«äžèšã®éãèšè¿°ããã
# /posts/post2ã«äžèšã®éãèšèŒãããšãpost1=>post2ã«ãªãã€ã¬ã¯ãããã
aliases:
- /posts/post1
# Pretty URL & Ugly URL
URL ã®åœ¢åŒã config ã front matter ã§èšå®ã§ããã
# Pretty
example.com/posts/
example.com/posts/post-1/
# Ugly
example.com/posts.html
example.com/posts/post-1.html
# Menu
.Site.Menu
ã§ã¢ã¯ã»ã¹ã§ããã- ã¡ãã¥ãŒã®å®çŸ©ã¯ front matter ããã㯠Config ã«èšèŒããã
- Menu Templates ã®å®çŸ©ãå¿ èŠãšãªãã
- å
¥ãåã«ãããå Žåã¯
parent
ãã£ãŒã«ãã§èŠªãæå®ããã
[menu]
[[menu.main]]
identifier = "about"
name = "about hugo"
pre = "<i class='fa fa-heart'></i>"
url = "/about/"
weight = -110
[[menu.main]]
name = "getting started"
pre = "<i class='fa fa-road'></i>"
url = "/getting-started/"
weight = -100
# Multilingual Mode
# å€èšèªç°å¢ã®ã»ããã¢ãã
å€èšèªå¯Ÿå¿ãæå¹ã«ããããããã¯èšèªããšã« Config ã®èšå®ãå€ããããšãã«ã¯ãConfig ã«languages
ã»ã¯ã·ã§ã³ãèšèŒããã
DefaultContentLanguage: en
languages:
en:
params:
linkedin: https://linkedin.com/whoever
title: My blog
weight: 1
fr:
params:
linkedin: https://linkedin.com/fr/whoever
navigation:
help: Aide
title: Mon blogue
weight: 2
params:
navigation:
help: Help
languages
ãããã¯ã«èšèŒã®ãªããã®ã¯ãã°ããŒãã«ã®å€ã«ãã©ãŒã«ããã¯ãããã
ããã©ã«ãèšèªã®ãã¹ã¯/posts/post-1
ããã®ä»ã®èšèªã®ãã¹ã¯/ja/posts/post-1/
ã®ããã«ããããããã
èšèªããšã®ã³ã³ãã³ãã®äœææ¹æ³ã¯ããã¡ã€ã«ã§åããæ¹æ³ãšãã©ã«ãã§åããæ¹æ³ãããã 察å¿èšèªã 2 èšèªããããªãããã¡ã€ã«ã§åããã»ãã楜ã§ããã ãã®æ¹æ³ã¯ãããŒã¯ããŠã³ä»¥å€ã«ããç»åãã¡ã€ã«ã PDF ãã¡ã€ã«ã«ãé©çšã§ããã
/content/about.en.md
=>example.com/about/
/content/about.ja.md
=>example.com/about/ja/
# å€æ°
.IsTranslated
çŸåšã®èšäºã翻蚳ããããã®ã.Lang
çŸåšã®ããŒãžã®èšèªãååŸ.Translations
çŸåšã®èšäºã®ä»ã®èšèªã®ããŒãžäžèŠ§ãååŸ.AllTranslations
çŸåšã®èšäºã®å šãŠã®èšèªã®ããŒãžäžèŠ§ãååŸïŒèªèº«å«ãïŒ
# åèªã®ç¿»èš³
ãããžã§ã¯ãã®ã«ãŒãã«i18n
ãã©ã«ããäœããen.toml
, ja.toml
ãªã©ã®ãã¡ã€ã«ãäœãã
other
ãšone
ã䜿ããããããŸãã¡æå³ãããåãããªãã
[home]
other = "Home"
[wordCount]
other = "This article has {{ .WordCount }} words."
ãã³ãã¬ãŒãã®äžã§ãäžèšã®ããã«ããŠäœ¿ãã
{{ i18n "home" }}
{{ i18n "wordcount" . }} <!-- ã³ã³ããã¹ãã.ããæž¡ããŠããç¹ã«æ³šç® -->
# æ¥æã®ç¿»èš³
ãã¡ã (opens new window)ãåç §
# Menu ã®ç¿»èš³
languages
ã»ã¯ã·ã§ã³ã®äžã«ãåèšèªããšã®ã¡ãã¥ãŒãå®çŸ©ããã
å®çŸ©ã®æ¹æ³ã¯éåžžãšåãã
defaultContentLanguage = "en"
[languages.en]
weight = 0
languageName = "English"
[[languages.en.menu.main]]
url = "/"
name = "Home"
weight = 0
[languages.de]
weight = 10
languageName = "Deutsch"
[[languages.de.menu.main]]
url = "/"
name = "Startseite"
weight = 0
.Site.Menus
ã§ãçŸåšã®èšèªã®ã¡ãã¥ãŒãååŸã§ããã
ãªã³ã¯ãäœæãããšãã¯absLangURL
orrelLangUrl
ãã¡ã³ã¯ã·ã§ã³ã䜿ããªããšãããã©ã«ãèšèªã«ãªã³ã¯ãããŠããŸãã®ã§æ³šæã
{{ range .Site.Menus.main }}
<a href="{{ .URL | absLangURL }}">{{ .Name }}</a>
{{ end }}
<!-- ããã㯠-->
<a href="{{ $.Site.LanguagePrefix }}/*****"></a>
# Syntax Highlighting
Hugo 㯠Chroma ãã·ã³ã¿ãã¯ã¹ãã€ã©ã€ã¿ãŒãšããŠäœ¿çšããŠããã
ãã€ã©ã€ãããããšãã¯highlight
ã·ã§ãŒãã³ãŒãã䜿ãã
{{< highlight go "linenos=table,hl_lines=8 15-17,linenostart=199" >}}
<!-- ... code -->
{{< / highlight >}}
linenos
è¡çªå·ã®è¡šç€ºæç¡ãinline ã table ãéžã¹ããhl_lines
ãã€ã©ã€ããããè¡ã®çªå·linenostart
è¡ã®éå§çªå·
ãªããã·ã§ãŒãã³ãŒãã§ã¯ãªãéåžžã®ã³ãŒããããã¯ãèªåçã«ãã€ã©ã€ããããå Žåã¯ããªãã·ã§ã³ãæå® (opens new window)ããã
pygmentscodefences: true
# Templates
# Go Templates ã®åºæ¬çãªäœ¿ãæ¹
# æŠèŠ
Go Templates ã¯ãHTML, variables, functions ããæ§æãããã
variables 㚠functions 㯠{{}}
ã®äžã§äœ¿çšããããšãã§ããã
{{ .Title }} <!-- predefined variable -->
{{ .$mykey }} <!-- custom variable -->
{{ FUNCTION ARG1 ARG2 ..... }} <!-- function -->
ã¡ãœããããã£ãŒã«ãã«ã¯ããããã§ã¢ã¯ã»ã¹ããã
{{ .Params.bar }}
()
ã¯èŠçŽ ãã°ã«ãŒãã³ã°ããæã«äœ¿ã
{{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }}
# Variables
ãã³ãã¬ãŒãã¯å¿
ãPage variable (opens new window)ã.
ãšããŠåãåãã
{{ .Title }}
Custom Variable ã¯äžèšã®ããã«äœ¿ãã$
ãå¿
ãå¿
èŠã
{{ $address := "123 Main St." }}
{{ $address }}
# Functions
{{ add 1 2 }}
{{ lt 1 2 }} <!-- 1 less than 2 ? -->
# Partials
partials ã䜿ããšãã®èšæ³ã
<!-- `layouts/partials/header.html`ãåã蟌ã¿ããå Žå -->
{{ partial "header.html" . }}
# Template
internal templates (opens new window)ã䜿ããšãã®èšæ³ã
{{ template "_internal/opengraph.html" . }}
# ç¹°ãè¿ã
<!-- ã³ã³ããã¹ãã䜿ãå Žå -->
{{ range $array }}
{{ . }} <!-- . ã¯$arrayã®äžã«ããåèŠçŽ ãæã -->
{{ end }}
<!-- ã«ã¹ã¿ã å€æ°ã䜿ãå Žå -->
{{ range $elem_val := $array }}
{{ $elem_val }}
{{ end }}
<!-- ã€ã³ããã¯ã¹ã䜿ãããå Žå -->
{{ range $elem_index, $elem_val := $array }}
{{ $elem_index }} -- {{ $elem_val }}
{{ end }}
<!-- ãããã®key-valueãã¢ã䜿ãããå Žå -->
{{ range $elem_key, $elem_val := $map }}
{{ $elem_key }} -- {{ $elem_val }}
{{ end }}
# æ¡ä»¶åå²
if
, else
, with
, or
, and
ãªã©ãããã
TIP
Go Templates ã false ãšå€å®ãããã®
false
0
- é·ãã 0 ã®
array
,slice
,map
,string
# with
èŠçŽ ãååšããå Žåã«åŠçãè¡ããå
éšã®.
ã¯ãæž¡ãããã©ã¡ãŒã¿ã«ãã€ã³ããããïŒrange
ãšäžç·ïŒã
{{ with .Params.title }}
<h4>{{ . }}</h4>
{{ else }}
<h4>no data!</h4>
{{ end }}
# if
with ãã现ããå¶åŸ¡ãã§ãããwith ãšç°ãªãã.
ã¯ãã©ã¡ãŒã¿ãšãã€ã³ããããªãã
{{ if (isset .Params "description") }}
{{ .Params.description }}
{{ else if (isset .Params "summary") }}
{{ .Params.summary }}
{{ else }}
{{ .Params.something }}
{{ end }}
# and, or
{{ if (and (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")) }}
# Pipes
<!-- ã·ã£ããã« -->
{{ (seq 1 5) | shuffle }}
<!-- htmlãžã®ããŒã¹ -->
{{ index .Params "disqus_url" | html }}
<!-- æ¡ä»¶æãç°¡åã«æžã -->
{{ if isset .Params "caption" | or isset .Params "title" | or isset .Params "attr" }}
{{ end }}
# Context(.
ã®ããš)
.
㯠Current Context ã§ããã
Current Context ãšã¯ãçŸåšã®ããŒãžã«é¢é£ããããŒã¿ã®ããšã§ããã
range ãããã¯ã®äžãªã©ã§ã°ããŒãã«ã³ã³ããã¹ãã«ã¢ã¯ã»ã¹ããããšãã¯ãäžèšã® 2 çš®é¡ã®æ¹æ³ãããã
<!-- ã°ããŒãã«ã³ã³ããã¹ããã«ã¹ã¿ã å€æ°ã«ä¿åããŠãã -->
{{ $title := .Site.Title }}
<!-- $.ãã€ãã -->
{{ $.Site.Title }}
# White Space
ãã«ãæã«{{}}
ã®åšãã«ãã¯ã€ãã¹ããŒã¹ãæ¹è¡ãäœããããªããšãã¯ãäžèšã®ããã«ããã
<div>
{{- .Title -}}
</div>
<!-- äžèšã®ã³ãŒãã¯äžèšã®HTMLã«å€æããã -->
<div>Hello, World!</div>
# Comments
{{ /* comment */ }}
<!-- htmlã³ã¡ã³ããåºåããããšã -->
{{ printf "<!-- Our website is named: %s -->" .Site.Title | safeHTML }}
ãªããHTML ã³ã¡ã³ãã®äžã« Go template ã¯æžãã¡ãã ãã
# Parameters
config ã®params
ã«èšèŒãã key-value => ãµã€ãã¬ãã«ã®ãã©ã¡ãŒã¿ã.Site.Params
ã§ã¢ã¯ã»ã¹ããã
front matter ã«èšèŒãã key-value => ããŒãžã¬ãã«ã®ãã©ã¡ãŒã¿ã.Params
ã§ã¢ã¯ã»ã¹ããã
# Layout ãã¡ã€ã«ã®éžå®
Hugoga ããåããŒãžã«å¯Ÿå¿ãã Layout ãã¡ã€ã«ãéžå®ããéã«ã¯ãäžèšã®æ¡ä»¶ãå šãŠèæ ®ããããã§æ±ºå®ãããã ãããšè€éãªã®ã§ã詳现ã¯ãã¡ãã®ããŒãž (opens new window)ãåç §ã
# Kind
ããŒãžã®çš®é¡ãsingle page ãªã_default/single.html
, list page ãªã_default/list.html
ã䜿ãããã
# Output Format
name (e.g. rss, amp, html) ãš suffix (e.g. xml, html)ãäŸãã°index.amp.html
ãªããããããããããã³ãã¬ãŒãã䜿ãããã
# Language
äŸãã°index.ja.md
ãªã©ãOutput Format ãšæ··åããŠäœ¿ãå Žåã¯ãindex.fr.amp.html
ã®ããã«ããªããšé©åãªãã³ãã¬ãŒããéžã°ããªãã®ã§æ³šæããã
# Layout
front matter ã®layout
ã«èšå®ãããŠããå€
# Type
Type(=Section Name, ããã㯠front matter ã§æå®ãã Type)
# Section
Section å
# Base Template
ããŒã¹ãšãªããã³ãã¬ãŒãã®ããšãblock
ãšdefine
ãçµã¿åãããŠäœ¿ããBase Template ã¯è€æ°äœæã§ããã
<!-- layouts/_default/baseof.html -->
{{ block "main" . }}
<h1>baseof page main!</h1>
{{ end }}
{{ block "footer" . }}
<h1>baseof page footer</h1>
{{ end }}
<!-- layouts/_default/single.html -->
{{ define "main" }}
<h1>hello from single page</h1>
{{ end }}
åºåçµæ
<h1>hello from single page</h1><!-- äžæžããããŠãã -->
<h1>baseof page footer</h1>
# List Page Template
homepage, section page, taxonomy list, taxonomy terms ã§äœ¿çšãããã¬ã€ã¢ãŠãã
# _index.md
ã«ã€ããŠ
ã»ã¯ã·ã§ã³ããšã®ããŒãžãäŸãã°example.com/posts/
ã¯ã_index.md
ã®æç¡ã«ãããããã
list templates ã䜿ã£ãŠèªåçã«äœæãããã
_index.md
ãååšããªãã£ãå Žåã¯ã.Title
ã«ã»ã¯ã·ã§ã³åãã»ããããã ãã®ã³ã³ããã¹ããããã³ãã¬ãŒãã«æž¡ãããã
# 䞊ã¹æ¿ã
ããã©ã«ãã§ã¯ãWeight > Date > LinkTitle > FilePath ã®é ã§äžŠã¶ã ã«ã¹ã¿ã ããã«ã¯äžèšã®ããããã£ã䜿ãã
.Pages.ByWeight
.Pages.Date
.Pages.ByPublishDate
.Pages.ByExpiryDate
.Pages.ByLastmod
.Pages.ByLength
.Pages.ByTitle
.Pages.ByLinkTitle
linktitle ããªããã° title ã䜿ããã.Pages.ByParam "rating"
.Pages.Pages.ByDate.Reverse
éé
# ã°ã«ãŒãã³ã°
{{ range .Pages.GroupBy "Section" }}
{{ .Key }} <!-- ã»ã¯ã·ã§ã³å -->
{{ range .Pages }}
{{ . }}<!-- ã»ã¯ã·ã§ã³ããšã®åå¥ããŒãž -->
{{ end }}
{{ end }}
.Pages.GroupBy "Section"
.Pages.GroupByDate "2006-01"
2006-01 éšåã¯ãã©ãŒããããæå®ããŠããã ãã§å®éã«äœ¿ãããå€ã§ã¯ãªãã®ã§æ³šæ.Pages.GroupByPublishDate "2006-01"
.Pages.GroupByParam "param_key"
.Pages.GroupByParamDate "param_key" "2006-01"
# where
3 ã€ã®åŒæ°ãåãã
array
orslice of maps or structs
key
orfield name
match value
{{ range where .Pages "Section" "post" }
# first
{{ range first 10 .Pages }}
# åããæ
{{ range first 5 (where .Pages "Section" "post").ByTitle }}
# Homepage Template
ããŒã ããŒãžïŒcontent/_index.md
ïŒã«ã ãå¥ã®ã¬ã€ã¢ãŠããé©çšãããæã«äœ¿ããlayouts/index.html
ã«é
眮ããã
Homepate Template ã§ã¯ããµã€ãå
ã®å
šãŠã®ããŒãžã.Pages
ã§ååŸã§ãã
# Section Template
homepage, page, section, taxonomy, taxonomyTerm ã§äœ¿ãããã.Pages
ã§é
äžã®ã³ã³ãã³ããååŸã§ããã
Hugo ã®å
šãŠã®ããŒãžã¯ãããŒãžã®çš®é¡ãè¡šã.Kind
å€æ°ãæã£ãŠããã
home
,page
,section
,taxonomy
,taxonomyTerm
ã®ããããã
ç¹å®ã® Kind ã®äžã®ãç¹å®ã®å€ã®ããŒãžãæãåºããšãã¯.Site.GetPage
ã䜿ãã
{{ .Site.GetPage "section" "posts" }}
{{ .Site.GetPage "page" "search" }}
# Taxonomy Templates
taxonomy ã§ã¯äžèšã® 3 ã€ã®ãã³ãã¬ãŒãã䜿ãããã
- taxonomy list template
- taxonomy terms template
- single page template
# taxonomy list template
list templates ãšäžç·ã
.Data.Terms
㧠terms ãååŸã§ãããterms ã§äœ¿ããã¡ãœããã¯ä»¥äžã®éãã
.Get(term)
.Count(term)
.Alphabetical
.ByCount
.Reverse
# taxonomy terms template
.Term
çŸåšã®ããŒãžã® term ãååŸ.WeightedPages
çŸåšã®ããŒãžã® term ã«åœãŠã¯ãŸã(weight ã€ãã®)ããŒãžäžèŠ§.Count
çŸåšã®ããŒãžã® term ã«åœãŠã¯ãŸãããŒãžæ°.Pages
çŸåšã®ããŒãžã® term ã«åœãŠã¯ãŸãããŒãžäžèŠ§
# single page template
.Params.tags
èšå®ãããŠãã term ã®äžèŠ§ãååŸ
# ãã®ä»
taxonomy ã®åãåºãæ¹ãªã©ã詳现ã¯ããã¥ã¡ã³ã (opens new window)ãåç §
# Single Page Templates
page variables ãš site variables ã䜿ããã以äžã
# Content View Templates
ã³ã³ãã³ããå€åœ¢ããŠè¡šç€ºããããæã«äœ¿ãã.Render
ãšããç¹å¥ãªã¡ãœããã䜿ãã
Content View Template ã«ã¯ãåŒã³åºãå
ã®.
ãèªåçã«æž¡ãããã
⟠layouts/
⟠post/
li.html <= content view template
list.html
summary.html <= content view template
<!-- list.html -->
<h1 id="title">{{ .Title }}</h1>
{{ range .Pages }}
{{ .Render "summary"}}
{{ end }}
<!-- summary.html -->
<article class="post">
{{ .Title }} - {{ .Summary }}
</article>
# Data Templates
data
ãã©ã«ãã«é
眮ãã yaml, json, toml ã¯ããã³ãã¬ãŒããã.Data[.path].filename.keyname
ã§ã¢ã¯ã»ã¹ã§ããã
åçã« JSON ã CSV ãååŸããããšãã§ããããã ããèªèšŒã¯äœ¿ãããMethod ã GET ã®ã¿ã
{{ $data := getJSON "url" }}
{{ $data := getCSV "separator" "url" }}
# Partial Templates
ã¬ã€ã¢ãŠãã®äžéšåãæ
ããã³ãã¬ãŒãã§ãããlayouts/partials
ã«é
眮ããã
partial ã¯ãã³ã³ããã¹ããåŒæ°ãšããŠæž¡ãããŠããªãéãããããªãã³ã³ããã¹ãã«ãã¢ã¯ã»ã¹ã§ããªãã®ã§æ³šæããã
<!-- åŒã³åºãå
ãã³ãã¬ãŒã -->
{{ partial "path/filename.html" . }}
<!-- or -->
{{ partial "path/filename.html" (dict "key1" "value1" "key2" "value2") }}
<!-- partial -->
<h1>{{ .Title }}</h1>
# Shortcode Templates
Markdown ãã¡ã€ã«ã®äžã§äœ¿ãããã®ãã³ãã¬ãŒããlayouts/shortcodes/
ã«é
眮ããã
# named args
<!-- markdown -->
{{< mycode color="red">}}
<!-- short code -->
<p style="color:{{ .Get color }}" />
# positional args
<!-- markdown -->
{{< mycode red>}}
<!-- short code -->
<p style="color:{{ .Get 0 }}" />
# Inner
<!-- markdown -->
{{< mycode >}}
some contents
{{< /mycode >}}
<!-- markdown(åŠçãå¿
èŠãªå Žå) -->
{{% mycode %}}
**some contents**
{{% /mycode %}}
<!-- short code -->
<p>{{ .Inner }}</p>
# shortcodes ã®äžã§äœ¿ããå€æ°
$.Params
shortcode ã«æž¡ãããå€$.Page.Params
shortcode ã®åŒã³åºãå ããŒã¯ããŠã³ã® front matter ãååŸ$.Page.Site.Params
ãµã€ãã¬ãã«ã® Par
# Local File Templates
ããŒã«ã«ãã¡ã€ã«ããã©ã«ãããããŒã¿ãååŸã§ãããå¿ èŠã«ãªã£ãæã«ããã¥ã¡ã³ã (opens new window)ãèªãã
# 404 Page
ã«ã¹ã¿ã ãããå Žåã¯layouts/404.html
ãäœæããã
# Menu Templates
ã¡ãã¥ãŒã¯partials
ã䜿ã£ãŠäœæããããšãå€ããé
ç®ã¯ Config ã§èšå®ããããšãå€ãã
äžèšã®å€æ°ãçµã¿åãããŠäœæããã詳现ã¯ããã¥ã¡ã³ã (opens new window)åç
§ã
.Site.Menus.main
.Site.Menus.main.HasChildren
.HasMenuCurrent
.IsMenuCurrent
ãªããå€èšèªå¯Ÿå¿ç°å¢ã§ãªã³ã¯ãäœæãããšãã¯ãabsLangURL
or relLangUrl
ã䜿ãããšã
# Pagination
config ã§äžèšã®ããã«èšå®ããã
paginate: 10 # ããã©ã«ã
paginatePath: page # ããã©ã«ããpost/1/ ãªã©
Paginator
ã䜿ãç°¡åãªæ¹æ³ãšãPaginate
ã䜿ã詳现ãªæ¹æ³ã®ãäºéãããã
<!-- Paginator -->
{{ range .Paginator.Pages }}
{{ range (.Paginator 20).Pages }} <!-- äžéæ°ã¯äžæžãã§ãã -->
<!-- Paginate -->
{{ range (.Paginate ( .Pages.ByTitle )).Pages }}
{{ range (.Paginate ( .Pages.ByTitle ) 20).Pages }} <!-- äžéæ°ã¯äžæžãã§ãã -->
ãã³ãã¬ãŒãã®æžãæ¹ããã«ãã€ã³ãã³ãã¬ãŒãã䜿ãã èªèº«ã§äœãå Žåã¯ããã¥ã¡ã³ã (opens new window)ãèªãã§é 匵ãã
{{ template "_internal/pagination.html" . }}
{{ range .Paginator.Pages }}
{{ .Title }}
{{ end }}
# RSS Templates
ããã©ã«ã㧠RSS ã¯èªåçã«çæããããexample.com/index.xml
, example.com/posts/index.xml
ãªã©ã
ããã¥ã¡ã³ã (opens new window)
# Sitemap Templates
ããã©ã«ã㧠ãµã€ãããã ã¯example.com/sitemap.xml
ã«èªåçã«çæãããã
ããã¥ã¡ã³ã (opens new window)
# Robots.txt
äœæããå Žåã¯layouts/robots.txt
ãäœæããã
# Internal Templates
ãã«ãã€ã³ã®ãã³ãã¬ãŒããããã®ã§ãé©å®å©çšããã
_internal/disqus.html
_internal/google_news.html
_internal/google_analytics.html
_internal/google_analytics_async.html
_internal/opengraph.html
_internal/pagination.html
_internal/schema.html
_internal/twitter_cards.html
# ãããã°
äžèšã®ããã«ãããšæ å ±ã詳现ã«ææ¡ã§ããã
{{ printf "%#v" . }}
# Variables
- .Site (opens new window)
- .Page (opens new window)
- Shortcode (opens new window)
- Taxonomy (opens new window)
- Menu (opens new window)
# ãã©ãã«ã·ã¥ãŒãã£ã³ã°
# baseof.html ãé©çšãããªã
åãã³ãã¬ãŒãã®ãããã¬ãã«ã¯ãdefine "main"
ã®ã¿ã§ããããšãäœèšãªãã®ããããš baseof ãé©çšãããªãã
<!-- ããšãã°ããã«äœèšãªã³ã¡ã³ããæããšé©çšãããªã -->
{{ define "main" }}
<h1>hello from lista page</h1>
{{ end }}