This is not a trivial matter; as the number of layout details grows, it becomes increasingly difficult to maintain. However, I will try to provide a βsimpleβ solution for this.
Suppose you have 3 parts, as in your example. However, this should work for N parts. You install them in an array:
$parts = [ 0 => '<div class="header">Header</div>', 1 => '<div class="content">Content</div>', 2 => '<div class="footer">Footer</div>' ]
Then you want the combinations to be deterministic. This is what I came up with, although I'm sure there is some algorithm to execute all possible combinations (one example algorithm), so this step is automatic:
$layout_combinations = [ 0 => [0, 1, 2], 1 => [0, 2, 1], 2 => [1, 0, 2], 3 => [1, 2, 0], 4 => [2, 0, 1], 5 => [2, 1, 0] ];
Do you really have $layout == 'layout_one' ? We will need to convert it:
$layout_number = [ 'layout_one' => 0, 'layout_two' => 1, 'layout_three' => 2, 'layout_four' => 3, 'layout_five' => 4, 'layout_six' => 5 ];
To use it, after defining the parts above, simply do:
$layout = 'layout_four'; if (!array_key($layout, $layout_number)) throw new Exception('Invalid layout'); $layout_number = $layout_number[$layout]; $layout_structure = $layout_combinations[$layout_number]; foreach ($layout_structure as $part_number) { echo $parts[$part_number]; }
The main advantage is that it expands very easily. If you want to place another part, just add it to the $parts array, add the corresponding new $layout_combinations to the conversion of the number "english =>".
Note: the step can be prevented if $layout = 4 instead of $layout = 'layout_four' . This is very preferable because it allows you to do this automatically by simply adding an element to the end of your $parts array.