Hugo Internal Templates Gists

If you’re like me and want to know what’s under the hood: Here I share with you my gists about the internal templates of Hugo (latest version). I formatted them so you can read more easily through the files. The purpose is to discover what’s in there and to eventually take it as a basis for own individual templates. Have Fun.

Internal Disqus Template
Internal Google Analytics async Template
Internal Google Analytics Template
Internal Google News Template
Internal Opengraph Template
Internal Schema Template
Internal Twitter Cards Template

5 Likes

YMMW, but to see the latest internal shortcodes and templates, you can just bookmark tpl/tplimpl/template_embedded.go.

  • Search for addInternalTemplate for all internal templates. Example: disqus template.
  • Search for addInternalShortcode for all internal shortcodes. Example: figure shortcode.

If you have cloned the repo, you can even use grep (or rg, ag, ack, etc.) to do a search like this:

Look for all internal templates

km²~/go.apps/:../hugo/tpl/tplimpl> rg 'addInternalTemplate' template_embedded.go
63:     t.addInternalTemplate("_default", "rss.xml", `<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
90:     t.addInternalTemplate("_default", "sitemap.xml", `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
113:    t.addInternalTemplate("_default", "sitemapindex.xml", `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
125:    t.addInternalTemplate("", "pagination.html", `{{ $pag := $.Paginator }}
168:    t.addInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
190:    t.addInternalTemplate("", "opengraph.html", `<meta property="og:title" content="{{ .Title }}" />
234:    t.addInternalTemplate("", "twitter_cards.html", `{{- with $.Param "images" -}}
251:    t.addInternalTemplate("", "google_news.html", `{{ if .IsPage }}{{ with .Params.news_keywords }}
255:    t.addInternalTemplate("", "schema.html", `{{ with .Site.Social.GooglePlus }}<link rel="publisher" href="{{ . }}"/>{{ end }}
271:    t.addInternalTemplate("", "google_analytics.html", `{{ with .Site.GoogleAnalytics }}
283:    t.addInternalTemplate("", "google_analytics_async.html", `{{ with .Site.GoogleAnalytics }}
292:    t.addInternalTemplate("_default", "robots.txt", "User-agent: *")

Look for all internal shortcodes

km²~/go.apps/:../hugo/tpl/tplimpl> rg 'addInternalShortcode' template_embedded.go
17:     t.addInternalShortcode("ref.html", `{{ if len .Params | eq 2 }}{{ ref .Page (.Get 0) (.Get 1) }}{{ else }}{{ ref .Page (.Get 0) }}{{ end }}`)
18:     t.addInternalShortcode("relref.html", `{{ if len .Params | eq 2 }}{{ relref .Page (.Get 0) (.Get 1) }}{{ else }}{{ relref .Page (.Get 0) }}{{ end }}`)
19:     t.addInternalShortcode("highlight.html", `{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}`)
20:     t.addInternalShortcode("test.html", `This is a simple Test`)
21:     t.addInternalShortcode("figure.html", `<!-- image -->
39:     t.addInternalShortcode("speakerdeck.html", "<script async class='speakerdeck-embed' data-id='{{ index .Params 0 }}' data-ratio='1.33333333333333' src='//speakerdeck.com/assets/embed.js'></script>")
40:     t.addInternalShortcode("youtube.html", `{{ if .IsNamedParams }}
49:     t.addInternalShortcode("vimeo.html", `{{ if .IsNamedParams }}<div {{ if .Get "class" }}class="{{ .Get "class" }}"{{ else }}style="position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; overflow: hidden;"{{ end }}>
56:     t.addInternalShortcode("gist.html", `<script src="//gist.github.com/{{ index .Params 0 }}/{{ index .Params 1 }}.js{{if len .Params | eq 3 }}?file={{ index .Params 2 }}{{end}}"></script>`)
57:     t.addInternalShortcode("tweet.html", `{{ (getJSON "https://api.twitter.com/1/statuses/oembed.json?id=" (index .Params 0)).html | safeHTML }}`)
58:     t.addInternalShortcode("instagram.html", `{{ if len .Params | eq 2 }}{{ if eq (.Get 1) "hidecaption" }}{{ with getJSON "https://api.instagram.com/oembed/?url=https://instagram.com/p/" (index .Params 0) "/&hidecaption=1" }}{{ .html | safeHTML }}{{ end }}{{ end }}{{ else }}{{ with getJSON "https://api.instagram.com/oembed/?url=https://instagram.com/p/" (index .Params 0) "/&hidecaption=0" }}{{ .html | safeHTML }}{{ end }}{{ end }}`)
5 Likes

So true but for some people it’s just convenient to see the html code at a glimpse. Of course your tip is ideal for developers.

Hmm. Maybe someday we could generate a page for these in the Hugo docs. :slight_smile:

3 Likes

Discourse sends me a notification today that my answer above is “popular”. I wonder why …

That answer is very much outdated.

Folks looking for Hugo-inbuilt templates and shortcodes should now directly visit the tpl/tplimpl/embedded/templates/ directory in the repo: https://github.com/gohugoio/hugo/tree/master/tpl/tplimpl/embedded/templates.

I would say these are more accessible:

6 Likes