Assigning string concate inside a loop

Am trying to build a markdown table from a dictionary inside a range operator. However the string variable $metadata is not correctly created and accessible outside the loop.


{{ range $key, $value := $books }}
    {{$metadata := printf "| %v | %v | \n" $key $value}}
{{ end }}

Full code listing:

{{ with resources.GetRemote "https://archive.org/advancedsearch.php?q=collection%3AServantsOfKnowledge+(language%3Aori+OR+language%3A%22Oriya%22)&fl%5B%5D=collection&fl%5B%5D=addeddate&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=title&fl%5B%5D=description&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=2&page=1&output=json" }}
  {{ with . | unmarshal }}
      {{with .response.docs}}
      {{range . }}
          {{ $books := dict 
            "identifier" .identifier
            "title" .title
            "content" .description
            "img" .identifier
            "date" .addeddate
          }}
                  {{ range $key, $value := $books }}
                  	{{$metadata := printf "| %v | %v | \n" $key $value}}
                  {{ end }}
                  {{ warnf "%v" $metadata}}
	    {{ end }}
 	  {{ end }}
 {{ end }}
{{ end }}

Take a look at the Hugo Docs:

and note that you need to use := outside the loop and = inside.

Ended up rewriting the code as:


          {{ $metadata := " " }}
          {{ range $key, $value := $books }}
               {{ $metadata = $metadata | printf "| %v | %v | \n" $key $value }}
          {{ end }}
          {{ warnf "%v" $metadata}}

But I am not sure why this adds an extract “%!(EXTRA string=” to appended values?

%!(EXTRA string=| img | bhabe-bhola-vol-3-1956 | 
%!(EXTRA string=| identifier | bhabe-bhola-vol-3-1956 | 
%!(EXTRA string=| date | 2021-06-13T15:41:58Z | 
%!(EXTRA string=| content | "ଭାବେ ଭୋଳା ଭାଗ ୩" (Romanization: /bhāb```

Maybe use “%s” instead of “%v”?

That doesn’t seem make any difference here.

After reading this post I managed to cobble a markdown table as follows. It is not pretty but does the job.

 {{ $tableheader := "|   |   |\n|---|---|"}}
 {{ $metadata := "" }}
          {{ range $key, $value := $books }}
              {{ $metadata = $metadata | printf "%s%s%s" $value "|" | printf "\n|%s|%s" $key | printf "%s"  }}
          {{ end }}
{{ warnf "%s%s" $tableheader $metadata}}

Output


|   |   |
|---|---|
|title|ଭାବେ ଭୋଳା ଭାଗ ୩|
|img|bhabe-bhola-vol-3-1956|
|identifier|bhabe-bhola-vol-3-1956|
|date|2021-06-13T15:41:58Z|
|content|"ଭାବେ ଭୋଳା ଭାଗ ୩" (Romanization: /bhābe bhoḷā bhāga 3/, IPA: /bʱaːbe bʱol̪aː bʱaːɡɔ 3/) ବାସୁଦେବ ମହାପାତ୍ରଙ୍କ ଦ୍ୱାରା ରଚିତ ଏବଂ ଓଡ଼ିଆରେ ଲିଖିତ ଏକ କବିତା ପୁସ୍ତକ । ଏହା ୧୯୫୬ ମସିହାରେ ପ୍ରକାଶିତ ହୋଇଥିଲା । "Bhabe Bhola Volume 3" is an Odia-language poetry book authored by Basudeba Mahapatra and published in 1956.|

I think what you mean to do here is:

$metadata | append (printf "| %v | %v \n" $key $value) or similar.

The way you had it $metadata was being fed to the printf as a parameter after $key and $value, and that resulting third parameter was 'extra` leading to the output.

Check out the Hugo Docs for Pipes:

Which are Go Pipes:

1 Like

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