How to display a custome page variable

I got through the doc, a few posts around (and the github issue) but I couldn’t figure out how to display the content of a custom defined variable.

Let’s say I have the variable named “mycontent” in the header of my index.md, how do I do to display the content of the variable “mycontent” ?
I tried {{ .mycontent }}, {{ $.mycontent }}, {{ .Page.mycontent }}, {{ % .mycontent % }}, but can’t get it to display… :frowning:

I also would like to know how to define my own functions (to pretty print a set of variables) and how to create array/set/list of variable :slight_smile:

Thanks a lot, Hugo is a very nice work!

1 Like

The variable you created was defined in the frontmatter (the area seperated by +++ in TOML, --- for YAML or {} for JSON). To access those variables, just call .Params.mycontent.

The creation of own functions is a bit limited, because the functions you can access are defined in the source code of Go (which you use as a binary). Either you take a look at the basic functions of Go’s html/template package or you use Hugo’s custom template functions, which extend the standard template functions of html/template. There you’ll find some simple options to format trim, lowercase strings too. Otherwise ask explicitly for formatting options (with an example) or do a bit of research on you own.

And finally. you can create a list of elements in the frontmatter to. It depends on the format (TOML, JSON or YAML), that you chose for the frontmatter:

A simple example for TOML:

+++
myList = [
    "String one",
    "String two"
]
+++

And the YAML version:

---
myList: [
    "String one", 
    "plugins"
]
---

For further information about the frontmatter take a look at the corresponding section in the docs

Thanks for the fast and detailed answer !

Also, I may have wrongly expressed my need about “mycontent” variable. I do not actually just want to “access” it, but “display/print/echo” it, and the {{ .Params.mycontent }} only renders “{{ .Params.mycontent }}” on the webpage. How can I display its content?

This is Go template syntax and to be put inside Go templates in /layouts.

My best guess it that you put this into the content (md) file, and that doesn’t work. Look in the documentation about shortcodes for that scenario.

Perfect !
That’s what I was looking for, I just went a bit too fast on it, my bad.

I still struggle to get it work though. When I call

{{% myshortcode "my text" %}}

It displays “my text”, nevertheless when I call

{{% myshortcode .Params.mycontent %}}

it does not display anything, I can see in the log:

ERROR: 2015/08/13 index:10: unrecognized character in shortcode action: U+002E ‘.’. Note: Parameters with non-alphanumeric args must be quoted
ERROR: 2015/08/13 error processing shortcode shortcodes/myshortcode.html ERR: template: shortcodes/myshortcode.html:1:64: executing “shortcodes/myshortcode.html” at <index .Params 0>: error calling index: index out of range: 0

How do I use one of my page-variables into a shortcode as argument ?

The above doesn’t make sense and does not work.

If you want to use page params INSIDE a shortcode:

.Page.Params.mycontent

I suggest you read the documentation and look at some example repos.

I notice that you keep sending people to the documentation and seem annoyed by people asking. No offense, but if people keep asking about it is because the documentation is really not straightforward about it. We appreciate the help though.

For example, I have a type of content which is called report and in a new “report” frontmatter, say “energy.md”, I want to have:

--- 
pdf: "energy.pdf"
---

and then on the template have an “a” element:

<a href="/doc/{{ .Params.pdf }}" > Download PDF </a>

and have hugo render:

<a href="/doc/energy.pdf" > Download PDF </a>

To download the thing from one of the static folders.

But that doesn’t work, and as much as I have gone through the Variables section in the documentation I just can’t put two and two together. For the rest, Hugo is just awesome.

Edit: From various sources here I was able to figure it out, but only after a lot of poking around. I feel it would do a lot of good to a lot of people if it were right at the top of the “Variables” section of the documentation, or the tutorial on themes (templates). The answer was:

On the /content/report/energy.md

---
pdf: "energy.pdf"
---

and on the single.html template in the layouts/report/single.html

<a href={{ printf "/doc/%s" $.Params.pdf  }}> Download PDF </a>

which correctly renders:

<a href="/doc/energy.pdf"> Download PDF </a>

And the end result:

http://seeagt.github.io/report/energy/

3 Likes

No offense taken, but this is an open source project. If you want better documentation, the only guaranteed way is doing it yourself.

1 Like

Yeah, thanks. I will write a short section on this very thing and submit it. Anyway, I edited my post with the solution.

FFWD 2019 Q2 and situation is still the same, with documentation.

Regardless of the fact “this is a open source project” nothing is stoping you to ask for help in organizing the documentation first, then writing it second. It you would do it first, you would seem much less annoyed :slight_smile:

Maybe you did it in the meantime I do not know.

Closing this old thread. Any assistance on improving the documentation practically (rather than just saying it needs improved) is always welcome, as this is an open source project.

1 Like