Andrew Welch · Insights · #twig #string #craftcms

Published , updated · 5 min read ·


Please consider 🎗 sponsoring me 🎗 to keep writing articles like this.

Advanced String Manipulation in Twig with Craft CMS

Learn how to unleash hid­den string manip­u­la­tion pow­ers in Twig with Craft CMS 5, no plu­g­ins required!

String manipulation twig craft cms

While Twig has some use­ful func­tions & fil­ters for string manip­u­la­tion and inter­po­la­tion, some­times you need to do more advanced string manip­u­la­tion than what it offers out of the box.

You might turn to a plu­g­in like Typogri­fy or Word­smith to help you out with your string manip­u­la­tion needs. And while both plu­g­ins are quite use­ful, and do far more than just string manip­u­la­tion, you may find out that you don’t need them at all!

This arti­cle will show you how to tap into pow­er­ful string manip­u­la­tion that is already baked into Twig when using Craft CMS.

No plu­g­ins required!

Link create() some Stringy

Since Craft CMS 3, a PHP library called Stringy has been bun­dled with Craft and is used inter­nal­ly for many string manip­u­la­tion functions.

Stringy offers hun­dreds of help­ful string manip­u­la­tion func­tions that are sit­ting there just wait­ing for you to use them. The ques­tion is how do you tap into them from Twig?

The answer is the create() func­tion, which has been avail­able in Twig since Craft CMS 3.1.6:

{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}

The code above cre­ates a new Stringy PHP object from the Twig vari­able someString. The first para­me­ter to create() is the class to cre­ate, and the sec­ond is an array of para­me­ters to pass in when cre­at­ing it.

In our case, we just pass in the someString vari­able, and the Stringy class is returned to us in the someStringy variable.

This opens up hun­dreds of Stringy func­tions we can now per­form on our string!

N.B.: The {# @var someStringy \Stringy\Stringy #} Twig com­ment is a type-hint that will give you auto­com­plete if you’re using Php­Storm:

Screenshot 2024 05 01 at 13 37 02

Auto­com­plete of the Stringy meth­ods in PhpStorm

Link A Taste of Stringy

Let’s have a look at a few use­ful exam­ples of what you can do with your new­found pow­ers. This is just a small taste of what you can do, as there are hun­dreds of Stringy func­tions avail­able to you.

slugi­fy()

{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.slugify() }}

Out­puts:

this-is-my-string-there-are-many-like-it-but-this-one-is-mine

titleize­ForHu­mans()

{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.titleizeForHumans() }}

Out­puts:

This Is My String. There Are Many Like It, but This One Is Mine.

longest­Com­mon­Sub­string()

{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.longestCommonSubstring('but this one is not mine') }}

Out­puts:

but this one is

safeTrun­cate()

{% set someString = 'This is my string. There are many like it, but this one is mine.' %}
{# @var someStringy \Stringy\Stringy #}
{% set someStringy = create('Stringy\\Stringy', [someString]) %}
{{ someStringy.safeTruncate(25, '…') }}

Out­puts:

This is my string. There…

Link String it all together

The exam­ples pre­sent­ed here are just the tip of the ice­berg, explore all of the func­tions avail­able in Stringy to see what else you can do… all with­out a plugin.

You may be won­der­ing whether the afore­men­tioned plu­g­ins Typogri­fy and Word­smith some­how do these things bet­ter? Nope, turns out they do it in the same way, using Stringy themselves!

Hap­py stringing!