How to use word count to dynamically select words?

Continuing the discussion from Select params.title 2 words separately:

Hello,

In the past while learning @jmooring’s answer help solved my issue. I am wondering since using first and last is great for 2 words it fails when there is only 1 word or more than 2 words. according to Hugo I need to define tag to use split.

how can I make the tag to be dynamic in previous example the words were “Company name” 2 words

# previous example that worked
{{ $titleFirst := split .Title " " | first 1 }} 
{{ $titleLast := split .Title " " | last 1 }}
{{ index $titleFirst 0 }}<span class="text-indigo-500">
{{ index $titleLast 0 }}</span>

// and I get the desired html
<h1>"Company "<span class="text-indigo-500">name</span></h1>

now I want to extended this to be able to use “company” 1 word and “Company legal name” 3 words to get

# for single word take the last letter
<h1>"Compan"<span class="text-indigo-500">y</span></h1>
# for more than 2 words split everything before last word
<h1>"Company Legal "<span class="text-indigo-500">name</span></h1>

can one combine word count and then calculate based on the number of words, can the value of a tag be dynamic?

thanks for taking the time

I would use because not knowing better :wink:

{{- $str := "lastthingggg" -}}

{{- $parts := split $str " " -}}
{{- $count := len $parts -}}
{{- $last := "" -}}
{{- $first := "" -}}

{{- if gt $count 1 -}}
	{{- $last = index ($parts | last 1) 0 -}}
	{{- $first = strings.TrimSuffix (print " " $last) $str  -}}
{{- else -}}
	{{- $last = substr $str -1 -}}
	{{- $first = strings.TrimSuffix $last $str  -}}
{{- end -}}

LAST: {{$last}}<br>
FIRST: {{$first}}<br>

EDIT:
Change (print " " $last) to just $last if you want to keep the trailing space.
EDIT 2:
If that’s the case then you can move the line {{- $first = strings.TrimSuffix $last $str -}} below the if ... end condition and can delete the other identical line inside.

1 Like

Thank you :pray: @GhsvsDe

it took me little longer to understand. so this is the final code I used using your method

{{- $str := .Title -}}

{{- $parts := split $str " " -}}
{{- $count := len $parts -}}
{{- $last := "" -}}
{{- $first := "" -}}

{{- if gt $count 1 -}}
{{- $last = index ($parts | last 1) 0 -}}
{{- $first = strings.TrimSuffix $last $str -}}
{{- else -}}
{{- $last = substr $str -1 -}}
{{- $first = strings.TrimSuffix $last $str -}}
{{- end -}}
{{$first}}
<span class="text-indigo-500">{{$last}}</span>

# Result

1 word
<h1>Compan<span class="text-indigo-500">y<span></h1>
2 words
<h1>Company<span class="text-indigo-500">Name<span></h1>
3 words
<h1>Company Legal<span class="text-indigo-500">Name<span></h1>

I’m still stumped by the last portion, so I went with the present code. Do you mind if I ask if you learned about these functions from the Hugo documentation or another Golang source?

Thank you for setting such a good example code for me to follow.

I’m coding for decades with several languages ((Hu)Go is new for me). Makes it easier to find the places in the docs I’m searching for to cook my spaghettis. The rest is “time will tell” (= optimizing).

I always start in my favourite search engine startpage.com with deactivated JS because it’s not too overloaded with hits for the “Hugo Fashions company” and “Did you mean?”.

When learning something new most of the time I’m reading, noting, combining (diverse sources), varying and testing this or that that I’ve found then; before i come back to the main issue.

Short: Old-fashioned, sometimes time-consuming, learning by doing (and cusrsing).

1 Like

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