A common debate is whether PHP adequately separates business logic from presentation. Some argue that PHP developers ought to use a template engine like Smarty[0] to separate PHP code (business logic) from the HTML (presentation), while others argue that PHP and HTML, especially with CSS, are already segregated enough. I'm sure most PHP coders have seen (or occasionally write) sloppy PHP code with HTML (and SQL) statements splattered throughout. Notwithstanding, I believe that careful PHP coders can adequately separate business logic from presentation without using a template engine.

I've found PHP's heredoc[1] string syntax to be valuable for achieving this separation. For example, imagine creating a simple "contact us" form for a company that already has a novice web designer.

You can do something like this...
//
// contact_us.php
//
<?php
        $begin_comments_form = "<form action='' method='POST' />";
        $name_field = "<input type='text' name='name' size=40 />";
        $email_field = "<input type='text' name='email' size=40 />";
        $comments_field = "<textarea name='comments' rows=40 cols=4 />";
        $end_comments_form = "</form>";
        $current_date = date("m-d-Y");
        require ("contact_us_presentation.inc");
?>

...while the web designer can use something like this:
//
// contact_us_presentation.inc
//
// Dear Web Designer with No Knowledge of PHP:
//
// Please do not modify the lines that contain the text "EOD".
// Otherwise, feel free to use the variables $name_field, $email_field, and
// $comments_field in any way you like. All three must be nested in the
// variables $begin_comments_form and $end_comments_form
//
echo <<<EOD
$begin_comments_form
<h2 font-variant: smallcaps;'> Today is $current_date. Thank you for your comments! </h2>
<h3> Please enter your name: </h3> $name_field <br/>
<h3> Please enter your email address: </h3> $email_field <br/>
<h3> Enter comments here: </h3/> <br/> $comments_field <br/>
$end_comments_form
EOD;


This keeps the PHP readable and sans presentation, and it makes the HTML a piece of cake. The HTML-only web designer sees only three foreign things: "echo <<<EOD", "EOD;", and $_____ variables -- very easy to deal with.

Granted, this method does not allow the HTML coder to manage program flow (e.g. if, for, while statements) but I've found it to be very helpful, especially when _I_ am the PHP coder AND HTML web developer and need help keeping things organized.

FWIW.

Richard Miller


[0] http://smarty.php.net/
[1] http://us2.php.net/manual/en/ language.types.string.php#language.types.string.syntax.heredoc


Attachment: smime.p7s
Description: S/MIME cryptographic signature

____________________
BYU Unix Users Group 
http://uug.byu.edu/ 
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to