How to add multiple html elements to a variable and return it?

{{ $slice := .xxx }}
{{ $divs := newScratch }}

{{ range $slice }}
    {{ $id := printf "tag-check-%s" . }}
    {{ $input := partial "functions/area_element_style/base/input" (dict "type" "checkbox" "class" "btn-check" "name" "tag_name" "value" . "id" $id) }}
    {{ $label := partial "functions/area_element_style/base/label" (dict "innerHTML" . "class" "btn" "for" $id) }}

    {{ $div_inner := printf "%s%s" $input $label }}

    {{ $div := partial "functions/area_element_style/base/div" (dict "innerHTML" $div_inner "class" "btn-group btn-group-sm") }}

    {{ $divs.Add "html" $div | safeHTML }}
{{ end }}

{{ return $divs.Get "html" }}

{{ $slice := .xxx }}
{{/*{{ $divs := newScratch }}*/}}
{{ $divs := "" }}

{{ range $slice }}
    {{ $id := printf "tag-check-%s" . }}
    {{ $input := partial "functions/area_element_style/base/input" (dict "type" "checkbox" "class" "btn-check" "name" "tag_name" "value" . "id" $id) }}
    {{ $label := partial "functions/area_element_style/base/label" (dict "innerHTML" . "class" "btn" "for" $id) }}

    {{ $div_inner := printf "%s%s" $input $label }}

    {{ $div := partial "functions/area_element_style/base/div" (dict "innerHTML" $div_inner "class" "btn-group btn-group-sm") }}

{{/*    {{ $divs.Add "html" $div | safeHTML }}*/}}
    {{ $divs = $div }}
{{ end }}

{{/*{{ return $divs.Get "html" }}*/}}
{{ return $divs }}

The first code returns the desired code, but some special symbols are escaped, such as angle brackets <>,

The way the second piece of code is written, only the last content of the range loop is returned. The good thing is that no special characters are escaped.

How can I safely output html characters through return?

{{ $divs := newScratch }}
{{ range slice "<div>foo</div>" "<div>bar</div>" }}
  {{ $divs.Add "html" . }}
{{ end }}
{{ $divs.Values.html | safeHTML }}

The last line can also be:

{{ $divs.Get "html" | safeHTML }}

Thanks to @jmooring for his reply, which made me confirm that I need to use newScratch to splice elements. This is my first time using this.

The problem has been solved. The specific code is as follows. When it comes to return, the returned element needs to be wrapped in parentheses when using safeHTML and other similar functions.


{{ $slice := .xxx }}
{{ $divs := newScratch }}

{{ range $slice }}
    {{ $id := printf "tag-check-%s" . }}
    {{ $input := partial "functions/area_element_style/base/input" (dict "type" "checkbox" "class" "btn-check" "name" "tag_name" "value" . "id" $id) }}
    {{ $label := partial "functions/area_element_style/base/label" (dict "innerHTML" . "class" "btn" "for" $id) }}

    {{ $div_inner := printf "%s%s" $input $label }}

    {{ $div := partial "functions/area_element_style/base/div" (dict "innerHTML" $div_inner "class" "btn-group btn-group-sm") }}

    {{ $divs.Add "html" $div }}
{{ end }}

{{ return ($divs.Get "html" | safeHTML) }}

There are many ways to concatenate strings; using a scratch pad would not be my first choice.

{{ $s := slice "a" "b" "c" }}

{{ $c := delimit $s "" }}
{{ $c }} --> "abc"

{{ $c := "" }}
{{ range $s }}
  {{ $c = add $c . }}
{{ end }}
{{ $c }} --> "abc"

{{ $c := "" }}
{{ range $s }}
  {{ $c = print $c . }}
{{ end }}
{{ $c }} --> "abc"

{{ $c := "" }}
{{ range $s }}
  {{ $c = printf "%s%s" $c . }}
{{ end }}
{{ $c }} --> "abc"

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