How to pass variables to a partial template?

How to pass variables to a partial template? I have a code in the template

{{$ Alt: = .Pamams.alt_name}}
{{Partial “brand_ritmo_tpl”. }}

I want, that in brand_ritmo_tpl it was possible to use a variable $ alt. Is it possible?

1 Like

Something ala

{{partial "brand_ritmo_tpl"(dict "context" . "myvar" $val  }}

I try that code

{{ $alt := .Params.alt_name }} {{ partial "brand_canyon_tpl" (dict "context" . "alt_parent" $alt) }}
In brand_canyon_tpl.html the tag

{{ .alt_parent }} is not work. What tag should be inserted to display the value of the variable?

Spitballing here, but perhaps context.alt_parent?

.alt_parent

If that does not work, you have done something wrong (my guess: You have some other place calling the same partial with “.” as arg) …

I do not understand what place is at stake.
I have a code in template

{{ $alt := .Params.alt_name }} {{ partial "brand_canyon_tpl" (dict "context" . "alt_parent" $alt) }}

I want to transfer a variable to another template and output it there. (in brand_canyon_tpl.html)

I tried

{{ .context.alt_parent }}
{{ .alt_parent }}
{{ $.context.alt_parent }}

Nothing helps. What am I doing wrong? Can have someone’s own working combination. I would adapt it.

1 Like

@igramnet Again, just spitballing since I have about 10 second and can’t test. How about with .html added to the partial call, assuming you have said partial at layouts/partials/brand_ritmo_tpl.html?

Can you write a tested and comprehensive anwser when you have time? I am having problems with this too. Much much appreciated.

Does the space matter?

...tpl"(dict...
...tpl" (dict...

I have this code in index.html template
{{ partial "client_block" . (dict "feedback" "1") }}
How can I get the variable ‘feedback’ in client_block template ?

You are passing 3 arguments to partial; it can accept only 2.

See:

I know this is an old topic but it was the top search result for this problem so I’ll add my solution. I’m able to pass both the context and a variable to a partial and use both inside the partial this way.

Including the partial:

{{ partial "partialFileName" (dict "context" . "myVar" "val") }}

Accessing the variable inside the partial:

{{ .myVar }}

But then everything you would normally reference through the . changes to use .context:

{{ .context.Title }}
{{ .context.Scratch.Get "myScratchVar" }}

3 Likes

Super! But how can I transfer my variable in .Render?
For example,
{{ .Render "blog_section.li" (dict "context" . "first_page" "1") }}

and in template how can I get the variable first_page?

We can solve this problem with scratch variables.
refer this: https://harmstyler.me/posts/2019/how-to-pass-variables-to-a-partial-template-in-hugo/