incrediBILL - 7:43 am on Nov 13, 2011 (gmt 0)
Basically, I like to keep the design and code separate as much as possible which allows the designer to keep designing and the programmer programming without stepping all over each other, or one waiting on the other to complete something, speeds up projects and allows a broader use of WYSIWYG design tools like DreamWeaver or MS Expression Web over the full lifetime of the project/site for quicker changes.
With that in mind, I prefer doing full page designs with variables embedded and substituted on the fly without having to put snippets of PHP code in the HTML source itself, or worse yet, having HTML intertwined all over a page of PHP code.
IMO, it's easier to implement sites when you separate design from code, the way I've been doing it in other platforms for ages. Code and HTML all mixed together is something I've always disliked about how most implement their PHP code and sites.
Includes don't cut it unless you just want to include a generic header and footer without any dynamic variables included in the code.
Maybe there's an easier way of doing this, but this is the best I've come up with so far.
$mystr = file_get_contents("somepagedesign.html");
eval("\$mystr = \"$mystr\";");
The above will basically read in a chunk of HTML and then substitute any $variables found in that HTML without having to have any inline PHP code in the HTML page itself. This method simplifies putting variable data back in forms, especially lengthy forms, among many other things.
Assuming the following variables in PHP:
$ua="Internet Explorer 7.0";
And following HTML was contained in somepagedesign.html:
<p>Your IP address is: $ip<br>Your user-agent is: $ua
The code above would display:
Your IP address is: 127.0.0.1
Your user-agent is: Internet Explorer 7.0
I find it's actually much faster to code pages this way and with a few tricks, you can even call out subsections of a page for displaying tables of data, etc.
So the question is, is this the best way to do this or are there other PHP tricks for accomplishing a cleaner separation of HTML and PHP code that supports variable substitution that I'm not aware of?