Printf did not load a partial - why?

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:

{{ partial (printf %s%s%s suppliers/ (index .Params.supplier) .html ) }}

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?!

First, every partial templates needs to put under layouts/partials

let’s say we put all these suppliers partial templates under layouts/partials/suppliers

layouts/
  partials/
    suppliers/
      supplier1.html
      supplier2.html

you dynamic partiall call is like this:

<!-- 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.

But what is this: {{ partial $partialPath . }}?

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.

1 Like

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%s suppliers/ (index .Params.supplier) .html ) }}) can not do that?
OK.
Thank you!

well yes you can put it directly at partial first argument

{{ partial (printf `suppliers/%s.html` .Params.supplier) . }}

@pamubay, unfortunately that does not work. I have the code:

{{ partial (printf `weight/%s.html` .Params.grossweight) . }}

It trows an error:

error calling partial: partial "weight/2.html" not found

the existing partial is called weight2.html
How to get rid of that slash??
If I just remove it, the error message is:

error calling partial: partial "weight%!s(<nil>).html" not found

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.