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 wOOdy-Soft 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
 
Much of the design of the page can be controlled via css, in an external stylesheet.

You however need some html to keep the css in place.
How you keep code away from the html, is not very important, as long as it's easy to modify the html without damaging the code.

Some CMS have html with "modules" that will be replaced by content..

The "modules" can be defined like: {content} {title} and then be replaced by the PHP..

eg.
Code:
<html>
<head>
<title>{title}</title>
</head>
<body>
{menu}
{content}
</body>

then you have some php which gathers the menu, content and title and then replaces the template occurrances of {title}, {body} and {menu}, etc. with its true elements for the user display.

you then need to also define some layout for the menu, content, etc. but that's not very complexed.

Olav Alexander Mjelde
Admin & Webmaster
 
sleipnir214 recommends using heredocs in a similar post I placed a while ago.

Change contexts taxes server performance and 'print'ing is too messy to debug.

Changing contexts with messy print:
Code:
<?PHP

$var = "Hello World!";

?>

<html>
<head><title>My test</title></head>
<body>
<? PHP print "<div align=\"center\"><font color=\"blue\">".$var."</font></div>";?>
</body>
</html>


With heredocs:
Code:
<?PHP

$var = "Hello World!";

print<<<HTML

<html>
<head><title>My test</title></head>
<body>
<div align="center"><font color="blue">$var</font></div>
</body>
</html>

HTML;

?>

 
The point of templates is that it seperates logic from design. This comes in handy when you work on a large project and you have a couple of programmers and a couple of designers. Neither one wants to really worry about what the other is doing. What makes me so qualified to answer? Well, I work for a large project that wrote their own template engine, and work on that very principle, seperating logic from design. You may have heard of us. phpBB. :) What's the point of templating with Smarty or similar? Well, for one, it makes things easier to maintain. Secondly, it can speed things up significantly, especially if you compile your templates.

OOzy: I would stick with an already written one, especially if your site is small. It'll save you a lot of headache on down the road. However, if you write your own template engine, you now know the code intimately, and can fix problems that arise. Your call. All of the template engines above are great. I've used both Smarty and FastTemplate, and which one you use is up to what your needs are. If your site or project is small, you might want to use FastTemplate as it doesn't have as much features that you will end up not using. I hope that answered your question. :)

----------------------------
"Security is like an onion" - Unknown
 
A technique I've used is the generate all the data you need in one function and return it as an XML string. I then use XSTL to transform this data with an XSLT template which contains how the page should look. It then becomes throwaway should you want to replace the format of the screen. The advatage being you don't have to pick put the code from the HTML.
If your in intranet mode you can do the transform in the browser, only sending down the XML to already cached XSTL template, it's really quick !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top