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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Separating HTML and PHP

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Is there any article to show the technique of separating HTML from PHP code
 
If you are asking about code in a web page on your end (a page you are developing...)

Typically a .php page that has html will have html code, and the php code will be enclosed in <?php ?> tags.

Sometimes, a .php page will have all php code where the entire code is enclosed in <?php ?> tags and the html code is generated there.

If you are talking about visiting a nice php webpage on the internet, and wanting to see the source, it's impossible. PHP is processed into html before the page is ever loaded into your browser.

[cheers]
Cheers!
Laura
 
You are right "ask the right question to get the right question"

I am talking about how to keep the php code free from html i.e. .php files contains only php code. .html should only have html code.

I hope I explained it right.
 
Just curious Oozy -
Can you give me a scenario where this is necessary?

I mean I use some php pages that only execute when variables are POSTed to them, thus they are never rendered on web browsers, thus there is absolutely no html in them. These pages are .php

Conversely, I have some very static webpages where only html is used, and no php in them. These pages are .htm(l)

And then I have Database-driven web pages where use php and html. These pages are .php

I'm having difficulty applying your question to any scenario I've encountered.


[cheers]
Cheers!
Laura
 
LTeeple

When you write custom PHP applications that are used by several customers who want their own look but the same functionality you will need to use HTML/PHP code separation.

Example:
A university offers a database driven FAQ system which can be used by sub-units such as colleges. Every college has their own "look" and feel web site in which they want to integrate the FAQs. Templates allow the complete separation of look and functionality. The customers, here departments, can change their look without having to take PHP code into consideration and without the danger of interfering with the functionality of that code.

Another scenario:
You write a PHP application that you will support for the forseeable future. The client has no PHP knowledge but is a savvy designer and design changes are expected to happen rather frequently. By separating into external HTML templates and "pure" PHP code you allow freedom to the designer and safety to your scripts.

Oozy
Depending on the scope of your scripts you might want to use templates or not. Templates will always mean more work for the server as the HTML and the dynamic data have to be parsed before sending the result to the client. If the pages are rather static that might be wasteful unless a caching mechanism is in place. If you have no concern about server load templates are wonderful. I've used them extensively and subscribe to the design/functionaly separation philosophy.

If we are talking about - excuse my language - dinky little script, go ahead and mix PHP and HTML.
 
Dears

My site is a tutorial site for Electrical Engineering students. The site will have tutorial and tools for the mean times. The tools are things like filter designer (i.e Lowpass, highpass, bandpass, etc.). I am planning to include other things in the future i.e. forum, more tools, etc. I really don't want to start wrong then I have to redo everything when the site gets bigger.

I am not familiar with templates and I am not planning to change the look of my site until I get all the functions (tutorial, tools, etc) of the site incorporated.

The question is how to I do the separation. Currently, I do this:

ohms_law.html

<?php

$html_code="

<p>Ohms Law</P>

...OTHER HTML CODE


"
?>

Then I have my code included this file and send it to the client

What do you think?
 
DRJ478:
"When you write custom PHP applications that are used by several customers who want their own look but the same functionality you will need to use HTML/PHP code separation."

DRJ478 you are great at paraphrasing! So for the above scenario, php includes would be useful?

[cheers]
Cheers!
Laura
 
Here's the scoop on templates:
1. You have a flat html file that has placeholders for variables, block definitions for repeating blocks etc.
2. You have a repository for the dynamic information.
3. The PHP code loads the template and usually uses regular expressions to populate the placeholders with dynamic values. Blocks can be parsed repeatedly with changing information, such as in a list.
4. The script "spits" out the final document to the browser. It appears as a "flat" HTML page.

Have a look at this:

There is a learning curve to templates. But once you are there, you can crank out apps with sophisticated GUI and rock solid code in a short time.
 
00zy:
You may also be talking about modularizing the functions of your site.

One simple way is to have a main PHP script which produces a basic HTML framework. That main script can then populate the framework with inclusions of other PHP scripts.

main.php would output a 3-row by 3-column table:

main.php:
Code:
print '<html><body><table>
<tr>
   <td>';

include ('output_navigation.php');

print '
   </td>
   <td>';

include ('output_more_stuff.php');

print '
   </td>
   <td>';

include ('output_still_more_stuff.php');

print '</td></tr></table></body></html>';

The included files produce additional content and can be reused. More files and be created with more content. You can rearrange the placement of content easily.

You can include program logic which can automagically change content based on user input.

Abstract that out and you can have a script which reads its configuration from a file or a database then output the specified content in the specifed location.

You could even make the additional content scripts themselves use templates.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Guys/Gals

Do I have, then, to use a thirdparty template or should I make my own?

If I have to, then which template should I use?
 
Dear all

I have been reading about templates and they seem nice; however, I got few concerns:

1. Do I have to use a thirdparty template? If yes, then is it Smarty, FastTemplate, PHPLib or what? Since my site isn't that big, do I have to make my own?

2. What is the best technique? Is it separating php code from html or separating business logic (i.e. retrieving data from a database or making a calculation from) from data presentation?

3. Which syntax should I use in the html: {NAME} or <?=$name?> (Like Smarty)

4. If I am going to make my own style, do I have to make a template for each page (in my case for Tutorials and Tools)
 
1. I'd go with the PHPLib template and make adjustements for whatever I need. I wrapped a class around it that makes instantiation etc. a one-line statement. Why reinvent the wheel?
2. IMO, complete separation between presentation and logic.
3. I suggest {!NAME!} since {blah} can run into trouble when there is JavaScript in the template.
4. A template can be just a "container". Very simple example:
Code:
<html>
<head>
<title>My Site</title>
</head>
<body>
{!content!}
</body>
</html>
The PHP code would parse whatever into the variable $content.
 
Try not to make simple things to complicated. OK, I've been using php for about 4 days now, and noticed that everyone seems to jumble up all the php and the html all smashed together. annoying!!

I've been using coldfusion for YEARS. ASP a bit. DO your LOGIC first, THEN output. It'll make your life so much better.

make your queries, set your variables, work your data at the TOP of a file. then do your html and output variables at the end of the file. its all in one php file, because thats what we're working with. nothing wrong with that. keeping logic seperated for output is extremly helpful for fixing code, makeing changes, or if someone elseever has work on the code.

nothing says annoying coding to me more than a line of 5 or more echos in php doing nothing but outputing html. sometimes it OK to ?> out of php and do your html, then <?php back into it when you need it. Same concepts with asp and cf.

work the data and logic as much as you can, then show results. sometimes you need to mix up html and logic, like with looped outputs and stuff. but moving as much of the php logic as possilble above the html helps clean up code a LOT!

PHP is already way to annoying to look at, this seperation can help, especialy if you learning still.



 
My favorite is having a kind of function "wrapper" I guess you could say. I built a function to produce the main portions of the HTML and all I do is fill in the body using whatever HTML code I choose. As I run through my scripts I just keep adding on to the variable $html and at the end I call my Print_Page($html); function and it creates the rest for me. Very quick and easy and still allows for a lot of control over the HTML.
 
I always make my menus dynaminc, one include or function, same with headers and footers.

 
Avoid loading lots of data into variables or you'll use a lot of memory on the server, if your getting a lot of hits you'll notice the performance, but I agree with imstillatwork's comment about doing most of the logic at the top of the file.

Reuse as much as possible! When I build sites, I usually have a common.php; that connects to db's, checks user authentication, loads base classes for the page; a header.php, sidebar.php and footer.php, with common html elements for convenience, these usually include PHP, but only related to the particular file.

--BB
 
The source of the problem I think, is the tutorials that are mostly so poorly written. people take them as absolute truth, and some people never see more effecient ways of coding.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top