Dynamic ordering of HTML output from PHP

I have a basic understanding of PHP, but I feel that what I'm trying to do can be made much simpler. I use PHP to display HTML content for a Wordpress theme. I want users to be able to determine the display order of HTML blocks. I am currently using the if/elseif setting. The following is a simplified example:

 if ( $layout == 'layout_one' ) { echo '<div class="header">Header</div>'; echo '<div class="content">Content</div>'; echo '<div class="footer">Footer</div>'; } elseif ( $layout == 'layout_two' ) { echo '<div class="content">Content</div>'; echo '<div class="header">Header</div>'; echo '<div class="footer">Footer</div>'; } elseif ( $layout == 'layout_three' ) { echo '<div class="footer">footer</div>'; echo '<div class="header">Header</div>'; echo '<div class="content">Content</div>'; } 

Now this method is becoming an absolute bear due to a shift in the number of layout options (permutations). Therefore, in my opinion, you should write a separate function for each block of HTML content, and then somehow have another function that represents them in the order specified by the user. Or perhaps put all the blocks in an array, and then reorder the array based on layout selection ...

Anyway, I hit a wall on how to actually do it. Any guidance would be greatly appreciated. Thanks

+5
source share
4 answers

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.

+6
source

How about this:

 <?php $h = '<div class="header">Header</div>'; $c = '<div class="content">Content</div>'; $f = '<div class="footer">Footer</div>'; if ($layout == 'layout_one') { echo $h.$c.$f; } elseif($layout == 'layout_two') { echo $c.$h.$f; } elseif($layout == 'layout_three') { echo $f.$h.$c; } 
+2
source

Create an identifier for each layout that defines the order of the components, for example. h_c_f means that the header, content, footer and c_f_h are the content, footer, header.

Store the contents of your sections in an array indexed by the corresponding letter:

 $section_stuff = array( 'h' => '<div>Header</div>', 'c' => '<div>Contents</div>', 'f' => '<div>Footer</div>', ); 

Collect your result using the layout ID to complete the order:

 // $order is the layout name, eg c_f_h, f_c_h, etc. // split $order by "_" to get the appropriate order $ordered = explode( '_', $order ); foreach ($ordered as $section) { echo $section_stuff[$section]; } 

Obviously, you will need to add error checking, etc.

The advantage of this method is that you can use something like a drag and drop interface to adjust the order of partitions; the identifier name defines the layout, not an arbitrary value.

If you want to generate all possible layouts in advance, the PHP cookbook contains several suitable suggestions.

+2
source

The switch key can be used instead of long elseif conditions. Because the switch is more effective than elseif and will support a large number of conditions. An exact replacement for your code will look like this for a switch.

 switch ($layout) { case 'layout_one': echo '<div class="header">Header</div>'; echo '<div class="content">Content</div>'; echo '<div class="footer">Footer</div>'; break; case 'layout_two': echo '<div class="content">Content</div>'; echo '<div class="header">Header</div>'; echo '<div class="footer">Footer</div>'; break; case 'layout_three': echo '<div class="footer">footer</div>'; echo '<div class="header">Header</div>'; echo '<div class="content">Content</div>'; break; } 

additionally complex work cane performed by php include function. You can create a template as a php file with the same name that is used in the if statement and use the variable in the include statement. Sample code will look like this.

 include("/path_to/layout/".$layout.".php"); 

An example HTML layout will look like this with the appropriate names.

 layout_one.php <div class="header">Header</div> <div class="content">Content</div> <div class="footer">Footer</div> 
+1
source

Source: https://habr.com/ru/post/1200975/


All Articles