<$.Scratch.Get>: can't evaluate field Scratch in type string

Hello. I use a partial template in a partial. This is my structure.
For example:
index.tpl code:

{{ .Scratch.Set "Fields" "Name#1|Phone#1|Email%%File%%Message" }}
{{ partial "modules/Main" . }}

modules/Main.tpl code

			{{ $FieldsRow := split ($.Scratch.Get "Fields") "%%" }}

			{{ range $key, $Fields := $FieldsRow }}
			<div class="field_form_f">
				{{ $.Scratch.Set "FieldData" $Fields }}
				{{ partial "modules/SwitchInput" . }}
				{{ $.Scratch.Delete "FieldData" }}
			</div>
			{{ end }}

modules/SwitchInput.tpl code is:

{{ $FieldData := .Scratch.Get (string "FieldData") }}
{{ $Field := split ($FieldData) "|" }}
{{ range $key, $FieldValue := $Field }}
	{{ $FieldTemp := split ($FieldValue) "#" }}
	{{ index $FieldTemp 0 }}

{{ if eq (index $FieldTemp 0) "Name" }}
	{{ if (index $FieldTemp 1) }}
		{{ $.Scratch.Set "Filed_50" 1 }}
	{{ end }}
	{{ $.Scratch.Set "TextName" (T "NAME_text") }}
	{{ $.Scratch.Set "Name" "contact_name" }}
	{{ $.Scratch.Set "Type" "text" }}
	{{ $.Scratch.Set "maxLength" 50 }}
	{{ $.Scratch.Set "Require" 1 }}
	{{ partial "modules/InputField" . }}
{{ end }}

{{ end }}

modules/InputField.tpl code is:

{{ $name := $.Scratch.Get "Name" }}
<div class="{{ if ($.Scratch.Get "Filed_50") }} class50{{ end }}">
	<input id="{{ $name }}" type="{{ $.Scratch.Get "Type" }}" maxlength="{{ $.Scratch.Get "maxLength" }}" />
	<label for="{{ $name }}">{{ $.Scratch.Get "TextName" }}{{ if ($.Scratch.Get "Require") }} *{{ end }}</label>
</div>

And My errors are

Rebuild failed:

Failed to render pages: render of “home” failed: execute of template failed: template: index.html:486:3: executing “index.html” at <partial “modules/Main” .>: error calling partial: “…\layouts\partials\modules\Main.html:1:25”: execute of template failed: template: partials/modules/Main.html:27:8: executing “partials/modules/Main.html” at <partial “modules/SwitchInput” .>: error calling partial: “…\layouts\partials\modules\ContactForm\SwitchInput.html:1:25”: execute of template failed: template: partials/modules/SwitchInput.html:1:25: executing “partials/modules/SwitchInput.html” at <.Scratch.Get>: can’t evaluate field Scratch in type string
hugo v0.81.0-59D15C97 windows/amd64 BuildDate=2021-02-19T17:07:12Z VendorInfo=gohugoio

Why .Scratch.Get param is not string?

{{ partial "modules/SwitchInput" . }}

You call this while ranging through $FieldsRow, so the value passed to the partial (the dot) is not the page context.

Okay, I tried that

 {{ $.Scratch.Set "FieldData" $Fields }}
 {{ $tplData := partial "modules/SwitchInput" . }}
 {{ $.Scratch.Delete "FieldData" }}
 {{ $tplData }}

but in this case the variable FieldData doesn’t send to the partial “SwitchInput”

No, you didn’t. You are still passing the wrong context to the partials.

diff --git a/layouts/partials/modules/Main.html b/layouts/partials/modules/Main.html
index 89bc2cf6c..9e191b96a 100644
--- a/layouts/partials/modules/Main.html
+++ b/layouts/partials/modules/Main.html
@@ -1,9 +1,10 @@
 {{ $FieldsRow := split ($.Scratch.Get "Fields") "%%" }}
 
+{{ $currentPage := . }}
 {{ range $key, $Fields := $FieldsRow }}
   <div class="field_form_f">
     {{ $.Scratch.Set "FieldData" $Fields }}
-    {{ partial "modules/SwitchInput" . }}
+    {{ partial "modules/SwitchInput" $currentPage }}
     {{ $.Scratch.Delete "FieldData" }}
   </div>
 {{ end }}
diff --git a/layouts/partials/modules/SwitchInput.html b/layouts/partials/modules/SwitchInput.html
index 6876f83b5..be16c7bdb 100644
--- a/layouts/partials/modules/SwitchInput.html
+++ b/layouts/partials/modules/SwitchInput.html
@@ -1,3 +1,5 @@
+{{ $currentPage := . }}
+
 {{ $FieldData := .Scratch.Get (string "FieldData") }}
 {{ $Field := split ($FieldData) "|" }}
 {{ range $key, $FieldValue := $Field }}
@@ -13,7 +15,7 @@
     {{ $.Scratch.Set "Type" "text" }}
     {{ $.Scratch.Set "maxLength" 50 }}
     {{ $.Scratch.Set "Require" 1 }}
-    {{ partial "modules/InputField" . }}
+    {{ partial "modules/InputField" $currentPage }}
   {{ end }}
 
 {{ end }}

Please read this:
https://www.regisphilibert.com/blog/2018/02/hugo-the-scope-the-context-and-the-dot/

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