Troubleshooting
Context
A template accepts a context, which is data passed into the template. A template can be one of the following:
- simple value
- object
- method on an object
Usually, the current context is a the Page object. You can rebind the context to another value or object with range or with blocks. with is commonly used to change the context. For example, if you are within a shortcode but need to access the Page context, you can rebind it within the with block:
{{ range slice "one" "two" "three" (dict "test" 1) }}
<p>{{ . }}</p>
{{ end }}
// rebind to page
{{ with .Page }}
<p>{{ . }}</p>
{{ end }}
.Page could have been any value, including a string.
The context in a shortcode always refers to the shortcode itself, not the outer context. For example, to access the title in the Page context in a shortcode, you must use .Page.Title rather than .Title. In contrast, a layout page can refer to the same value with only .Title.
Access the outer context–in a shortcode, the Page context–by prepending the value with a $:
{{ with "dogs" }}
<p>{{ $.Page }} - {{ . }}</p> // [Page context] - dogs
{{ end }}
Actions
A template action is any expression enclosed in double curly braces ({{ }}) that evaluates data, executes a function, controls the flow, etc.
Variables
A variable is a user-defined value prepended with a $. It can hold any of the following:
- string
- integer
- floating point
- boolean
- slice
- map
- object
For slices, you can access a specific value with the index keyword:
{{ $odds := slice 1 3 5 7 9}}
<p>Seconds index: {{ index $odds 2 }}</p>
Create a map with the dict keyword. You can access map values with dot notation:
{{ $myMap := dict "one" 1 "two" 2 "three" 3 "four" 4 }}
<p>$myMap.two: {{ $myMap.two }}</p>
Functions
There are a lot of functions. Use the alias when available.
hugo-vals shortcode
page-methods
Resource methods
Many resource methods work with images.
Content
Returns the content of the resource, usually from the assets/ directory. You need to first use with and the .Get method to retrieve the resource, which also changes the context to that resource. Then, you can use .Content to work with the contents of the file.
This returns a template.HTML when it is a page, or it returns a string. Use this to get JS, CSS, image, and other pages.
Content:
52GetRemote
Data
The .Data method returns the body of an HTTP request. You send the HTTP request with resource.GetRemote.
It has these methods:
- ContentLength
- ContenType
- Headers
- Status
- StatusCode
- TransferEncoding
Err
Returns the error message if a resource.GetRemote HTTP request fails. By default, Hugo fails the build if the request returns an error. You can handle the error and fail the build with errorf. You can log the error as a warning with warnf rather than fail the build.
MediaType
Media type of the given resource. It has methods to return the type, main type, subtype, and more.
text/plainResourceType
Returns the main type of the resource's media type:
text textName
Returns the name of the resource or its file path as a fallback. If it is a page resource in a page bundle and you create an element in the resources array in the front matter, the Name method returns the value of the name parameter. Otherwise, it returns the filepath.
For remote resources, it returns a hashed file name.
/test/test.txtParams
Only works with page resources. Returns values defined in the resources.params front matter.
Permalink and RelPermalink
Returns and publishes the resource's permalink (absolute path) or relpermalink (relative path). It publishes the resource to the public/ directory with a hash:
https://rjseymour66.github.io/test/test.txtTitle
For a page resource, returns the title defined in the front matter. For a global resource, returns the relative path or hashed file name of the resource.
/test/test.txt