Display the first word of a variable in Statamic

D

Today, I ran into a use case where I want to grab the first word of the Title form field in a Statamic Collection. In this case, the Title field is a full name. In my HTML, I want to pluralize the first name of that field.

First, I looked through the list of Statamic Modifiers. Unfortunately, none of the modifiers did what I was looking to do. Next, I headed to the Statamic Forums. I didn’t find a solution, so I added a question to the new GitHub Discussions. I posted my question, “Is there a way to display the first word of a field?” and received two responses within a few hours! Statamic has such a great developer community!

Statamic's developer community is enthusiastic, active, and helpful
Statamic’s developer community is enthusiastic, active, and helpful.

Here is the solution I used to display the first word of a field in Statamic

Use PHP’s explode function to grab the first word

According to Educba, “The explode() function of the PHP Programming Language is an inbuilt function that helps split a string into many different strings.” Because Statamic uses the PHP framework Laravel, PHP’s explode to split your string at spaces is available.

INPUT (in the kelly-barkhurst.md file)

title: 'Kelly Barkhurst'

ANTLERS (in the people.html file)

<p>{{ title explode=" " first="true" }}'s Articles</p>

OUTPUT (displays in browser)

Kelly's Articles

Assuming your field’s name is “title,” the script in your HTML will look like this.

  • title == the name of my field, substitute with your field name
  • explode == php function splitting your string
  • ” “ == splits string at spaces
  • first=”true” == means to display the first string

Variations

Alternately, you could display the last split string using last instead of first.

last="true" 

Use Regex

Another contributor suggested using regex_replace to grab and display the entire string before the last word.

While this solution becomes problematic for names where there might be more than a single word in the last name (like ‘de Luca’ or ‘ter Haseborg’) or credentials like Ph.D. appended to the end of a name, this is an effective tool and technique that might have other use cases in your code.

INPUT (in the kelly-barkhurst.md file)

title: 'Kelly Barkhurst'
test: 'Kelly Barkhurst Third Fourth Fifth Notshown'
test2: 'Kelly Barkhurst Third Fourth Fifth M.F.A.'

ANTLERS (in the people.html file)

<p>{{ title regex_replace='\\s+\\w+$|' }}'s Articles</p>
<p>{{ test regex_replace='\\s+\\w+$|' }}</p>
{{ test2 regex_replace='\\s+\\w+$|' }}

According to Jason Varga (Statamic developer), this regex option searches for:

regex_replace='\\s+\\w+$|' 
  • \s a space
  • + one or more (spaces)
  • \w is a letter/number
  • + again, one or more (letters/numbers)
  • $ end of the line

Then it replaces what it found with nothing, effectively chopping off the last word.

OUTPUT (displays in browser)

Kelly's Articles
Kelly Barkhurst Third Fourth Fifth
Kelly Barkhurst Third Fourth Fifth M.F.A.

To note, while this specific regex_replace works for strings of letters/numbers and spaces (alpha and spaces), it doesn’t work when introducing punctuation at the end of the string. You’ll see in the example above that the M.F.A is still displayed. This exact code does not work in the use case of a period or comma as the final character of the final word in the string.

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

Recent Posts

Archives

Categories