percipio.london: 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!
Twig
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!
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.
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!
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!
SQL
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.
PHP
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.
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′.
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.