What If I want to add span tag in name?

Continuing the discussion from Adding an icon next to menu item:

what if I want to add something span tag in

{{- $text := print $icon " " .Name | safeHTML -}}
<li><a href="link"><i></i><span>Name</span></a></li>

?

<li><a href="link">{{ $icon }}<span>{{ .Name }}</span></a></li>

Just use the variables directly

Here my full menu template.

{{- define "partials/inline/menu/items.html" }}
  {{- $page := .page }}
  {{- range .menuEntries }}
    {{- $attrs := dict "href" .URL }}
    {{- if $page.IsMenuCurrent .Menu . }}
      {{- $attrs = merge $attrs (dict "class" "active" "aria-current" "page") }}
    {{- else if $page.HasMenuCurrent .Menu . }}
      {{- $attrs = merge $attrs (dict "class" "ancestor text-blue-800" "aria-current" "true") }}
    {{- end }}
    {{- $icon := printf "<i class=\"%s\"></i>" .Pre -}}
    {{- $text := print $icon " " .Name | safeHTML -}}
    <li class="font-medium sm:py-4">
      <a {{- range $k, $v := $attrs }}{{- with $v }}{{- printf " %s=%q" $k $v | safeHTMLAttr }}{{- end }}{{- end -}}>{{ $text }}</a>
      {{- with .Children }}
        <ul>
          {{- partial "inline/menu/items.html" (dict "page" $page "menuEntries" .) }}
        </ul>
      {{- end }}
    </li>
  {{- end }}
{{- end }}

as Menu templates

To

    <li class="font-medium sm:py-4">
      <a {{- range $k, $v := $attrs }}{{- with $v }}{{- printf " %s=%q" $k $v | safeHTMLAttr }}{{- end }}{{- end -}}>
        {{ $icon }}<span>{{ .Name }}</span>
      </a>

Or replace {{- $text := print $icon " " .Name | safeHTML -}} with {{- $text := printf "%s <span>%s</span>" $icon .Name | safeHTML -}}.

1 Like

Thank you so much :heart_eyes: :heart_eyes: :heart_eyes:

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