I was trying to load a partial with printf function. The partial is part of my self-made shopping-cart, it contains order configuration and price calculation, getting prices from .Site.data.prices. I have 2 suppliers at the moment and 2 partials (supplier1.html and supplier2.html) for order configuration and price calculation respectively.
I wanted to connect either this or that partial depending on .Params.supplier value, using this printf function:
I have 2 directories that contain partials: layouts/_default and layouts/partials.
The main product template is layouts/_default/single.html
If I put suppliers directory in _default, the server did not start - the error message was: error calling partial: partial “suppliers/supplier1.html” not found. If it was in partials directory - the partial was found, the server started, but the partial supplier1.html was not loaded into the template single.html.
I removed the suppliers folder and put supplier1.html into partials folder: partials/supplier1.html
The function line was: {{ partial (printf %s%s (index .Params.supplier) .html ) }}
To sum it all up - if supplier1.html was not found, the server did not start. If it was found, the server started, but did not load supplier1.html into the template.
Why it did not load the partial into the template?!
<!-- put it inside variable for clarity -->
{{ $partialPath := printf `suppliers/%s.html` .Params.supplier }}
{{ partial $partialPath . }} <!-- dont forget to pass the . (dot) context -->
[quote=“pamubay, post:2, topic:29898”]
Thank you very much @pamubay!
So, I can put this statement {{ $partialPath := printf `suppliers/%s.html` .Params.supplier }}
into the template where I want to insert the partial, and it will insert the partial.
The $partialPath is a variable that defines the path of the partial. The partial then loads the partial. So you add BOTH lines and the partial in question will be loaded.
So, printf assembles the partial name and path - that would e.g. look like “suppliers/supplier1.html” and stores it into the $partialPath variable.
But to insert it into a template I should use {{ partial $partialPath . }} statement.
Without that ( {{ partial (printf %s%s%ssuppliers/ (index .Params.supplier) .html ) }}) can not do that?
OK.
Thank you!