Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple design question 2

Status
Not open for further replies.

mmaz

Programmer
Nov 22, 2000
347
Hi,

Can anybody can make suggestions as to which approach I should consider if I want to separate my HTML code from my php server-side code?

Thanks,

Marie
 
I'm very new to php, and I was wondering if there's anyway to separate the presentation layer (html) from the code. I've programmed a lot with ASP, and I always thought it was very messy because the html is mixed with the server-side code(vbscript or javascript).

It seems to me that php works the same way, or is there any other way to do it in a cleaner manner?

Thanks,

Marie
 
You can, in PHP, produce a script that looks like:

Code:
<html><body>
<?php echo $foo; ?>
</body></html>

You can also produce a script which is functionally equivalent, which looks like

<?php
print '<html><body>'. $foo.'</body></html>';
?>

I personally prefer the latter style. (In ASP, a script equivalent to the second example would be more efficient -- ASP doesn't handle context switching well. I do not know if this is the case in PHP.)

It is also possible to create an HTML template as a separate file, and use PHP to process that template. A simple example is:

foo.html:
Code:
<html><body>!!FOO!!</body></html>[code]

foo.php:
[code]<?php
$keys = array (&quot;!!FOO!!&quot;);
$values = array (&quot;the foo value&quot;);

$file = fopen (&quot;foo.html&quot;, &quot;r&quot;);
while ($line = fgets($file, 1024))
{
     print preg_replace ($keys, $values, $line);
}
fclose ($file);
?>

But then, you could to the equivalent in ASP/VBScript, too.

 [i]Want the best answers?  Ask the best questions:[/i] [URL unfurl="true"]http://www.catb.org/~esr/faqs/smart-questions.html[/URL]
TANSTAAFL!
 
The third solution will probably be the best in my case.

So let's say I only had one textfield named txtHello, and I want its value to be &quot;hello&quot; when the user opens the page, how would I do this?

Thanks,

Marie
 
forget my last post. I was thinking my form would be on foo.html, but it's not, so there's no problem.

thanks again,

Marie
 
I've always been a big fan of separating logic from design whenever possible. PHP allows you to do that very well with include(). You still server your HTML through the PHP engine but your page itself is either predominantly code, or predominantly design. I've used both styles depending upon the nature of a site. If I find I am using a lot of the same HTML on multiple pages, (headers, footers, etc), I will put those blocks of HTML into separate PHP files and just call them as includes.

<?PHP

Some code here but now I want to display the header

include(&quot;path/header.php&quot;);

where header.php contains the actual HTML header echo to the browser. It saves a lot of time, particularly where maintenance is concerned. If I want to change an attribute, say, in the body tag on all of the pages for my site, I only have to change the one file, header.php. This method also allows you to re-use blocks of PHP in the same manner.

Ok, so I'm not great at explaining things. Look into the PHP include(), I think that will be exactly what you were asking about.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top