How to structure php that generates html

1.7k views Asked by At

I have a php file and a bunch of HTML elements that include:

  1. header
  2. css
  3. a "spacer" element used repeatedly, static
  4. a bunch of templates that will take arguments from the PHP code for what to insert

Basically I'm wondering what the right way to factor all these files in. I'm guessing I should have a file.css in my folder that the php will slurp up into memory and drop in the header information. Should the spacer element be saved as "spacer.html", and the PHP will slurp up one copy of it into a string and drop it into the HTML as necessary? Or should all of these just exist as string constants in the PHP?

It's a little trickier for the dynamic "template" elements because I'm not sure how to separate the HTML and let it have markers that the arguments get dropped into.

3

There are 3 answers

4
AlienWebguy On BEST ANSWER

You should look into MVC. A popular one right now is CodeIgniter. The idea is you take those HTML "templates" and create (V)iews. PHP uses a (C)ontroller to direct the user and call on (M)odels to fetch data, then send the appropriate variables to the (V)iews for the browser.

3
matthewvb On

I usually suggest using some good ol' php includes to tackle this.

File structure wise, I'd probably have an index.php file -- which (without knowing much about your templates in #4) would outline the page. You could call in the header.html page or put the header right in the index.php file.

The css file would best be named: style.css (this is pretty standard) and you can call that from your header file.

Not sure if you're getting at this, but you can include HTML in a .php file. You'd just name the file something like: index.php and then your code can be:

<p>This is HTML.</p>

<?php echo("This is PHP."); ?>

And intermix them throughout.

2
hsanders On

I've noticed some of the other answerers (SP) mentioned MVC. MVC is incredibly important because it allows you to separate your business logic from your "view" display/UI layer and your database logic from your business logic.

Like, AlienWebguy, I'd recommend Code Igniter, but there are a number of other good frameworks out there for PHP.

As far as what it appears that you're asking is how you should structure both your view layer and business logic. If I have something common like a header and footer, I'll put them in

view/include/header.php and view/include/footer.php

The header file will generally contain the <html> tag, the style sheet link(s), any common javascript script files, and a common header (like the logo and navigation). The footer file will generally contain the copyright info, any footer links, and the </body></html>.

Generally what you should look to do in creating your views effectively is to have them process model objects to display the HTML, and generate absolutely no HTML in your controller layer. E.G.

<table>
<?php
foreach ($users as $user) {
    printf('<tr><td>%s</td><td>%s</td></tr>', $user->id, $user->user_name);
}
?>
</table>

Doing that makes things a lot cleaner by avoiding interspersing concerns at the wrong "layer".

The other thing you can do, if you're not interested in writing straight PHP in your views, is you can use a templating engine. Code igniter includes support for (but you don't have to use) a template engine.