.Resources.Match Returns Page Missing Resources - hugo

A page on my site calls .Resources.Match to fetch a group of content files that I expect to have their own .Resources property, but do not. I would like to fetch the page, then get a resource. The content structure is like a page bundle, and I've added [[resources]] to the front matter, but neither .BundleType nor Resources is set (.Resources returns an empty array). How can I get the content, then the resource?
Directory
page/
├─ index.md
└- leaf_bundles/
└- leaf/
├─ index.md
└─ resource.etc
Template page/index.md
{{ $leaf := .Resources.Match "leaf_bindles/**/*.md" | index 0 }}
{{ $leaf.IsPage }} // => true
{{ $leaf.Resources }} // => []
Content Front Matter leaf/index.md
[[resources]]
name = "resource"
src = "resource.etc"
I assume that if I were to create a template for "leaf", .Resources would be populated in that context. Is it possible to fetch leaf bundle resources from a page returned itself by a call to .Match?

I was able to achieve the desired result -- accessing page bundle content and resources from a parent page -- using .Pages and the where operator. Instead of calling .Resources.Match to fetch the .md file of a page bundle, .Pages where "Type" "mySection" returns the page bundle in its entirety, including a pointer to its resources. I set the "type" in front matter, and am good to go.
Thanks to misterbisson whose post led to my solution.

Related

How can I have a different frontmatter for my about page in Hugo?

I'm using the PaperMod theme. I have the following params in my config.yml
---
# some predefined variables
params:
ShowReadingTime: true
ShowShareButtons: true
ShowBreadCrumbs: true
ShowCodeCopyButtons: true
---
The params work as expected for my posts. But then I have single pages (e.g. About page) that I don’t want to use the same params. I tried overriding the above values in the index.md of my /about/ directory, but it didn't work.
So I read more in the docs and blog posts about _index.md, but not sure if I figured it out right, it seems that I should do something like this instead:
config.yml // remove such params
content
posts
_index.md // only add params here
post1.md
post2.md
about.md // special page that doesn't need the params
But when I do such, the params I set don’t have any effect on post1, post2.
Am I doing this the right way? I thought I can think of content/posts as a section and each section would need a _index.md for its custom front matter variables.
I noticed that the theme uses the {{ .Param KEY }} function to obtain the value of these custom parameters.
The function first checks the page's parameters (front matter) then falls back to the global configuration file(s). In the global configuration file, custom parameters are specified under the params section, however, in the page's front matter, the custom parameters are specified at the root level.
I would set them in the config to make them site-wide variables. Then I would set different params in the page and overwrite with the logic below.
Referring to site-wide variables:
.Site.Params.yourparamkey
Referring to a page variable:
.Params.yourparamkey
Checking which one to use:
{{- if .Params.yourparamkey -}}
{{- $yourparamkey := .Params.yourparamkey -}}
{{- else -}}
{{- $yourparamkey := .Site.Params.yourparamkey -}}
{{- endif -}}
Using your param key:
{{ $yourparamkey }}

Read site configuration from a Hugo partial

I want to add a map to the main page of my blog. It needs to take a few parameters. I'm struggling to figure out how to configure this properly. I got it working by hard-coding the settings but that's not ideal for sharing my solution with others.
My problem is that I implemented it as a partial: {{ partial "map.html" (where site.RegularPages "Type" "in" site.Params.mainSections) }}
From my understanding I cannot access the .Site.Params variable in a partial. I've also been considering using a shortcode but that doesn't seem to be the right choice either because shortcodes can only be used in content, not in templates. I also don't want to add this into the index.html of the template directly as it is independent of the theme.
What is the correct way to achieve this?
.Site.Params == site.Params
see docs: https://gohugo.io/functions/site/#readout
That should resolve immediate issue. Otherwise: Smitop's solution.
(i.e. 'site' can be accessed globally)
You can pass the current context to a partial with .:
{{ partial "header.html" . }}
You can then access the context from within the partial as you would from the caller:
This site is called {{ .Site.Title }}.
Hugo doesn't directly support passing multiple arguments to a partial. If you want to pass things in addition to the context, you can pass a dictionary with all the values:
{{ partial "header.html" (dict "Ctx" . "Percent" "84") }}
And then from within the partial:
This site is called {{ .Ctx.Site.Title }}, and is {{ .Percent }}% awesome!

How to create a HUGO page with no direct links

Is there anyway to create a hugo page which has no direct links? I would like to be able to create a markdown file for a page but only allow people to find the page if they have a direct link.
Yes it is possible. It really depends on how you are generating your pages. As for now I am showing two ways you can do it.
Way 1: Your _default/list.html generates the list of links to your content. You can customize that to exclude a link. Watch the if conditional.
{{ range .RegularPages }}
{{ if (not in .Title "title of page to exclude") }}
<li>
{{.Date.Format "2006-01-02"}} | {{.Title}}
</li>
{{ end }}
{{ end }}
Way 2: Alternatively you can put an html file in the static folder of your hugo directory. If the directory does not exist, you can create it. No direct links will be generated for static contents unless you explicitly link to it from somewhere else.
You can put all pages you do not want to list into a separate content sub-folder and not include it in the menu.
For example, my setup:
content
├── blog
├── notes
└── voice
Where only voice and blog have menu items, so everything in notes can not be accessed by links from the site.

How to change the homepage in hugo?

How can I have /posts as homepage?
Should I redirect, change the baseURL in hugo 1config or make changes in
the theme 2config?
Footnotes
1 https://gohugo.io/getting-started/configuration/
2 https://github.com/luizdepra/hugo-coder/wiki/Configurations
You can modify the home.html file, as the index.html file is embedding it and there is nothing else in index.html
https://github.com/luizdepra/hugo-coder/blob/master/layouts/partials/home.html
Make the changes in the above file in theme/layouts/partials/home.html these changes will take effect on the site as soon as you save the file (if you are already running $ hugo server -D)
For me, it helped to add a layouts/index.html file to my theme. Here is its content:
{{ define "main" }}
{{ $pag := .Paginate (where site.RegularPages "Type" "in" site.Params.mainSections ) 6 }}
<div class="archive-body">
{{ range $pag.Pages }}
{{ .Render "li" }}
{{ end }}
</div>
{{ partial "pagination" . }}
{{ end }}
"li" is a partial HTML template, which renders a single page for me.
Then I had to specify mainSections in my config.toml. Since my content is located inside content/post directory, here is the configuration.
[params]
mainSections = ["post"]
Since this is a list, you should be able to add more than one section. For example, if your content is spread let's say between content/post and content/articles and so on. I haven't tried this, though.
I know this is an old question, but the easiest way for me to set a particular markdown page as the landing page was simply to create a layouts/index.html to override my theme's, and put this in it:
<script>window.location = "/mainlist"</script>
This way, I can keep all my theme's styling, not worry about editing templates, and just focus on creating the content. As a newcomer to hugo, this worked quite well as a replacement for Pelican's save_as: index.html.

How to display Hugo categories used to create list

In my Hugo list.html page that is accessed when a user clicks on a category, I'd like the user to be able to see which category they clicked on by displaying it.
I've tried the following code which I think is attempting to get it from the URL:
{{ range .Params.categories }}
{{ . }}
{{ end }}
And my config.toml includes the relevant lines:
[taxonomies]
tag = "tags"
category = "categories"
Presently, nothing is displayed, and no 'a' tags are generated.
The variable .Title is what you are looking for.
When an individual taxonomy term page is being generated, the variable .Title will be set to the current term (that is the actual tag or category).
When the list taxonomy page itself is being generated, it is set to the name of the taxonomy (e.g. tags).
This is different from .Site.Title which is set in the config.toml file.
This is also different from .Title for an individual post which is set from the file's front matter.
So, the following snippet from my website :
<title>{{ .Site.Title }} {{ with .Title }} | {{ . }}{{ end }}</title>
Works equally well for any kind of page since .Title will be automatically set as appropriate for the page type.

Resources