# Select params.title 2 words separately

**URL:** <https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223>\
**Category:** support\
**Created:** [February 17, 2022, 11:23pm UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223 "2022-02-17T23:23:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitifi9191](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pitifi9191](https://discourse.gohugo.io/u/pitifi9191)\
**Post date:** [February 17, 2022, 11:23pm UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/1 "2022-02-17T23:23:41Z")

</div>

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

```auto
# 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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 17, 2022, 11:57pm UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/2 "2022-02-17T23:57:21Z")

</div>

Please spend some time reading about the available functions:

- [https://gohugo.io/functions/first](https://gohugo.io/functions/first)
- [https://gohugo.io/functions/last](https://gohugo.io/functions/last)
- [https://gohugo.io/functions/split](https://gohugo.io/functions/split)

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

```

---

<div class="post-metadata">

**Author:** ![pitifi9191](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pitifi9191](https://discourse.gohugo.io/u/pitifi9191)\
**Post date:** [February 18, 2022, 10:36am UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/3 "2022-02-18T10:36:49Z")

</div>

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”

> [@pitifi9191](#):
>
> ```auto
> # index.md file
> title: Company name
> 
> ```

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

```auto
{{ $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](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/2/9/2988871ba8df81847ce34e41f80fda041f942cb5.png)

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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 18, 2022, 10:40am UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/4 "2022-02-18T10:40:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pitifi9191](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pitifi9191](https://discourse.gohugo.io/u/pitifi9191)\
**Post date:** [February 18, 2022, 10:53am UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/5 "2022-02-18T10:53:53Z")

</div>

@jmooring thanks, this is Mind blowing 🤯

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

```auto
{{ $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

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 18, 2022, 11:51am UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/6 "2022-02-18T11:51:01Z")

</div>

[https://gohugo.io/functions/split](https://gohugo.io/functions/split) → returns a slice  
[https://gohugo.io/functions/first](https://gohugo.io/functions/first) → returns a slice  
[https://gohugo.io/functions/last](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`](https://gohugo.io/functions/index-function/) function to extract an individual element from a slice, or you can use the [`delimit`](https://gohugo.io/functions/delimit) function to form a string from all of the elements.

```auto
{{ $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)

```

---

<div class="post-metadata">

**Author:** ![pitifi9191](https://avatars.discourse-cdn.com/v4/letter/p/3ab097/32.png) [@pitifi9191](https://discourse.gohugo.io/u/pitifi9191)\
**Post date:** [February 18, 2022, 1:36pm UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/7 "2022-02-18T13:36:16Z")

</div>

thanks @jmooring , once again for providing detailed quality example and sharing your knowledge.

I use the code like this now

```auto
{{ $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

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 20, 2022, 1:36pm UTC](https://discourse.gohugo.io/t/select-params-title-2-words-separately/37223/8 "2022-02-20T13:36:22Z")

</div>

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