Conditional Text in Hugo

Hi there,

I’m trying to implement something in Hugo which is possible using DITA.

Let’s say I have a user manual. The content in it can be applied to a number of different products.

I want to have the text change based on the conditional product name.

So if content is for product “foo” display the text with the product name “foo”, for the product “bar” display the same text with the product name “bar”.

What is the best way to do this with Hugo?

Edit: to clarify, I want to be able to write the content in one place, then change the product name in the content depending on the model selected.

So the intro page for product one would say “Hello, welcome to the PRODUCT ONE page” and the intro page for product two would say “Hello, welcome to the PRODUCT TWO page”.


You can put arbitrary data in your data folder. That data can be used in your content pages ( and/or shortcodes ):

See this page in the docs:

Maybe you can leverage this

Hi Sascha,

Not sure this exactly solves either.

The main issue is that the content is the exact same, but just with different model numbers.

I think this should mean using templates for these pages, and empty markdown files on the content side, then apply the templates based on the markdown file.

So a markdown file for intro page would apply the template intro page with it’s variables for model numbers, and would include front matter stating that model number.

However, the way templates seem to work in hugo, I can only apply one template (single) to all pages?

I’d create a folder in content to hold your manual content and each markdown file be a piece of content for each product. Use front matter in each piece of content to associate it with products maybe using a list so it can apply to multiple products.

Or use related content front matter the headless feature may be useful if you’d like to use markdown to import / query but not have each individual content be rendered in to public.

Hey Peter,

Would this allow me to write the content once and include the product name depending on the md file/page they’re accessing?

Sure the list of related products which you put in the manual content markdown files should match at least the file name or something in the product files front matter so you can range over it using where

I think I might be missing this, so apologies if it’s flying over my head. Did you mean including the generic markdown files within a product markdown file. How does it pick up the product name then?

If you have a page per product, you could simply either have your single.html template grab product information from your frontmatter, and put that in some fixed area of the page, or, if you need the location to be variable within your markdown, use a shortcode that does a similar thing. You could put the datapoints in a data file and pinpoint them, or, you could put the datapoints in frontmatter (assuming one markdown file per product), and pull from there.

Hey Rick,

Well, for each product there’s a bunch of pages (more than listed in example below)

so for example:

  • Product one intro

  • Product one getting started

  • Product one connecting to network

  • Product one features

  • Product two intro

  • Product two getting started

  • Product two connecting to network

  • Product two features

So I guess, what I want is a bunch of templates for each type of page, or a single template that includes a bunch of templates that change based on the page type. Then they would also include the product model in the templated content.

The approach I tried (that didn’t work):

In my single:

{{if eq .Type “productintropage”}}
{{ partial “/Users/patrickh/Workspaceh/Vagrant/NPM/adyen-docs/site/layouts/partials/productintropage.html” . }}

The productintropage partial looks like:

<h 1> {{ .Site.Params.productmodel }} User Manual </h 1>

it doesn’t work, but I’m very new to the Liquid templates and how they work.

So basically the logic would be:

  • Check which type of page (for example intro page)
  • Load the template for that type of page (intro page template)
  • In the template have variables that are assigned by frontmatter (product name)

Then I could have a single source for the intro page content, but be able to change the product name based on where they are in the docs.

I’m not sure this is how templates are supposed to be used…

You don’t need to include the entire PATH to your partial. You only need to call it like so {{ partial "productintropage.html" . }} And Hugo will find it.

It’s not Liquid. It’s Go Templates.

Ah, sorry someone (IRL) corrected me earlier today when I called them go templates :o So the approach I’ve outlined above should work? Or was your reply just specific to the partial?

Your approach should work but you don’t need to enter the partial’s PATH in your template.

1 Like

OK cool, thanks for this. I’ll give it a shot tomorrow and report back here if I’ve no luck.

OK, so this didn’t work but I’ve rethought the approach. I’d like to have all content in MD files that can be accessed for review by anyone, so doesn’t make sense to have it in templates.

I’m now stuck with an issue on the new approach I’m using:

  1. I’ve made my own shortcode that accesses a front matter variable for productName.

  2. In the docdock theme there’s an excerpt/excerpt-include which includes content from one markdown file in another.

  3. I’ve created a markdown file with the text This is the {{% frontmattervar %}} user guide, wrapped in {{% excerpt %}} tags

The problem with this is that the content included is included as raw markdown, so the variable doesn’t work when it’s in the reuse file. It does work if it’s directly in the content.

Any ideas why this might be? I thought it was because of the “markdownify” command in the excerpt-include shortcode, but this doesn’t seem to be the case… (for info on what I’m talking about:

I’m wondering if there’s a way to escape the shortcode so that Hugo interprets it as such instead of markdown…

First of all the DockDock theme is pretty complex and I have no experience with it. You may have better luck asking the theme’s author.

With that said when one uses {{% shortcode %}} the % tells Hugo to interpret it as Markdown.
Instead when one uses {{< shortcode >}} the angled brackets tell Hugo to interpret the shortcode as HTML.