HTML Logo

The iframe srcdoc Attribute

6th December 2020 - 7 minutes read time

I was working on a web page generation program recently and was looking for a way to present different versions of the same page with slight differences in the markup and styles. Although using the iframe element came to mind I wasn't keen on rendering out lots of different versions of the page and then referencing them individually in each iframe. I did think about creating using an API to send back each page on request but I thought that this might just overcomplicate the program.

After a little bit of research I came across the srcdoc attribute of the iframe, which solved the problem quite neatly.

The iframe is normally used with a src attribute, which points to some external page. The following example will render the page at example.com within the page.

CSS Logo

Show The First N Items In A List With CSS

1st December 2020 - 4 minutes read time

A common design method is to use list elements to create the layout of a menu or a section on a page. This is all fine until the users come along and create lots of list items that mess up the layout of the page. In CSS it is possible to show only the first few items in the list so that your users can't mess up the layout.

To show only the first 2 items in a list use the adjacent sibling combinator to hide the third element and everything after that.


li + li + li {display: none;}

You can add as many li items to this list as you need to to ensure that the layout of your page works with the correct number of elements.

You can also do the same thing in CSS3 using a default display:none on all list items and then just showing the first 2 items in the list using the :nth-child pseudo-class.

Linux Logo

Finding My Most Commonly Used Commands On Linux

28th November 2020 - 12 minutes read time

I'm a proponent of automation, so when I find myself running the same commands over and over I always look for a way of wrapping that in an alias or script.

I spend a lot of my day to day job in the command line and I realised today that I must have typed 'git status' for the millionth time and wondered what my most commonly used commands were. So I found a stack overflow post showing my most used commands in a nice little bash one liner.


history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl |  head -n10

This command extracts information from the bash history and shows me the root command I have run. This showed me the following on my current system.

PHP Logo

Adding Arguments And Options To Deployer Tasks

15th November 2020 - 17 minutes read time

I have been adding to my custom Deployer scripts for a number of months and I have now been using it to do more than just deploy my sites. Since it acts as a connection to my website server I have been using Deployer to perform other tasks like creating backups and clearing Drupal caches without having to log into the server to do it. What has helped me here is that I have set out my deployment tasks in a very modular fashion, so although my deployment runs a database backup, I there is nothing to stop me running the database backup command on it's own without doing a full deployment.

PHP Logo

Creating A Game With PHP Part 3: Snake

10th November 2020 - 14 minutes read time

So far in this series of posts we have looked at detecting key presses from the user and creating a game of tic tac toe using PHP and the command line. The next step is to create a game that has graphics and real time user input. As we are using the command line we don't have much space to work with so the graphics we create aren't going to be very detailed. The simplest action game I could think of is snake. It has a few simple rules, can have very basic graphics and doesn't involve any physics or other mechanics that would effect the game as a whole. In fact the game snake dates back to the 1976 game Blockade, which was created using just text strings.

PHP Logo

Creating A Game With PHP Part 2: Tic Tac Toe

9th November 2020 - 14 minutes read time

Following on from my last post about creating a command line game in PHP we now have a mechanism to listen to keypresses. The next step from here is to create a simple game. After thinking about a game that would fit into the command line I decided that something simple like tic tac toe (also called noughts and crosses) would be a good starting point. The game board is small and the conditions for winning are pretty simple to understand.

PHP Logo

Creating A Game With PHP Part 1: Detecting Key Input

8th November 2020 - 7 minutes read time

I was watching a documentary about old computers on YouTube recently and it showed a video of an early computer game creating using the command line. This wasn't a text based adventure game, but a game creating using text for the graphics running as a program on the command line. This got me thinking that creating something like this should be possible using PHP. If it was possible on a 30 year old computer then surely it's possible to get PHP to do it, right? I thought it might be interesting to create a series of posts showing how to put this together.

Drupal Logo

Drupal 8: Creating A Subquery In Views

7th November 2020 - 7 minutes read time

You've probably been there before. A Drupal View you are working on looks great and has all the data and fields you need, but when you look a little closer at the results you realise that something is off. After looking at the generated SQL query you see that there is a problem with one of the joins which causes your counts to be off a little. Ultimately, you need to remove this join, but you really need that data included in your results.

Creating a subquery can allow you to pull out data from a specific field without adding another join that would cause the results to be out.

Drupal Logo

Drupal 9: Creating Custom Twig Functions And Filters

1st November 2020 - 7 minutes read time

Whilst Twig is a powerful tool in its own right there are occasions when you need to pull out data from Drupal or manipulate it in certain ways. I normally do this using preprocess steps, but I recently found that it was also possible to extend Twig within the Drupal framework to provide your own functions and filters. This can be useful if you have custom templates and need to perform special actions on data items to format them in different ways.

Setting Up

Before we can create Twig filters and functions in Drupal we need to tell Drupal that we have a class that contains them. This is done by creating an entry in the your module services file that contains the tag tag.extension. In a module called "custom_twig" the file would be called custom_twig.services.yml and would contain the following.

PHP Logo

New Features And Breaking Changes PHP8

29th October 2020 - 22 minutes read time

PHP8 will be released on November 26th 2020 and there is lots being written about the upcoming features in this release has now been released! As this is a major version there will be new features as well as breaking changes so it's important to be aware of what is changing. This will make it easier to think about how PHP8 will effect your applications and what actions you will need to take to ensure you can upgrade without incident.

I thought I would go through a few of the main changes to see whats going to be in the next PHP release.

Running PHP8

A good first step is to look at how to install PHP8 so that you can check it out for yourself. If you are using Ubuntu then the simplest way to install PHP8 is to use the existing ondrej/php PPA library. This can be installed using the following commands.