Which is the correct way to join these variables?

New topic from this question. I would like to split the duplicate tailwind styles as follows:

  1. A var for all the classes appearing in all three instances.
  2. A var for classes only appearing in the active states.
  3. A var for classes only appearing in the default state.

See Example below

{{- var1 := "bg-blue-500 text-red-200" }}
{{- var2 := "hover:bg-blue-400 border-r" }}
{{- var3 := "hover:bg-blue-300 ml-4" }}

Then in the dict part of class, I would like to merge var1 to var2 for both active states and var1 to var3 for the default state so that the styles now appear together. Which is the correct way to do it? cc @jmooring .

...
  {{ $var1 := "class-1" }}
  {{ $var2 := "class-2" }}
  {{ $var3 := "class-3" }}
  {{ $activeState := printf ("%s %s") $var1 $var2 }}
  {{ $defaultState := printf ("%s %s") $var1 $var3 }}

  {{- $page := .page }}
  {{- range .menuEntries }}
    {{- $attrs := dict "class" $defaultState "href" .URL }}
    {{- if $page.IsMenuCurrent .Menu . }}
      {{- $attrs = merge $attrs (dict "class" $activeState "aria-current" "page") }}
    {{- else if $page.HasMenuCurrent .Menu .}}
      {{- $attrs = merge $attrs (dict "class" $activeState "aria-current" "true") }}
    {{- end }}
...
1 Like

Thanks! Forgot about printf. I played with union and append but both presented broken strings.

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