What is the syntax for overriding a site/page parameter value in a template? I searched the discourse threads but I didn’t find a working code syntax.
This is my incentive: we used Hugo to create a documentation site that has some versioned content. For reasons that I will not go into, the content for the different versions is currently saved in the same GitHub branch. Versioned site sections have latest-release/ and vX.Y/ directories (e.g., v1.0/). We have global site variables (params) for storing the latest version number and all supported version numbers. We also have a shortcode to return the latest produt version number, which we use in the MD content files.
Because Hugo requires unique navigation-menu page identifiers across the site, we also had to use
<version> in the
parent key values of each page.
When we release a new version, we would need to duplicate the current latest-release/ directory to a new vX.Y/ directory, and replace all version-number references that are intended to refer to the “active” version of the ancestor version directory, both in the front matter and in the content. We could do this with a script, which is our current plan, but I was hoping there was a way to handle this with Hugo. This was my thought:
- Define a global product-version site param / data param that will be initialized to the latest version number.
- In the layout template files, identify if the current page is a descendant of a /vX.Y/ directory (which we can do by parsing the page URL), and if so, override the version-number param configuration for the specific page. In addition, the pages would set
parentBase params, and the layout will define the
main.menu.identifier/parent params for the page by appending
-<version> to the configured base number.
- Define a product-version shortcode that returns the page’s configured version number, or the default global site configuration if there is no unique page configuration.
This way, we should be able to simply copy an existing <doc section>/latest-release/ directory to a <doc section>/vX.Y/ directory and the page menu IDs and active-version shortcode references will all be updated automatically (via the template) to match the version number set in the ancestor version directory, without the need for any manual or scripting editing of the pages. But this design requires the ability to update page params from the layout template, which I wasn’t able to do far. So for the time being, I’m going with a scripting solution that will configure the version number in the front matter of each page in a new versioned-content directory to override global site configurations (and I can still have an active-version shortcode that returns either the page version configuration or the global site configuration if the former doesn’t exist, so there would be no need to edit the MD content). We would also need to update our cross-reference shortcode so that veresioned-content references will refer to the pages for the active version.