When the template is rendered, the frontmatter evaluation is done. So you can’t set it later on because it won’t be read (uneducated guess). It would probably lead to complications further down the road anyway.
Have a look at .Scratch. This is your notepad to save values while doing templating.
There are some notes about how to access these values global or in partials - so be careful how to define your variables. It’s pretty straight forward if you want to carry values around.
Thank you guys!
It’s a shortcoming of Hugo that a reference to a partial or data file can’t be stored in a parameter. OK, I will find a workaround. @davidsneighbour, do you think I can store a reference in a .Scratch - and it will be usable?? Do you know how I can set .Scratch in .Params?
I was looking for something like
{{ if .Params.supplier1 }}
{{ with $.Site.Data.prices.supplier1 }}
{{ end }}
{{ if .Params.supplier2 }}
{{ with $.Site.Data.prices.supplier2 }}
{{ end }}
I don’t think of it as “shortcoming” because it’s possible. It’s just not the task of a template to start configuring something. You can store whatever you want in a GLOBAL scratch and access it where ever. You have to move away from “let’s put it into .Params” and into “let’s put it into my own $supplierselection”. Then it works.
Apart from this: maybe I understand your question completely wrong:
Frontmatter (YAML):
supplier:
name: "supplier 1"
address: "somewhere"
Somewhere in the page layout:
{{ with .Params.supplier }}
{{ .name }}
{{ .address }}
{{ end }}
That is possible. can be deeper structured. You need to have the “dot” on the page-“object” for this to work or just use another variable where it is located.
If you want to define “use supplier 1” via frontmatter, it’s the same way. You for instance add the supplier to a data file, then in the frontmatter you add supplier: "supplier1" Then you use the index function to write something like:
{{ with index .Site.Data.suppliers .Params.supplier }}
...
Here the data file has to have a key supplier1:, then below it name and address like above.
If not let me know and I’ll write a quick sample of how a global scratch could look like.
@davidsneighbour Thank you very much! I’ll try to figure it out.
My current idea is to have a dedicated template for each of the suppliers, but this may be a more elegant way if it works out.
Then add /layouts/partials/subfolderinpartials/supplier1.html and supplier2.html.
But I agree, my solution might be better, because it separates your layout from the content (in data). And you just add a third supplier by adding it to data.
Have a look at printf and index (see links below).
printfPRINTs Formatted content.
It’s very powerful, but for now, all you need to know is that %s means print a string.
So printf "%s%s%" param1 param2 param3 means print these three params without space after each other and use their values as string (%s, you could read them as anything else from integers to dates to whatever).
The partial looks in the folder /layouts/partials for a file and the rest is the formatted URL. So, you might go just with 2 %s and print the suppliername and “.html” in that place or you have a subfolder.
Other than than I think you understand it. The last line with the partial and print loads either supplier1 or supplier2.html from your partials subfolder.
No, I can’t Because you post only the things that YOU find connected to the issue. Could you please create a simple repository with a working demo of what throws the error? like three four sample content files and all connected layout files. No need to add design. Even better if you can make the original repo public.
Everyone trying to help will need more info and appreciates something they can use locally to test it out.
This is the syntax I need: {{ partial (printf `%s%s%s` `suppliers/` (index .Params.supplier) `.html` ) }}
However I’m getting an error message again: executing “main” at <partial (printf `%s%s%s` `suppliers/` (index .Params.supplier) `.html`)>: error calling partial: partial “suppliers/supplier1.html” not found. - This happens while _default/suppliers/supplier1.html exists.
I have 2 directories that contain partials: layouts/_default and layouts/partials.
The main product template is layouts/_default/single.html
If suppliers directory is in _default, the server does not start - the error message is “partial not found”. If it is in partials directory - the partial is found, the server starts, but the partial suppliers1.html is not loaded into the template…
I’ve removed the suppliers folder and put supplier1.html into partials folder: partials/supplier1.html
The fundtion line is: {{ partial (printf `%s%s` (index .Params.supplier) `.html` ) }}
To sum it all up - if supplier1.html is not found, the server does not start. If it is found, the server starts, but it is not loaded into the template.
Why it is not loaded?!