Is there a more efficient way to loop through pages?

Alternative to if... else... you can use the control structure switch functionality. But if you use if... elseif... or switch is mainly a matter of preference. The performance is the same.

if else

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}

Switch Case acts similar to Elseif control structure. The main difference is that Switch Case can be written in a way that it compares a given value with a set of predefined values without evaluating a set of conditions.

Opinion

In my opinion, `switch is more readable and this is also important for maintenance. If you need this check for a template more often you should think about a function, so that you don’t write recurring code.