"Programming"

{{- $js := slice 
    (resources.Get "js/color-preference.js") 
    (resources.Get "js/off-canvas.js")
    (resources.Get "js/table-of-contents.js") 
    | resources.Concat "assets/js/docs.js" 
    | minify 
    | fingerprint
}}
  • resources.Get

    • This loads a single asset from the assets/  directory in your Hugo project.

    • So resources.Get "js/color-preference.js"  fetches that file as a Hugo Resource .

  • slice

    • The slice  function creates a list  of items.

    • Here, it creates a list of the three JS resources.

    • So after this step, you have a slice of three JS resources .

  • | resources.Concat "assets/js/docs.js"

    • The pipe ( | ) passes the slice to resources.Concat .

    • resources.Concat "assets/js/docs.js"   combines all the files  in the slice into a single file.

    • "assets/js/docs.js"  is the target name  for the concatenated file.

    • So now you have one JS file  combining the three original files.

  • | minify

    • This compresses the concatenated JS file to reduce its size.

    • Removes whitespace, comments, and sometimes shortens variable names.

    • Good for production, because it makes the site load faster.

  • | fingerprint

    • This adds a hash  to the filename, e.g., docs.3a7f9e.js .

    • Helps with cache busting : browsers will download a new version if the file changes.

  • {{- $js := ... }}

    • Finally, the entire processed resource is assigned to the variable $js .

    • You can then use it in your template, for example:

(( range $some_page := .Site.Sections ))
    <p>
        (( printf "%#v" $some_page.File.BaseFileName ))
    </p>
(( end ))
Go Templates
  • |

    • Pipe operator. Passes the value on the left as input to the function on the right.

  • ((-  / -))

    • Trim whitespace before or after the tag.

Variables
(( $var1 := "dog" ))
(( $var2 := "cat" ))
Conditionals
  • eq

    • Equal

  • lt

    • Less than

  • gt

    • Greater than

  • ge

    • Greater or equal to

(( if eq $var1 $var2 ))
    It is true
(( else ))
    It is false
(( end ))
(( if not (eq $var1 $var2) ))
    It is true
(( else ))
    It is false
(( end ))
(( if and (lt $var1 $var2) (lt $var1 $var3) ))
    It is true
(( else ))
    It is false
(( end ))

Functions / Methods

Functions
Range
(( range .Pages ))
    (( .Title )) <br>
(( end ))
  • When iterating over something, the variables accessed inside the loop are from the current iteration.

Methods
Site
  • Methods - Site .

  • Site.Home .

    (( .Site.Home.Permalink )) β†’ https://example.org/docs/ 
    (( .Site.Home.RelPermalink )) β†’ /docs/
    
  • Site.Pages .

    (( range .Site.Pages ))
      <h2><a HREF="(( .RelPermalink ))">(( .LinkTitle ))</a></h2>
    (( end ))
    
  • Site.Sections .

    // This structure
    content/
    β”œβ”€β”€ books/
    β”‚   β”œβ”€β”€ book-1.md
    β”‚   └── book-2.md
    β”œβ”€β”€ films/
    β”‚   β”œβ”€β”€ film-1.md
    β”‚   └── film-2.md
    └── _index.md
    
    // Templated like:
    (( range .Site.Sections ))
      <h2><a HREF="(( .RelPermalink ))">(( .LinkTitle ))</a></h2>
    (( end ))
    
    // Renders to:
    <h2><a HREF="/books/">Books</a></h2>
    <h2><a HREF="/films/">Films</a></h2>
    
  • Site.MainSections .

    // Toml
    mainSections = ['books', 'films']
    
    // GO
    (( .Site.MainSections )) β†’ [books films]
    
    • If mainSections  is not  defined in the site configuration, this method returns a slice with one elementβ€”the top-level section with the most pages.

Page
Pages