The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

embebbed languages (php and html) and code organization

Here's my typical PHP code for table based presentation:

<?

foreach ( $friends as $friend ) {

?>

    <tr>
      <td><?= $friend[name] ?></td>
    </tr>

<?

}

?>

This gives a decent separation of HTML and PHP, wich the alternative does not:

<?
foreach($friends as $friend) {
 echo "<tr><td>$friend[name]</td></tr>";
}
?>

This alternative tends to build some strange code, specially wheen its necessary to use commans within the echoed strings.

Also if we need to change the HTML we can mess up the PHP code within more easily.


However the approach I use is strange for code reuse because functions tend to look strange if they have the escaping (<? ?>) characters within.

<?

function display_table_list ( $information, $index ) {

  foreach ( $information as $info ) {

?>
      <tr>
        <td><?= $info[$index] ?></td>
      </tr>
<?
  }

}
?>

What do you think of all this?
MdP
Monday, April 02, 2007
 
 
No offense intended, but you are about two or three years behind the times. All three techniques are acceptable, and there will be cases when one is prefered over another because it interrupts the train of thought, the least.

For example, I use the third option often when I have a list of elements and I need to wrap a conditional around each element before formatting and printing it. Conceptually, it's easier for me to think of it as two separate and distinct steps, parse and print.

Having said that, there are a number of frameworks and "templates" that arguably neatly separate code from content. If you combine them with cascading style sheets, you'll have the code, the controls (form buttons and fields), and the formatting each in separate places. Designers really only need to work with the formatting files.

It's been a while since I used PHP so I don't know what the current framework du jour is.
TheDavid
Monday, April 02, 2007
 
 
Framework != Templating system.

If you're going for a simple separation of content and logic, a templating system - like Smarty - is probably all you need.
KC Send private email
Monday, April 02, 2007
 
 
Template systems and frameworks add complexity to the program and the development process.

Is it really worth in a language like PHP that is itself a kind of template processing language?

Complexity to the program because parts of the template system become part of the program.

Complexity to the development program because some templating systems require that you learn a new language.
MdP
Tuesday, April 03, 2007
 
 
You can use PHP for both code and templates and still get good separation. Just put the templates and logic (a foreach loop isn't logic in this sense) in separate files, and use include or whatever to generate the HTML.
PHDude Send private email
Tuesday, April 03, 2007
 
 
I've never wrapped HTML code in a function in PHP -- instead I use separate files and include them.  You can even include files inside functions and any local variables from the function are available to the included file.

Also, I use the alternative syntax for control structures when I embed PHP into HTML code.  So your example becomes:

<? foreach ( $friends as $friend ): ?>
  <tr>
    <td><?= $friend[name] ?></td>
  </tr>
<? endforeach; ?>

Which looks a bit more like HTML tags and is a bit easier to follow.
Almost H. Anonymous Send private email
Tuesday, April 03, 2007
 
 
+1 to TheDavid.  All three are essentially fine, and it really just depends on what you're trying to do (and I'm not sure it's much more than a preference thing even then).  Languages like PHP, ASP, CF, etc. just have this "messy looking" thing to contend with by definition.
Chris
Tuesday, April 03, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz