coming back
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -23,9 +23,9 @@ This document is a brief primer on using Go templates." />
|
||||
Made with Book Theme
|
||||
https://github.com/alex-shpak/hugo-book
|
||||
-->
|
||||
<link rel="stylesheet" href="https://academics.jamesflare.com/css/custom.css">
|
||||
|
||||
<script type="text/javascript" src="https://academics.jamesflare.com/js/custom.js"></script>
|
||||
<link rel="stylesheet" href="https://academics.jamesflare.com/css/custom.css">
|
||||
|
||||
<script type="text/javascript" src="https://academics.jamesflare.com/js/custom.js"></script>
|
||||
<script>pangu.spacingPage();</script>
|
||||
</head>
|
||||
<body dir="ltr">
|
||||
@@ -236,18 +236,18 @@ details can be found in the <a href="https://golang.org/pkg/html/template/">Go d
|
||||
functions.</p>
|
||||
<p><strong>Go variables and functions are accessible within {{ }}</strong></p>
|
||||
<p>Accessing a predefined variable “foo”:</p>
|
||||
<pre><code>{{ foo }}
|
||||
<pre><code>{{ foo }}
|
||||
</code></pre>
|
||||
<p><strong>Parameters are separated using spaces</strong></p>
|
||||
<p>Calling the add function with input of 1, 2:</p>
|
||||
<pre><code>{{ add 1 2 }}
|
||||
<pre><code>{{ add 1 2 }}
|
||||
</code></pre>
|
||||
<p><strong>Methods and fields are accessed via dot notation</strong></p>
|
||||
<p>Accessing the Page Parameter “bar”</p>
|
||||
<pre><code>{{ .Params.bar }}
|
||||
<pre><code>{{ .Params.bar }}
|
||||
</code></pre>
|
||||
<p><strong>Parentheses can be used to group items together</strong></p>
|
||||
<pre><code>{{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }}
|
||||
<pre><code>{{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }}
|
||||
</code></pre>
|
||||
<h2 id="variables">
|
||||
Variables
|
||||
@@ -258,11 +258,11 @@ template is passed either a page or a node struct depending on which type of
|
||||
page you are rendering. More details are available on the
|
||||
<a href="/layout/variables">variables</a> page.</p>
|
||||
<p>A variable is accessed by referencing the variable name.</p>
|
||||
<pre><code><title>{{ .Title }}</title>
|
||||
<pre><code><title>{{ .Title }}</title>
|
||||
</code></pre>
|
||||
<p>Variables can also be defined and referenced.</p>
|
||||
<pre><code>{{ $address := "123 Main St."}}
|
||||
{{ $address }}
|
||||
<pre><code>{{ $address := "123 Main St."}}
|
||||
{{ $address }}
|
||||
</code></pre>
|
||||
<h2 id="functions">
|
||||
Functions
|
||||
@@ -276,7 +276,7 @@ are useful for building websites. Functions are called by using their name
|
||||
followed by the required parameters separated by spaces. Template
|
||||
functions cannot be added without recompiling hugo.</p>
|
||||
<p><strong>Example:</strong></p>
|
||||
<pre><code>{{ add 1 2 }}
|
||||
<pre><code>{{ add 1 2 }}
|
||||
</code></pre>
|
||||
<h2 id="includes">
|
||||
Includes
|
||||
@@ -287,7 +287,7 @@ able to access. To pass along the current context please remember to
|
||||
include a trailing dot. The templates location will always be starting at
|
||||
the /layout/ directory within Hugo.</p>
|
||||
<p><strong>Example:</strong></p>
|
||||
<pre><code>{{ template "chrome/header.html" . }}
|
||||
<pre><code>{{ template "chrome/header.html" . }}
|
||||
</code></pre>
|
||||
<h2 id="logic">
|
||||
Logic
|
||||
@@ -302,20 +302,20 @@ the /layout/ directory within Hugo.</p>
|
||||
a map, array or slice. The following are different examples of how to use
|
||||
range.</p>
|
||||
<p><strong>Example 1: Using Context</strong></p>
|
||||
<pre><code>{{ range array }}
|
||||
{{ . }}
|
||||
{{ end }}
|
||||
<pre><code>{{ range array }}
|
||||
{{ . }}
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<p><strong>Example 2: Declaring value variable name</strong></p>
|
||||
<pre><code>{{range $element := array}}
|
||||
{{ $element }}
|
||||
{{ end }}
|
||||
<pre><code>{{range $element := array}}
|
||||
{{ $element }}
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<p><strong>Example 2: Declaring key and value variable name</strong></p>
|
||||
<pre><code>{{range $index, $element := array}}
|
||||
{{ $index }}
|
||||
{{ $element }}
|
||||
{{ end }}
|
||||
<pre><code>{{range $index, $element := array}}
|
||||
{{ $index }}
|
||||
{{ $element }}
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<h3 id="conditionals">
|
||||
Conditionals
|
||||
@@ -330,31 +330,31 @@ logic in Go Templates. Like range, each statement is closed with <code>end</code
|
||||
<li>any array, slice, map, or string of length zero</li>
|
||||
</ul>
|
||||
<p><strong>Example 1: If</strong></p>
|
||||
<pre><code>{{ if isset .Params "title" }}<h4>{{ index .Params "title" }}</h4>{{ end }}
|
||||
<pre><code>{{ if isset .Params "title" }}<h4>{{ index .Params "title" }}</h4>{{ end }}
|
||||
</code></pre>
|
||||
<p><strong>Example 2: If -> Else</strong></p>
|
||||
<pre><code>{{ if isset .Params "alt" }}
|
||||
{{ index .Params "alt" }}
|
||||
{{else}}
|
||||
{{ index .Params "caption" }}
|
||||
{{ end }}
|
||||
<pre><code>{{ if isset .Params "alt" }}
|
||||
{{ index .Params "alt" }}
|
||||
{{else}}
|
||||
{{ index .Params "caption" }}
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<p><strong>Example 3: And & Or</strong></p>
|
||||
<pre><code>{{ if and (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
|
||||
<pre><code>{{ if and (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
|
||||
</code></pre>
|
||||
<p><strong>Example 4: With</strong></p>
|
||||
<p>An alternative way of writing “if” and then referencing the same value
|
||||
is to use “with” instead. With rebinds the context <code>.</code> within its scope,
|
||||
and skips the block if the variable is absent.</p>
|
||||
<p>The first example above could be simplified as:</p>
|
||||
<pre><code>{{ with .Params.title }}<h4>{{ . }}</h4>{{ end }}
|
||||
<pre><code>{{ with .Params.title }}<h4>{{ . }}</h4>{{ end }}
|
||||
</code></pre>
|
||||
<p><strong>Example 5: If -> Else If</strong></p>
|
||||
<pre><code>{{ if isset .Params "alt" }}
|
||||
{{ index .Params "alt" }}
|
||||
{{ else if isset .Params "caption" }}
|
||||
{{ index .Params "caption" }}
|
||||
{{ end }}
|
||||
<pre><code>{{ if isset .Params "alt" }}
|
||||
{{ index .Params "alt" }}
|
||||
{{ else if isset .Params "caption" }}
|
||||
{{ index .Params "caption" }}
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<h2 id="pipes">
|
||||
Pipes
|
||||
@@ -370,26 +370,26 @@ pipes is that they only can work with a single value and that value
|
||||
becomes the last parameter of the next pipeline.</p>
|
||||
<p>A few simple examples should help convey how to use the pipe.</p>
|
||||
<p><strong>Example 1 :</strong></p>
|
||||
<pre><code>{{ if eq 1 1 }} Same {{ end }}
|
||||
<pre><code>{{ if eq 1 1 }} Same {{ end }}
|
||||
</code></pre>
|
||||
<p>is the same as</p>
|
||||
<pre><code>{{ eq 1 1 | if }} Same {{ end }}
|
||||
<pre><code>{{ eq 1 1 | if }} Same {{ end }}
|
||||
</code></pre>
|
||||
<p>It does look odd to place the if at the end, but it does provide a good
|
||||
illustration of how to use the pipes.</p>
|
||||
<p><strong>Example 2 :</strong></p>
|
||||
<pre><code>{{ index .Params "disqus_url" | html }}
|
||||
<pre><code>{{ index .Params "disqus_url" | html }}
|
||||
</code></pre>
|
||||
<p>Access the page parameter called “disqus_url” and escape the HTML.</p>
|
||||
<p><strong>Example 3 :</strong></p>
|
||||
<pre><code>{{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
|
||||
Stuff Here
|
||||
{{ end }}
|
||||
<pre><code>{{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}}
|
||||
Stuff Here
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<p>Could be rewritten as</p>
|
||||
<pre><code>{{ isset .Params "caption" | or isset .Params "title" | or isset .Params "attr" | if }}
|
||||
Stuff Here
|
||||
{{ end }}
|
||||
<pre><code>{{ isset .Params "caption" | or isset .Params "title" | or isset .Params "attr" | if }}
|
||||
Stuff Here
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<h2 id="context-aka-the-dot">
|
||||
Context (aka. the dot)
|
||||
@@ -403,10 +403,10 @@ will no longer refer to the data available to the entire page. If you need to
|
||||
access this from within the loop you will likely want to set it to a variable
|
||||
instead of depending on the context.</p>
|
||||
<p><strong>Example:</strong></p>
|
||||
<pre><code> {{ $title := .Site.Title }}
|
||||
{{ range .Params.tags }}
|
||||
<li> <a href="{{ $baseurl }}/tags/{{ . | urlize }}">{{ . }}</a> - {{ $title }} </li>
|
||||
{{ end }}
|
||||
<pre><code> {{ $title := .Site.Title }}
|
||||
{{ range .Params.tags }}
|
||||
<li> <a href="{{ $baseurl }}/tags/{{ . | urlize }}">{{ . }}</a> - {{ $title }} </li>
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<p>Notice how once we have entered the loop the value of {{ . }} has changed. We
|
||||
have defined a variable outside of the loop so we have access to it from within
|
||||
@@ -431,21 +431,21 @@ benefit from having the table of contents provided. Sometimes the TOC just
|
||||
doesn’t make a lot of sense. We’ve defined a variable in our front matter
|
||||
of some pages to turn off the TOC from being displayed.</p>
|
||||
<p>Here is the example front matter:</p>
|
||||
<pre tabindex="0"><code>---
|
||||
title: "Permalinks"
|
||||
date: "2013-11-18"
|
||||
aliases:
|
||||
- "/doc/permalinks/"
|
||||
groups: ["extras"]
|
||||
groups_weight: 30
|
||||
notoc: true
|
||||
<pre tabindex="0"><code>---
|
||||
title: "Permalinks"
|
||||
date: "2013-11-18"
|
||||
aliases:
|
||||
- "/doc/permalinks/"
|
||||
groups: ["extras"]
|
||||
groups_weight: 30
|
||||
notoc: true
|
||||
---
|
||||
</code></pre><p>Here is the corresponding code inside of the template:</p>
|
||||
<pre><code> {{ if not .Params.notoc }}
|
||||
<div id="toc" class="well col-md-4 col-sm-6">
|
||||
{{ .TableOfContents }}
|
||||
</div>
|
||||
{{ end }}
|
||||
<pre><code> {{ if not .Params.notoc }}
|
||||
<div id="toc" class="well col-md-4 col-sm-6">
|
||||
{{ .TableOfContents }}
|
||||
</div>
|
||||
{{ end }}
|
||||
</code></pre>
|
||||
<h2 id="using-site-config-parameters">
|
||||
Using Site (config) Parameters
|
||||
@@ -463,24 +463,24 @@ provided if the <code>CopyrightHTML</code> parameter is provided, and if it is g
|
||||
you would declare it to be HTML-safe, so that the HTML entity is not escaped
|
||||
again. This would let you easily update just your top-level config file each
|
||||
January 1st, instead of hunting through your templates.</p>
|
||||
<pre tabindex="0"><code>{{if .Site.Params.CopyrightHTML}}<footer>
|
||||
<div class="text-center">{{.Site.Params.CopyrightHTML | safeHtml}}</div>
|
||||
<pre tabindex="0"><code>{{if .Site.Params.CopyrightHTML}}<footer>
|
||||
<div class="text-center">{{.Site.Params.CopyrightHTML | safeHtml}}</div>
|
||||
</footer>{{end}}
|
||||
</code></pre><p>An alternative way of writing the “if” and then referencing the same value
|
||||
is to use “with” instead. With rebinds the context <code>.</code> within its scope,
|
||||
and skips the block if the variable is absent:</p>
|
||||
<pre tabindex="0"><code>{{with .Site.Params.TwitterUser}}<span class="twitter">
|
||||
<a href="https://twitter.com/{{.}}" rel="author">
|
||||
<img src="/images/twitter.png" width="48" height="48" title="Twitter: {{.}}"
|
||||
alt="Twitter"></a>
|
||||
<pre tabindex="0"><code>{{with .Site.Params.TwitterUser}}<span class="twitter">
|
||||
<a href="https://twitter.com/{{.}}" rel="author">
|
||||
<img src="/images/twitter.png" width="48" height="48" title="Twitter: {{.}}"
|
||||
alt="Twitter"></a>
|
||||
</span>{{end}}
|
||||
</code></pre><p>Finally, if you want to pull “magic constants” out of your layouts, you can do
|
||||
so, such as in this example:</p>
|
||||
<pre tabindex="0"><code><nav class="recent">
|
||||
<h1>Recent Posts</h1>
|
||||
<ul>{{range first .Site.Params.SidebarRecentLimit .Site.Recent}}
|
||||
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
|
||||
{{end}}</ul>
|
||||
<pre tabindex="0"><code><nav class="recent">
|
||||
<h1>Recent Posts</h1>
|
||||
<ul>{{range first .Site.Params.SidebarRecentLimit .Site.Recent}}
|
||||
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
|
||||
{{end}}</ul>
|
||||
</nav>
|
||||
</code></pre></article>
|
||||
|
||||
@@ -564,7 +564,7 @@ so, such as in this example:</p>
|
||||
|
||||
</main>
|
||||
|
||||
<script src="/js/pangu.min.js"></script>
|
||||
<script src="/js/pangu.min.js"></script>
|
||||
<script>pangu.spacingPage();</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -29,9 +29,9 @@ Follow the following steps:" />
|
||||
Made with Book Theme
|
||||
https://github.com/alex-shpak/hugo-book
|
||||
-->
|
||||
<link rel="stylesheet" href="https://academics.jamesflare.com/css/custom.css">
|
||||
|
||||
<script type="text/javascript" src="https://academics.jamesflare.com/js/custom.js"></script>
|
||||
<link rel="stylesheet" href="https://academics.jamesflare.com/css/custom.css">
|
||||
|
||||
<script type="text/javascript" src="https://academics.jamesflare.com/js/custom.js"></script>
|
||||
<script>pangu.spacingPage();</script>
|
||||
</head>
|
||||
<body dir="ltr">
|
||||
@@ -223,14 +223,14 @@ you are reading right now.</p>
|
||||
<li>Open your browser to http://localhost:1313</li>
|
||||
</ol>
|
||||
<p>Corresponding pseudo commands:</p>
|
||||
<pre><code>git clone https://github.com/spf13/hugo
|
||||
cd hugo
|
||||
/path/to/where/you/installed/hugo server --source=./docs
|
||||
> 29 pages created
|
||||
> 0 tags index created
|
||||
> in 27 ms
|
||||
> Web Server is available at http://localhost:1313
|
||||
> Press ctrl+c to stop
|
||||
<pre><code>git clone https://github.com/spf13/hugo
|
||||
cd hugo
|
||||
/path/to/where/you/installed/hugo server --source=./docs
|
||||
> 29 pages created
|
||||
> 0 tags index created
|
||||
> in 27 ms
|
||||
> Web Server is available at http://localhost:1313
|
||||
> Press ctrl+c to stop
|
||||
</code></pre>
|
||||
<p>Once you’ve gotten here, follow along the rest of this page on your local build.</p>
|
||||
<h2 id="step-3-change-the-docs-site">
|
||||
@@ -239,13 +239,13 @@ cd hugo
|
||||
</h2>
|
||||
<p>Stop the Hugo process by hitting Ctrl+C.</p>
|
||||
<p>Now we are going to run hugo again, but this time with hugo in watch mode.</p>
|
||||
<pre><code>/path/to/hugo/from/step/1/hugo server --source=./docs --watch
|
||||
> 29 pages created
|
||||
> 0 tags index created
|
||||
> in 27 ms
|
||||
> Web Server is available at http://localhost:1313
|
||||
> Watching for changes in /Users/spf13/Code/hugo/docs/content
|
||||
> Press ctrl+c to stop
|
||||
<pre><code>/path/to/hugo/from/step/1/hugo server --source=./docs --watch
|
||||
> 29 pages created
|
||||
> 0 tags index created
|
||||
> in 27 ms
|
||||
> Web Server is available at http://localhost:1313
|
||||
> Watching for changes in /Users/spf13/Code/hugo/docs/content
|
||||
> Press ctrl+c to stop
|
||||
</code></pre>
|
||||
<p>Open your <a href="http://vim.spf13.com">favorite editor</a> and change one of the source
|
||||
content pages. How about changing this very file to <em>fix the typo</em>. How about changing this very file to <em>fix the typo</em>.</p>
|
||||
@@ -253,11 +253,11 @@ content pages. How about changing this very file to <em>fix the typo</em>. How a
|
||||
are located at the same relative location as the url, in our case
|
||||
<code>docs/content/overview/quickstart.md</code>.</p>
|
||||
<p>Change and save this file.. Notice what happened in your terminal.</p>
|
||||
<pre><code>> Change detected, rebuilding site
|
||||
|
||||
> 29 pages created
|
||||
> 0 tags index created
|
||||
> in 26 ms
|
||||
<pre><code>> Change detected, rebuilding site
|
||||
|
||||
> 29 pages created
|
||||
> 0 tags index created
|
||||
> in 26 ms
|
||||
</code></pre>
|
||||
<p>Refresh the browser and observe that the typo is now fixed.</p>
|
||||
<p>Notice how quick that was. Try to refresh the site before it’s finished building. I double dare you.
|
||||
@@ -335,7 +335,7 @@ Having nearly instant feedback enables you to have your creativity flow without
|
||||
|
||||
</main>
|
||||
|
||||
<script src="/js/pangu.min.js"></script>
|
||||
<script src="/js/pangu.min.js"></script>
|
||||
<script>pangu.spacingPage();</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user