Select params.title 2 words separately

Hi there, a really weird requirement has come-up for me. using Hugo can I grab the 2 word {{.title}} in 2 parts first and last word seperatly

# index.md file
title: Company name

something like this in layout

<h1>{{.title grab first word in page .title }} <span class="text-indigo-500">{{.title grab last word in page .title}}</span></h1>

to receive html output like this

<h1>Company <span class="text-indigo-500">name</span></h1>

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

thanks for taking the time

1 Like

Please spend some time reading about the available functions:

{{ $titleFirst := split .Title " " | first 1 }}
{{ $titleLast := split .Title " " | last 1 }}
1 Like

Thanks @jmooring,

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

{{ $titleFirst := split .Title "" | first 1 }}
{{ $titleLast := split .Title "" | last 1 }}
{{$titleFirst}}<span class="text-indigo-500">
{{$titleLast}}</span>

which results in title like this, don’t know why it is in brackets.
Screenshot 2022-02-18 at 12.32.11 PM

I want to grab a “word” as words can be different number of letters can I define in Hugo to take first word without a defining number of letters?

thanks

Look carefully at my example, and compare it to what you wrote. There’s a difference.

1 Like

@jmooring thanks, this is Mind blowing :exploding_head:

space between " " define if to grab a single letter or a word “it should go in docs for beginners”.

{{ $titleFirst := split .Title "" | first 1 }} 
# this result in C
{{ $titleFirst := split .Title " " | first 1 }} 
# this result in Company

The words are automatically place in [ ] for some reason, what addition can remove the brackets?

thanks

https://gohugo.io/functions/split → returns a slice
https://gohugo.io/functions/first → returns a slice
https://gohugo.io/functions/last → returns a slice

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)
1 Like

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>

thanks

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