2

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
djechlin
  • 59,258
  • 35
  • 162
  • 290

3 Answers3

2

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.

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • Other framework options include [Agavi](http://agavi.org), [Symfony2](http://symfony.com), [Nette](http://nette.org), [Yaf](http://pecl.php.net/package/yaf), [Zend Framework](http://framework.zend.com), [QCodo](http://www.qcodo.com), [Lithium](http://lithify.me/), [Yii](http://www.yiiframework.com/) and many more. – Treffynnon May 24 '12 at 20:54
  • MVC is one of the most general patterns used in web development. Go here: http://ootips.org/mvc-pattern.html for more informations about it. – bitfox May 24 '12 at 20:59
  • This all sounds well and good but could you point me in the right direction for how to use this to solve my problem of templated HTML code? – djechlin May 24 '12 at 21:00
  • djechlin: Each "template" is a view. Your controller layer would likely be what's processing the JSON into objects to pass into your view. – hsanders May 24 '12 at 21:04
2

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.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
hsanders
  • 1,913
  • 12
  • 22
1

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.

Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57
matthewvb
  • 932
  • 2
  • 11
  • 21
  • I do agree with @AlienWebguy about using an MVC framework. If you have a big or complex PHP app, this is the way to go. My answer assumes you just have a few files and are doing something simple. – matthewvb May 24 '12 at 20:49
  • There's a dynamic number of templates whose input will be specified by a JSON string stored in a database, if that gives you an idea of whether this is a few lines or something simple :P – djechlin May 24 '12 at 20:50
  • 1
    If you need just 1 php file to do all the templating and database pulls (parsing the JSON and outputting whatever the template needs), then I'd say this is simple and stick to a simple layout. If you're going to need multiple PHP files to handle different types of templates from your DB, then go with the MVC framework. – matthewvb May 24 '12 at 20:55