Variables in markdown


#1

I tried to use a variable in markdown like {{ .Site.Params.email }} but it doesn’t parse that. Is there a way around it?


#2

No. You can use only shortcodes inside .md content.

You can use this variable either from within the shortcode or from the single.html template.


#3

@onedrawingperday what would that look like (the variable from within a shortcode)? I don’t want to touch the single.html . It is decoupled as a theme so I don’t want to put anything directly about me into it


#4

See this from yesterday. Particularly the last code block applies in your case.

Sorry I can’t elaborate further as I’m out.


#5

Thanks mate


#6

I’ve come across the same issue, but shortcodes wont really solve my situation. If I wanted to do something like include an external file, read values from data, or even basic itteration from a markdown file, then I can’t. Creating a shortcode for each case I need to use it just feels horrible!

Ideally, opt-in rendering of markdown content would be awesome. Is there any reason it’s not supported? Every ticket i’ve seen just says use shortcodes, which doesn’t really work in my case.


#7

Actually you can use the full range of Hugo’s template functions for things like that without shortcodes.

All you need to do is create a partial in your single.html template, that will be rendered only if a front-matter parameter exists.

Within this partial you can get a different file or data values by specifying a URL in yet another front-matter parameter (if it exists).

Or even iterate over a set of other parameters and render them e.g. in a list.

There are ways to control how parts of your content are rendered with Hugo.


#8

I wonder why. I think variables in Markdown files would be great. I need to double check but I’m pretty sure you can do it with Jekyll.


#9

I’m certain you can do it with Jekyll. But it isn’t a big deal anyway. One thing I learned from migrating from Jekyll is to keep my markdown as decent as possible. Less shortcodes and if possible no variables at all but it’d be a nice to have. Also using backtics to make code pretexts over using some shortcode by the framework


#10
  • Having code in content is a bad idea in general (I could debate the “why” here, but there are plenty of resources online)
  • Shortcodes is a good compromise, it supplies a fine separation of content and logic/layout
  • With Hugo >= 0.24 you can use full template syntax in Archetype files used by hugo new, which is cooler than it sounds.

#11

@bep Even with the new 0.52 inlineShortcodes we aren’t allowed to access variables directly from the markdown?


#12

See this thread about the Param shortcode: