Can I use a custom front matter variable for this?

Edit: I don’t know how to get html to post as code in here. So I replace the code with gists.

Kind of a silly question, cause I feel like I should have figured this out by now. But I’ve come up short.

Essentially I have a credit: Photographer’s Name in my front matter. And I’d like to pass that into a caption, which is part of a shortcode right now that uses an image resizing template.

Right now:


{{< img src="200520142727.jpg" alt="" caption="Despite social distancing requirements, a crowd gathers along West Cliff Drive to get a view of the gray whale carcass being towed out to sea on Tuesday afternoon.">}}

That works…

But I’d like a pass a credit front matter if it’s available.

Soo something like…

But I can’t seem to pass a front matter variable to the image processing template. And since the figure tag is rendered from the shortcode, there’s now a way to inject that into the caption. Ultimately I’d like to pull the caption and credit from the exif data and if it’s not available, use the front matter and caption in the shortcode.

Thanks. Pretty excited about Hugo so far.

Either use three backticks above and below to create a code block:

Code block

Or use the code block button in the post editor i.e. </>

Also I edited your post above so that you can see.

Think of Hugo templates as the layers of an onion.

  • The shorcode template is one layer below the page that contains it.
  • That page also contains the front matter parameter you are trying to access from within the shortcode.
  • So you need to go higher up from within the shortcode.
  • That can be achieved by using the dollar sign $ and the .Page variable.

So the above should be if $

Also note that in Hugo a terser way to check for a condition is provided by the with function.

By using with if the condition is not found then the associated code block will be escaped -something that does not happen with the if conditional.

So a better way to write the above would be: with $

That works! Thank you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.