Referencing CUSTOM section Params/Variables from _index.md in Templates

Hi! My Hugo site (http://homebody.media) is to host my podcasts. Each podcast has its own subscribe links and social media accounts. I’m trying to display that information in each post without having to supply it in each post’s frontmatter.

To format my RSS files, I made an _index.md for each section containing it’s title as the sole line (as frontmatter). When I attempt to put the other variables (Twitter, for example) there, I am unable to get it to display on a post. Am I doing this incorrectly?

I’m super new to Go so I apologize if I misused any terms.

Thanks!

Without looking at your template, or at least some snippets of what you’ve tried, it’s hard to be sure exactly what the problem is… I’m assuming you want to edit your theme, or a layout, to display something you put in the front matter?

If that’s the case then it’s pretty easy; if you have a “twitter” variable in the front matter, you can write it out in the theme/layout by doing something like {{ .Params.twitter }}; there are a few special values (including the title) which are promoted to variables directly within the page (i.e., you can access the title as {{ .Title }} instead of {{ .Params.title }}, maybe that’s what is tripping you up…

An example (even using a variable called “twitter”): https://github.com/factopolis/factopolis/blob/master/themes/factopolis/layouts/partials/head.html#L106

If that’s not what you’re trying to do, please try to be a bit more specific. Code and file names are particularly helpful for establishing the context.

Thanks for the reply!

That’s exactly what I’m trying to do. When I try {{ .Params.Twitter }} or {{ .Params.twitter}} nothing happens. I’ve included a screenshot of my _index.md.

Screenshot from 2017-08-25 01-28-39

Are you in a loop or something which may have altered the scope? Try {{ $.Params.twitter }}, if that doesn’t work take a look at {{ $ }} to see what it is.

Jeff,

I’m in the header, though it made no difference outside of it – not in any loop or if statement.

When I try $.Params.Twitter (or .Params.Twitter, is there a difference?) nothing is returned though no error comes when the template is built

When I try $, Page(“PAGENAME”) is returned. Weird.

When I try $.Params.Twitter (or .Params.Twitter, is there a difference?) nothing is returned though no error comes when the template is built

Yes. $ is a variable which is assigned as kind of the root of the template. It’s kind of like specifying the global scope. .Params.Twitter means the Twitter property of the Params variable in the current scope, $.Params.Twitter means the Twitter property of the Params property of the $ variable. If you’re in a loop or some other construct which modifies the scope they are very different.

When I try $, Page(“PAGENAME”) is returned. Weird.

That’s expected. {{ $.Params }} should give you a map of the Params property for the page, one of which should be the “twitter” key and “SmarkOutPod” value. If that’s not the case then your front matter isn’t making it into the page’s data structure and you should take a close look at your directory structure.

I’ve been able to get to that to work when pulling from the frontmatter of an individual post. What I’m trying to do is pull a parameter from the frontmatter of the entire section’s _index.md file. Would this work any differently?

Like when I view post #1 “section/001.md”, I want to pull the Twitter account from “section/_index.md”.

Try $.Parent.Params.twitter.

BINGO. Jeff, you’re the best. Thanks!

Hopefully OK to continue on this old issue. I have the same problem, but the solution doesn’t work.

In my config.toml file, I have changed the directory setup:
[permalinks]
categories = “/inspiration/category/:slug/”

When I try {{ $ }} , it returns Page(“PAGENAME”), but this is obviously not matching the pagePath in my browser.

Browser pathname is: /inspiration/category/culture/ (as it should due to the permalinks)
PAGENAME is: /categories/culture (based on the taxonomy “categories” I’m using

I have tried creating _index.md files in multiple locations, but they don’t seem to be picked up:

  • /categories/culture/_index.md
  • /inspiration/caregory/culture/_index.md
  • /categories/culture.md

Is there a way to use the params (ie Params.twitter) from a file to populate the template? Or will this simply never work as long as I continue to work with the permalink structure I’m using?

Permalink structure should not have anything to do with it. I’m not sure I understand your issue, maybe you have a repo to share?

Thanks Regis! Unfortunately I can’t share the repo at this time.
Glad to hear the permalink structure is not causing the issue.

Let me try to explain it a bit better.

I have a taxonomy called atmosphere. One of the terms is family-friendly. I want to use the parameters from the YAML of the family-friendly page to change the frontend of the page.

Capture
If this was my YAML with parameters, I would like to include the value of the parameter image to include the image on the atmosphere/family-friendly page.

For the standard categories taxonomy, I can include an _index.md file in the respective folder of the term and it will show the image. For the taxonomy ‘atmosphere’ this doesn’t seem to work. It doesn’t seem to pick up on the _index.md file in the folder and include the image in the frontend.

I included the {{ $ }} in the template to find out the pagename (as recommended in previously in this thread). It is showing the page value /atmosphere/family-friendly. I would expect that if I would include the _index.md file in the /atmosphere/family-friendly folder of my site, that it would be included. But this is not the case. When I build my site, it returns the index.html file I created in /atmosphere/family-friendly in the permalink location /collection/atmosphere/family-friendly, not the /atmosphere/family-friendly.

This is my permalinks structure
Capture2

And this is my taxonomies structure:
Capture3

I find it strange that what works for the standard categories taxonomy, doesn’t work for my custom atmosphere taxonomy.

Thanks for any help/direction you may be able to provide.

Hard to find if anything else is amiss outside of those two screenshot you shared. What about recreating a minimal repo for us which reproduce the issue?