patternphpMinor
Creating an array that loops out on the frontend - Don't Repeat Yourself?
Viewed 0 times
therepeatyourselfcreatingarrayloopsthatdonoutfrontend
Problem
Entire Code Here
Don't repeat yourself
I know of the "Don't repeat yourself". Still it's getting messy in some places. The code below contains three blocks of code.
They are similar in some ways and different in others. It doesn't follow the same pattern in all ways.
What it does
It creates an array that I loop out on the frontend.
Question
Is there any better way to do this kind of stuff? Less messy, better structure?
```
$message_start = 'h2 tags - ';
$message_end = '' . $this->count_h2 . '';
if( $this->count_h2 == 0 ) {
$message = 'No tags found. Add some!';
$array['content_editor']['count_h2']['status'] = 2;
} elseif( $this->count_h2 == 1 ) {
$message = 'Some found. Too few!';
$array['content_editor']['count_h2']['status'] = 1;
} else {
$message = 'Many found. Great!';
$array['content_editor']['count_h2']['status'] = 0;
}
$array['content_editor']['count_h2']['message'] = $message_start . $message . $message_end;
$array['content_editor']['count_h2']['count'] = $this->count_h2;
$message_start = 'h3-h6 tags - ';
$h2_h6 = $this->count_h3 + $this->count_h4 + $this->count_h5 + $this->count_h6;
$counter = ( $h2_h6 == 0 ) ? '' : $h2_h6;
$message_end = '' . $counter . '';
if( $h2_h6 == 0 ) {
$message = 'No found. Add some!';
$array['content_editor']['count_h3_h6']['status'] = 1;
} else {
$message = 'Found, great!';
$array['content_editor']['count_h3_h6']['status'] = 0;
}
$array['content_editor']['count_h3_h6']['message'] = $message_start . $message . $message_end;
$array['content_editor']['count_h3_h6']['count'] = $this->h2_h6;
$message_start = 'Title keywords - ';
$counter = ( $this->found_keywords1_post_title == 0 ) ? '' : $this->found_keywords1_post_title;
$message_end = '' . $counter . '';
if( count( $this->keywords1 ) == 0 ) {
$message = 'No primary added.';
$array['content_editor']['missing_keywords1_post_title']['status'] = 2;
} elseif( $this->found_keywords1_post_title == 0 ) {
$message = 'No primary f
Don't repeat yourself
I know of the "Don't repeat yourself". Still it's getting messy in some places. The code below contains three blocks of code.
They are similar in some ways and different in others. It doesn't follow the same pattern in all ways.
What it does
It creates an array that I loop out on the frontend.
Question
Is there any better way to do this kind of stuff? Less messy, better structure?
```
$message_start = 'h2 tags - ';
$message_end = '' . $this->count_h2 . '';
if( $this->count_h2 == 0 ) {
$message = 'No tags found. Add some!';
$array['content_editor']['count_h2']['status'] = 2;
} elseif( $this->count_h2 == 1 ) {
$message = 'Some found. Too few!';
$array['content_editor']['count_h2']['status'] = 1;
} else {
$message = 'Many found. Great!';
$array['content_editor']['count_h2']['status'] = 0;
}
$array['content_editor']['count_h2']['message'] = $message_start . $message . $message_end;
$array['content_editor']['count_h2']['count'] = $this->count_h2;
$message_start = 'h3-h6 tags - ';
$h2_h6 = $this->count_h3 + $this->count_h4 + $this->count_h5 + $this->count_h6;
$counter = ( $h2_h6 == 0 ) ? '' : $h2_h6;
$message_end = '' . $counter . '';
if( $h2_h6 == 0 ) {
$message = 'No found. Add some!';
$array['content_editor']['count_h3_h6']['status'] = 1;
} else {
$message = 'Found, great!';
$array['content_editor']['count_h3_h6']['status'] = 0;
}
$array['content_editor']['count_h3_h6']['message'] = $message_start . $message . $message_end;
$array['content_editor']['count_h3_h6']['count'] = $this->h2_h6;
$message_start = 'Title keywords - ';
$counter = ( $this->found_keywords1_post_title == 0 ) ? '' : $this->found_keywords1_post_title;
$message_end = '' . $counter . '';
if( count( $this->keywords1 ) == 0 ) {
$message = 'No primary added.';
$array['content_editor']['missing_keywords1_post_title']['status'] = 2;
} elseif( $this->found_keywords1_post_title == 0 ) {
$message = 'No primary f
Solution
Architecturally, I'm adverse to hard-coding HTML tags within PHP code. If, for example, you wanted to migrate the code base from PHP to another language (Java, Python, Ruby, Go, Haxe, whatever) then you have to rewrite both the logic (PHP) and the presentation (PHP) code.
Consider decoupling the logic from the presentation so that you can change how the data appears without having to change how the data is retrieved. This simplifies switching implementation languages and opens the possibility to create different views of the same data for different devices.
To do this, you have to create an interface or contract between the logic and the system that renders the logic for the user. Fortunately, this is where XML excels.
Instead of hard-coding ``, you would create an XML document that represents the information you want to display. The XML document becomes the contract between the logic and presentation, which allows both to vary independently. Once the XML document is in place you can pass it to an XSLT Processor to generate the desired HTML content.
This offers the following advantages:
This has the following disadvantages:
Consider decoupling the logic from the presentation so that you can change how the data appears without having to change how the data is retrieved. This simplifies switching implementation languages and opens the possibility to create different views of the same data for different devices.
To do this, you have to create an interface or contract between the logic and the system that renders the logic for the user. Fortunately, this is where XML excels.
Instead of hard-coding ``, you would create an XML document that represents the information you want to display. The XML document becomes the contract between the logic and presentation, which allows both to vary independently. Once the XML document is in place you can pass it to an XSLT Processor to generate the desired HTML content.
This offers the following advantages:
- Decoupled. The presentation logic moves to XSLT, completely decoupling it from the PHP code.
- Migration. Migrating from PHP to another language means the presentation logic need not change.
- Devices. It becomes trivial to change the presentation for different devices (use a new template).
- DRY. With display logic in a central location, duplicated code is easier to eliminate.
- Databases. Many databases now allow constructing XML documents within stored procedures, eliminating complex code readily expressed in SQL.
- SaaS. It simplifies creating a SaaS API -- transforming XML to JSON is fairly easy a solved problem.
This has the following disadvantages:
- Effort. Migrating to an XML/XSLT solution for presentation takes a lot of work.
- Knowledge. Introduces a second programming language (XSLT) into the software.
- Developers. Far fewer developers know XSLT than PHP.
- XSLT 1.0. Using PHP, the processor is limited to XSLT v1.0 (many great features are in XSLT 2.0).
- Bugs. The XSLT Processor has some pesky known bugs, which could be avoided by moving to XSLT 2.0 with the SAXON processor.
Context
StackExchange Code Review Q#26803, answer score: 2
Revisions (0)
No revisions yet.