Percipio.London: Percipio Dev Tips – Part One

Stuart Byford
Michael Thomas

Blog by Stuart Byford and Michael Thomas
on the

Percipio Dev Tips – Part One

Since the turn of the year, our developers have been sharing different tips and tricks on our social feeds. So now there is a small collection; we thought it was about time to create a blog post so you can implement these into your workflow. 

These dev tips cover the different technologies that we use here daily at Percipio, we hope you enjoy!

Craft CMS


In the following section, you’ll find several valuable snippets of code that you can use in the Twig templating language. We use this language to create templates based on the data that we have inside of Craft CMS.

Our first snippet is a perfect example of the power of macros used in every project. It helps us avoid creating too many duplicate files and gives the opportunity to parse an options’ object through. No more taking care of orders in Twig – paving the road to DRY code!

{% macro atom(type, atom, options, prefix = '') %}
    {% spaceless %}
        {% include '_atoms/' ~ type ~ 's/' ~ prefix ~ type ~ '--' ~ atom with {
            options: options,
        } %}
    {% endspaceless %}
{% endmacro %}
{{- render.atom('text', 'content', { content: content.article }) -}}

We recently started using Look Up Tables ( LUT ) to keep our projects much cleaner and intuitive. This helps developers be more productive and easily pinpoint/​add missing values inside a single LUT, rather than create complicated if/​else’ statement switches.

Below you will see how we use the code with the Font Awesome Library to show icons dynamically throughout our templates.

{% macro fa_icon_lut(type) %}
	{%- set lut = {
		'download': 'fas fa-download',
		'envelope': 'fas fa-envelope',
		'facebook': 'fab fa-facebook-f',
		'print': 'fas fa-print',
		'phone': 'fas fa-phone',
		'twitter': 'fab fa-twitter',
		'default': 'fas fa-hashtag',
	} -%}
	{% spaceless %}
		{{- lut[type] ??? lut['default'] -}}
	{% endspaceless %}
{% endmacro %}

If you find you keep on writing long checks to see if a variable has been defined and contains a value, then you can use the null coalescing operator in Twig. This selects the first piece of code inline that is defined and is not null. If you wish to speed things up, this is an operator you will always use and makes your code so much easier to understand!

{# long version #}
{% if entry is defined and entry.description is defined %}
    {% set description = entry.description %}
{% elseif category is defined and category.description is defined %}
    {% set description = category.description %}
{% else %}
    {% set description = global.description %}
{% endif %}
{# shorthand #}
{% set description = entry.description ?? category.description ?? global.description %}

For performance reasons, in Craft CMS we will try to Eager Load as many fields as possible as this avoids multiple n+1 queries. Since we Eager Load, everything ends up in an array rather than an element, so we can’t run certain methods anymore.

Sometimes we might need to get creative in making sure the array only gives us the blocks we need. We do not load these blocks initially as in some cases you need all blocks, and elsewhere you will need to pass on specific blocks.

This is where the Twig filter filter’ becomes very useful, as we can use it to only get the block types that we need in a new array. As a result, we get rid of all the overhead!

{% do
) %}
{% set articleBlocks = entry.matrixField | filter(block => block.type == 'article') %}


Here at Percipio we deal with distance calculations and geometrics daily. Since MySQL 5.7.6 it is now possible to use simple, spatial convenience functions. For example, it’s now easy to calculate physical distances between coordinates.

  ST_Distance_Sphere(point_column, POINT(51.52, -0.08)) AS distance
  distance ASC


In PHP 8, we can finally use null safe operators in our statements, by typing ?->’ It looks to the left of the operator. For example, if ‘$foo’ is null, then our data/​getObject’ function will not be executed. As a result, our code won’t throw an error if executing a function on something that is null.

// Using the Nullsafe operator in PHP 8
// example 1
// example 2
$this = $data?->getObject()->item();

With the following simple function, you can count the number of times a certain value appears in a multi-dimensional array. This ensures we don’t run into an index not defined’ error if the value isn’t anywhere in the array. Instead, the function would tell us there are none by parsing 0.

function count_value($array, $col, $value)
    $total = 0;
    $array = array_column($array, $col);
    if (in_array($value, $array)) {
          $total = array_count_values($array)[$value];
    return $total;
$total = count_value($array, 'country', 'UK');

Craft CMS is an amazing tool, especially with the support for GraphQL. You can easily define your own GraphQL Type for a custom field, this gives you freedom in how you want to structure the data that schema returns.

$fieldType = GqlEntityRegistry::createEntity($typeName, new ObjectType(
        'name' => 'gqlFieldHandle',
        'fields' => [
            'customField' => [
                'name' => 'customField',
                'type' => Type::string(),
                'description' => 'The description of your custom field.'

We will continue to create dev tips and share them over our social feeds, so follow us on Twitter, Instagram& LinkedIn for regular updates!

Want to read more?

Check out our blogs

Blog by Jamie Taylor on

Development of the EEF’s Teaching and Learning Toolkit

Eef toolkit landing
ReadDevelopment of the EEF’s Teaching and Learning Toolkit

Blog by Carly Ayre on

Birmingham Design Festival 2021

Grid post 02
ReadBirmingham Design Festival 2021

Blog by Stuart Byford on

Four Peaks Challenge in aid of Great Ormond Street

PXL 20210709 164503033 MP
ReadFour Peaks Challenge in aid of Great Ormond Street
made withby Percipio


Building applications for better futures.


Unit 122, 372 Old Street

London EC1V 9LT

Telephone: 0208 1444 048

© 2021. Percipio is a Trademark of Percipio Global Ltd