apologies in advance I tried the calculate methods in Hugo like truncate to 7 words but that only work if all page titles are the same length. is Hugo aware of words in a title or paragraph if yes how can I use it to fetch only the first or last word
I try the first and last in docs but what’s happening is I end up getting the first letter and last letter like this [C] [e] I want to grab the first word like company instead of a letter “C”
this is how I added it again using your example, I am sure there is some mistake here but cannot figure out what it is
When you render a slice, you will see the elements in brackets, separated by spaces.
You can use the index function to extract an individual element from a slice, or you can use the delimit function to form a string from all of the elements.
{{ $s := "a b c" }}
{{ $s }} --> a b c (string)
{{ $w := split $s " " }}
{{ $w }} --> [a b c] (slice)
{{ first 1 $w }} --> [a] (slice)
{{ last 1 $w }} --> [c] (slice)
{{ index (first 1 $w) 0 }} --> a (string)
{{ index (last 1 $w) 0 }} --> c (string)
{{ index $w 0 }} --> a (string)
{{ index $w (sub (len $w) 1) }} --> c (string)
{{ delimit $w " " }} --> a b c (string)
thanks @jmooring , once again for providing detailed quality example and sharing your knowledge.
I use the code like this now
{{ $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>