Inline partial returns nothing: no context is passed

Hi, I do not understand how inline partials and partials returning a value work.
Call me stupid (a fair assessment, imho !) but the documentation doesn’t quite suffice to me. And it’s one of those problems, the worst because they’re silent.
You can use this shortcode as is: [details=“Open for more”]

{{- $types := split (.Get "types") " " -}}
{{- $N := default 2 ($.Get "N") -}}
{{- range $line := (split (strings.TrimSpace .Inner) "\n") }}
{{- $line = trim $line " " -}}
{{- $fields := split $line " " -}}
{{- $P := "" -}}
{{- $H := "" -}}
{{- $Trio := "" -}}
{{ if eq (len $fields) 3 }}
{{- $P = index $fields 0 -}}
{{- $H = print "[" (index $fields 1) "](R)\u00b7" -}}
{{- $Trio = index $fields 2 -}}
{{ else if eq (len $fields) 2 }}
{{- $P = index $fields 0 -}}
{{- $Trio = index $fields 1 -}}    
{{ end }}
{{- range $type := $types }}
<figure data-number="{{ $N }}" class="exo {{ default "normal" (default $.Page.Params.config ($.Get "config")) }}">
<samp>{{- partial "substitution.html" (dict "H" $H "trio" $Trio "type" $type) -}}</samp>
{{- range seq $N }}
<kbd contenteditable role="textbox" spellcheck="false"></kbd>
{{- end }}
</figure>
{{- end }}
{{- end }}
{{- $.Page.Store.Add "nbl" (mul (len $types) $N) -}}
  
{{- define "_partials/substitution.html" -}}
{{ print .trio }}
  
{{- $split := split .trio "" -}}
{{- $A := index $split 0 -}}
{{- $B := index $split 1 -}}
{{- $C := index $split 2 -}}
  
{{- $init := dict
"NVVV" "1→[2](V)→[3](R)"
"NNVR" "1→[2](V)→[3](R)"
"NVRN" "1→[2](R)→3"
"NRNV" "[1](R)→2→[3](R)"
"RNVN" "1→[2](R)→3"
"NNVV" "1→2→3"
"NNNN" "1→2→3"
-}}
  
{{- $body_patterns := dict
"acc"  "<em>1</em>23 <em>1</em>123 <em>1</em>223 <em>1</em>233 <em>1</em>123 <em>1</em>223 <em>1</em>233 <em>1</em>123 <em>1</em>223 <em>1</em>233 <em>1</em>23 <em>1</em>23 <em>1</em>23"
"NNNN" "123 1123 1223 1233 1123 1223 1233 1123 1223 1233 123 123 123"
"NVVV" "[123](V) 1[123](V) [1](R)2[23](V) [1](V)[2](R)3[3](R) 1[123](V) [1](R)2[23](V) [1](V)[2](R)3[3](R) 1[123](V) [1](R)2[23](V) [1](V)[2](R)3[3](V) [123](V) [123](V) [12](V)3"
"NNVR" "1[2](V)[3](R) 11[2](V)[3](R) 12[2](V)[3](R) 1[2](R)3[3](R) 11[2](V)[3](R) 12[2](V)[3](R) 1[2](R)3[3](R) 11[2](V)[3](R) 12[2](V)[3](R) 1[2](R)3[3](R) 1[2](V)[3](R) 1[2](V)[3](R) 1[2](V)3"
"NVRN" "[1](V)[2](R)3 1[1](V)[2](R)3 [1](R)2[2](R)3 [1](V)[2](R)33 1[1](V)[2](R)3 [1](R)2[2](R)3 [1](V)[2](R)33 1[1](V)[2](R)3 [1](R)2[2](R)3 [1](V)[2](R)33 [1](V)[2](R)3 [1](V)[2](R)3 [1](V)[2](V)3"
"NRNV" "[1](R)2[3](R) 1[1](R)2[3](V) [1](R)22[3](V) [1](R)23[3](R) [1](R)12[3](V) [1](R)22[3](V) [1](R)23[3](R) 1[1](R)2[3](V) [1](R)22[3](V) [1](R)23[3](V) [1](R)2[3](V) [1](R)2[3](V) [1](R)23"
"RNVN" "1[12](V)3 [1](R)2[2](V)3 [1](V)[2](R)33 1[12](V)3 [1](R)2[2](V)3 [1](V)[2](R)33 1[12](V)3 [1](R)2[2](V)3 [1](V)[2](R)33 [12](V)3 [12](V)3 [12](V)3"
"NNVV" "[123](V) 11[2](V)[3](V) 1[2](R)2[3](V) [12](V)[3](R)3 11[2](V)[3](V) 1[2](R)2[3](V) [12](V)[3](R)3 11[2](V)[3](V) 1[2](R)2[3](V) [12](V)[3](R)3 [123](V) [123](V) [12](V)3"
-}}
  
{{- $types := dict
"1" (print (index $init "NNNN") " " (index $body_patterns "acc"))
"2" (print .H " " (index $body_patterns "acc"))
"3" (print .H " " (index $init "NNNN") " " (index $body_patterns "acc"))
"4" (print .H " " (index $body_patterns "NNNN"))
"5" (print (index $init .type) " " (index $body_patterns .type)) -}}
  
{{- return ($.Page.RenderString (replace (replace (replace (index $types .type ) "1" $A) "2" $B) "3" $C)) -}}
{{- end }}

[/details]
it’s supposed to called like this:

{{< list types="5" >}}
NVVV mjb
fk NVVV mjb
{{</ list >}}

I checked, the variables $Trio and $type are there prior to the partial call. But not inside, no context is passed. So it returns nothing.

mmh, i took your code into a shortcode and your markdown into my index.html, plugged in some warnf and a counter and got:

your final .RenderString result is nil

looks like you have to check the inline partials logic

WARN  START [ 01 ]
WARN  samp  [ 01 ] H    :
WARN  samp  [ 01 ] Trio : mjb
WARN  samp  [ 01 ] Type : 5
WARN  .sub  [ 01 ] map[H: c:1 trio:mjb type:5]
WARN  .sub  [ 01 ] .trio = mjb
WARN  .sub RESULT [ 01 ] %!s(<nil>)
WARN  END   [ 01 ] -------
WARN  START [ 02 ]
WARN  samp  [ 02 ] H    : [NVVV](R)·
WARN  samp  [ 02 ] Trio : mjb
WARN  samp  [ 02 ] Type : 5
WARN  .sub  [ 02 ] map[H:[NVVV](R)· c:2 trio:mjb type:5]
WARN  .sub  [ 02 ] .trio = mjb
WARN  .sub RESULT [ 02 ] %!s(<nil>)

end of your inline partial

  {{- $result := $.Page.RenderString (replace (replace (replace (index $types .type ) "1" $A) "2" $B) "3" $C) -}}
  {{- warnf ".sub RESULT [ %02d ] %s" .c $result -}}
  {{- return $result -}}
1 Like

I beg to disagree, modify the text like this:

<figure data-number="{{ $N }}" class="exo {{ default "normal" (default $.Page.Params.config ($.Get "config")) }}">
{{ print $Trio }}
<samp>{{- partial "substitution.html" (dict "H" $H "trio" $Trio "type" $type) -}}</samp>
{{- range seq $N }}
  <kbd contenteditable role="textbox" spellcheck="false"></kbd>
{{- end }}
</figure>
{{- end }}
{{- end }}
{{- $.Page.Store.Add "nbl" (mul (len $types) $N) -}}

{{- define "_partials/substitution.html" -}}
{{ print .trio }}

Only the first value appears, immediately from within the partial the context becomes unreachable.

guess the docs beg to agree :wink:

Your (inline) partial uses a return statement which means, “don’t produce some output BUT return a value” even if the value (as in your case) is nil :wink:

Comment or remove the return action and you will see your {{ print .trio }}

you cannot mix both approaches with printing and return. In that case the warnf function will help to debug.

partials.Include

Executes the given template, optionally passing context. If the partial
template contains a return statement, returns the given value, else returns
the rendered output.
from: partials.Include

my list.html

This text will be hidden

{{- $types := split (.Get "types") " " -}}
{{- $N := default 2 ($.Get "N") -}}
{{- range $line := (split (strings.TrimSpace .Inner) "\n") }}
  {{- $line = trim $line " " -}}
  {{- $fields := split $line " " -}}
  {{- $P := "" -}}
  {{- $H := "" -}}
  {{- $Trio := "" -}}
  {{ if eq (len $fields) 3 }}
    {{- $P = index $fields 0 -}}
    {{- $H = print "[" (index $fields 1) "](R)\u00b7" -}}
    {{- $Trio = index $fields 2 -}}
  {{ else if eq (len $fields) 2 }}
    {{- $P = index $fields 0 -}}
    {{- $Trio = index $fields 1 -}}
  {{ end }}
  {{- range $type := $types }}
    <figure
      data-number="{{ $N }}"
      class="exo {{ default "normal" (default $.Page.Params.config ($.Get "config")) }}"
    >
                                                                        {{ print $Trio }}
      <samp>
        {{- $c := math.Counter -}}
        {{- warnf "START [ %02d ]" $c -}}
        {{- warnf "samp  [ %02d ] H    : %v" $c $H -}}
        {{- warnf "samp  [ %02d ] Trio : %v" $c $Trio -}}
        {{- warnf "samp  [ %02d ] Type : %v" $c $type -}}
        {{- partial "substitution.html" (dict "c" $c "H" $H "trio" $Trio "type" $type) -}}
        {{- warnf "END   [ %02d ] -------" $c -}}
      </samp>
      {{- range seq $N }}
        <kbd contenteditable role="textbox" spellcheck="false"></kbd>
      {{- end }}
    </figure>
  {{- end }}
{{- end }}
{{- $.Page.Store.Add "nbl" (mul (len $types) $N) -}}

{{- define "_partials/substitution.html" -}}
  {{ print .trio }}
  {{- warnf ".sub  [ %02d ] %v" .c . -}}
  {{- warnf ".sub  [ %02d ] .trio = %s" .c .trio -}}

  {{- $split := split .trio "" -}}
  {{- $A := index $split 0 -}}
  {{- $B := index $split 1 -}}
  {{- $C := index $split 2 -}}

  {{- $init := dict
    "NVVV" "1→[2](V)→[3](R)"
    "NNVR" "1→[2](V)→[3](R)"
    "NVRN" "1→[2](R)→3"
    "NRNV" "[1](R)→2→[3](R)"
    "RNVN" "1→[2](R)→3"
    "NNVV" "1→2→3"
    "NNNN" "1→2→3"
  -}}

  {{- $body_patterns := dict
    "acc"  "<em>1</em>23 <em>1</em>123 <em>1</em>223 <em>1</em>233 <em>1</em>123 <em>1</em>223 <em>1</em>233 <em>1</em>123 <em>1</em>223 <em>1</em>233 <em>1</em>23 <em>1</em>23 <em>1</em>23"
    "NNNN" "123 1123 1223 1233 1123 1223 1233 1123 1223 1233 123 123 123"
    "NVVV" "[123](V) 1[123](V) [1](R)2[23](V) [1](V)[2](R)3[3](R) 1[123](V) [1](R)2[23](V) [1](V)[2](R)3[3](R) 1[123](V) [1](R)2[23](V) [1](V)[2](R)3[3](V) [123](V) [123](V) [12](V)3"
    "NNVR" "1[2](V)[3](R) 11[2](V)[3](R) 12[2](V)[3](R) 1[2](R)3[3](R) 11[2](V)[3](R) 12[2](V)[3](R) 1[2](R)3[3](R) 11[2](V)[3](R) 12[2](V)[3](R) 1[2](R)3[3](R) 1[2](V)[3](R) 1[2](V)[3](R) 1[2](V)3"
    "NVRN" "[1](V)[2](R)3 1[1](V)[2](R)3 [1](R)2[2](R)3 [1](V)[2](R)33 1[1](V)[2](R)3 [1](R)2[2](R)3 [1](V)[2](R)33 1[1](V)[2](R)3 [1](R)2[2](R)3 [1](V)[2](R)33 [1](V)[2](R)3 [1](V)[2](R)3 [1](V)[2](V)3"
    "NRNV" "[1](R)2[3](R) 1[1](R)2[3](V) [1](R)22[3](V) [1](R)23[3](R) [1](R)12[3](V) [1](R)22[3](V) [1](R)23[3](R) 1[1](R)2[3](V) [1](R)22[3](V) [1](R)23[3](V) [1](R)2[3](V) [1](R)2[3](V) [1](R)23"
    "RNVN" "1[12](V)3 [1](R)2[2](V)3 [1](V)[2](R)33 1[12](V)3 [1](R)2[2](V)3 [1](V)[2](R)33 1[12](V)3 [1](R)2[2](V)3 [1](V)[2](R)33 [12](V)3 [12](V)3 [12](V)3"
    "NNVV" "[123](V) 11[2](V)[3](V) 1[2](R)2[3](V) [12](V)[3](R)3 11[2](V)[3](V) 1[2](R)2[3](V) [12](V)[3](R)3 11[2](V)[3](V) 1[2](R)2[3](V) [12](V)[3](R)3 [123](V) [123](V) [12](V)3"
  -}}

  {{- $types := dict
    "1" (print (index $init "NNNN") " " (index $body_patterns "acc"))
    "2" (print .H " " (index $body_patterns "acc"))
    "3" (print .H " " (index $init "NNNN") " " (index $body_patterns "acc"))
    "4" (print .H " " (index $body_patterns "NNNN"))
    "5" (print (index $init .type) " " (index $body_patterns .type))
  -}}

  {{- $result := $.Page.RenderString (replace (replace (replace (index $types .type ) "1" $A) "2" $B) "3" $C) -}}
  {{- warnf ".sub RESULT [ %02d ] %s" .c $result -}}
  {{- return $result -}}
{{- end }}
2 Likes

Oh damn, I understood. Never had that kind of problem, but a behavior such as this makes sense when you have the option of just printing stuff out by writing it instead of “printf” everything as in a normal compiled language. And I think I got confused, because I don’t need to return a value. Mfff.
I’ll see what I find with debug.

As you said, I did not understand the logic of my code. It’s corrected now. Also, it’s the first time I find a use for markdownify vs .Page.RenderString. Avoid passing .Page in the context.

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