Go back

Squeeze - a Statamic Addon

It actually is quite simple. I mean like really simple. It's a string replace, that's it. But I still like using the modifier as it kind of fits into my workflow. And also this was the first addon/composer package I ever developed. It was also a great way to learn how GitHub works with packagist, how you create releases on GitHub, what information should be in you composer.json file and much more stuff like this. If you want to check it out, you can find it here: https://github.com/doefom/squeeze

Now, the name comes from "squeezing" out given characters from a string. When using the modifier all you do is define the characters you want to get rid of and you're done. By default the modifier will remove:

  • _

  • -

  • /

  • :

  • whitespace

Here's what it looks like when using it. If you have a text variable in Statamic and use the default flat file system for storing your data you will end up with an entry and a corresponding .yaml file that contains the data. Let's say we have a variable text with the value "A_string-to/test:the squeeze", which is totally common in everyday programming life, you could get rid of the special characters by using the modifier. The .yaml file content will look something like this:

1text: "A_string-to/test:the squeeze"

To use it in you antlers templates you can then go ahead and squeeze out the characters like this:

1{{ text | squeeze }}

And the above example would give you "Astringtotestthesqueeze". Simple, right? Will you ever need this? Probably not. But if you do, you now know there's something out there that handles that for you.

Let's take a look at the code

As I mentioned before it's nothing more than a string replace.

1<?php
2 
3namespace Doefom\Squeeze\Modifiers;
4 
5use Statamic\Modifiers\Modifier;
6use Statamic\Support\Arr;
7use Statamic\Support\Str;
8 
9class Squeeze extends Modifier
10{
11 
12 const DEFAULT_NEEDLES = '_-/ :';
13 
14 /**
15 * Remove a given set of characters from a string.
16 *
17 * @param string $value The string to be modified
18 * @param array|null $params Any parameters used in the modifier
19 * @param array|null $context Contextual values
20 * @return string
21 */
22 public function index(string $value, ?array $params, ?array $context): string
23 {
24 // The strings to remove from the original string.
25 $squeezables = str_split(Arr::get($params, 0, self::DEFAULT_NEEDLES));
26 
27 // Go through all needles and replace them with an empty string.
28 $str = $value;
29 foreach ($squeezables as $squeezable) {
30 $str = str_replace($squeezable, '', $str);
31 }
32 
33 return $str;
34 }
35}

That's what a modifier class in Statamic looks like. In your antlers templates if you have the above example of {{ text | squeeze }}, then $value will be "A_string-to/test:the squeeze" and $params will hold the characters you want to squeeze, if you specified any. To provide additional "squeezables" you can use the modifier like this:

1{{ text | squeeze:":-(" }}

This will remove any sad faces from any string. However, this will also work for any of those characters separately, they do not need to appear combined in this order.

Conclusion

Easy addon, great way to get started with developing addons in general. For me it was really beneficial to get into the whole GitHub-packagist-versioning thing. Hadn't done this until then and it kind of paved the way for me because future packages didn't seem like such a big deal from now on. So, starting out with the simplest package you can imagine, personally I would recommend that.