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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Other framework options include Agavi, Symfony2, Nette, Yaf, Zend Framework, QCodo, Lithium, Yii and many more.
MVC is one of the most general patterns used in web development. Go here: ootips.org/mvc-pattern.html for more informations about it.
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: Each "template" is a view. Your controller layer would likely be what's processing the JSON into objects to pass into your view.
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.

2 Comments

@Joy: Please consider only adding code formatting to things that are actually code (HTML tags, paths, class names, etc). Adding it to every buzzword is very distracting.
@eldarerathis Thanks for the suggestion. I will keep that in mind from now on.
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.

3 Comments

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.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.