Passing Variables to a Child Template Loop

Hello. I connect the template like this and pass the variables into it.

    {{ .Scratch.Set "Count" 4 }}
    {{ $faqQ1 := T "Q1" | safeHTML }}
    {{ $faqA1 := delimit (slice (T "A1_1" | safeHTML) (T "A1_2" | safeHTML)) "," }}
    {{ $faqQ1 := T "Q2" | safeHTML }}
    {{ $faqA1 := delimit (slice (T "A2_1" | safeHTML) (T "A2_2" | safeHTML)) "," }}
    {{ .Scratch.Set "faqQ1" $faqQ1 }}
    {{ .Scratch.Set "faqA1" $faqA1 }}
    {{ .Scratch.Set "faqQ2" $faqQ2 }}
    {{ .Scratch.Set "faqA2" $faqA2 }}
    {{ .Scratch.Set "FAQ_SCHEME" "a1" }}
    {{ partial "modules/faq" . }}

In my template, I have a loop, inside which I want to accept variable data.

{{ range $i, $sequence :=  (seq (.Scratch.Get "Count")) }}
{{ $faqTitle = delimit (slice "faqQ" $sequence) "" }}
<div id="faq_{{ $sequence }}" class="one_faq">
    <div class="one_faq_title js_open_block" data-openbl="faq_{{ $sequence }}">
        <div class="ico_faq"></div>
        <div class="title_24">{{ .Scratch.Get $faqTitle  }}</div>
    </div>
</div>
{{ end }}

But in {{.Scratch.Get $ faqTitle}} nothing is passed. Also, nothing is passed to {{.Scratch.Get “faqQ1”}} if the variable is inside a loop.

What is the solution?

diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index deadad6..893ca37 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -6,8 +6,8 @@ <h1>{{ .Title }}</h1>
 {{ .Scratch.Set "Count" 4 }}
 {{ $faqQ1 := T "Q1" | safeHTML }}
 {{ $faqA1 := delimit (slice (T "A1_1" | safeHTML) (T "A1_2" | safeHTML)) "," }}
-{{ $faqQ1 := T "Q2" | safeHTML }}
-{{ $faqA1 := delimit (slice (T "A2_1" | safeHTML) (T "A2_2" | safeHTML)) "," }}
+{{ $faqQ2 := T "Q2" | safeHTML }}
+{{ $faqA2 := delimit (slice (T "A2_1" | safeHTML) (T "A2_2" | safeHTML)) "," }}
 {{ .Scratch.Set "faqQ1" $faqQ1 }}
 {{ .Scratch.Set "faqA1" $faqA1 }}
 {{ .Scratch.Set "faqQ2" $faqQ2 }}
diff --git a/layouts/partials/modules/faq.html b/layouts/partials/modules/faq.html
index 95ea9cf..eb1f6cc 100644
--- a/layouts/partials/modules/faq.html
+++ b/layouts/partials/modules/faq.html
@@ -1,9 +1,9 @@
 {{ range $i, $sequence :=  (seq (.Scratch.Get "Count")) }}
-{{ $faqTitle = delimit (slice "faqQ" $sequence) "" }}
+{{ $faqTitle := delimit (slice "faqQ" $sequence) "" }}
 <div id="faq_{{ $sequence }}" class="one_faq">
     <div class="one_faq_title js_open_block" data-openbl="faq_{{ $sequence }}">
         <div class="ico_faq"></div>
-        <div class="title_24">{{ .Scratch.Get $faqTitle  }}</div>
+        <div class="title_24">{{ $.Scratch.Get (string $faqTitle)  }}</div>
     </div>
 </div>
 {{ end }}
1 Like

Thanks!

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