Reputation: 8530
If I can ask that answers refrain from pointing towards frameworks or other libraries, I would appreciate it. I am aware of those, and am after more of an answer based on experience for quick little hacks.
Start with some base html
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Page Title</title>
</head>
<body>
// something is going to happen here, could be more html, or php
</body>
</html>
Let's say you will have hundreds of pages, of course, you want to modularize this in a way to have less code duplication.
I have seen, used, and debated many a tactic.
The basic includes method:
header.inc.php
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Page Title</title>
</head>
<body>
footer.inc.php
</body>
</html>
The stub of all of your hundreds of php pages:
<?php include 'header.inc.php'; ?>
// html or other php can go here
<?php include 'footer.inc.php'; ?>
Get a little more fancy, and have a var.inc.php and you could set $page_title and have that in front of the first include, saving the time of editing it out of hundreds of pages.
The functions method:
show.inc.php
function display_header($page_title) {
echo "
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>$page_title</title>
</head>
<body>";
}
You do the same for the footer, I suspect you get the idea. I know a lot of you are thinking objects at this point, which I would like to also avoid for this discussion.
Consider you are trying to teach a very young kid some principles, or that you are trying to get a designer to move into some form of the modern age.
Which either method, you start to run into cases where you are going to look at the output of the html and php in a browser. There is a very clear tab indention and structure to the source output that hits the browser. And most of the time, looking at the source of a site, I see html that is pretty abysmal. All one line, or varying forms of indenting.
How in a simple way do you solve this?
That display_header() function is going to spit in \t's all over the place, or spaces, depending on your editor, that are going to make the rendered output a mess.
There is this option:
function display_header($page_title) {
echo '<html>';
echo '<head>';
echo "\t" . '<meta http-equiv="Content-type" content="text/html; charset=utf-8" />';
echo "\t" . '<title>' . $page_title . '</title>';
echo '</head>';
echo '<body>';
}
Then, you get into cases of:
echo '<table>';
foreach ($variable as $item) {
echo '<tr><td>' . $item . '</td></tr>';
}
echo '<table>';
versus
echo "\t" . '<table>' . "\n";
foreach ($variable as $item) {
echo "\t\t". '<tr><td>' . $item . '</td></tr>' . "\n";
}
echo "\t" . '<table>' . "\n";
And now, you have just really made a mess of your php. So I start thinking output buffers, and I am going to totally freak the designer out with that concept. But I have just mixed more html with php that I would be proud of.
And I honestly have never been able to settle up with this stuff:
<?php foreach ($variable as $item) { ?>
....
<?php } ?>
Both yields much better generated html, but to me, it also yields a more complex php file. Pretty basic question, I am sure there will be some strong opinions, and a desire to push towards frameworks and OOP. I understand those methods, but again, am looking for best suggestions looking at giving some tools to a person who is never going to have a passion for programming, and just wants to get stuff done, in a way that will help them be efficient.
Upvotes: 1
Views: 4828
Reputation: 17246
I think you should re-evaluate your aversion to the last snippet. PHP is at its best as a templating language. When you use it that way, you can easily take a design mockup and convert into a dynamic page. You can also use standard HTML tools with it, take a look at ZenCoding for an example of what I'm talking about. You can have proper syntax highlighting and all the rest of the goodies. You also can trivially maintain some semblance of indentation and consistency without any extra effort at all.
Now I understand the tension you're feeling due to things getting messy and ugly. The key is to move complex logic out of the template into separate pure PHP files so that the template file feels more like HTML than PHP. iddqd suggests using Smarty as a templating language, but I have never liked Smarty. I believe it ends up being just a pointless redirection rather than a useful abstraction.
Of all the programming languages out there, PHP is one of the best as a templating language. There's no need to subvert it to be like any other programming language.
Upvotes: 1
Reputation: 526
As your code grows more complex, and your designs more complex, I think you'll find that templating is the answer. In the end, presentation should be separate from logic and this is the core of the MVC model.
I personally like Smarty
Upvotes: 1
Reputation: 45032
If you really care about the HTML layout when viewing the source then maybe look at PHP Tidy if you build up the HTML you can then output it with various options like indentation and lots of other configs.
See https://www.php.net/manual/en/book.tidy.php
Upvotes: 1
Reputation: 125287
Forget about the indentation of HTML output when writing PHP. Otherwise you're spending far too much time worrying about something completely trivial.
If you need to clean up the HTML output to make it easier for a designer to read, run it through Tidy. If this seems inefficient, remember you only need to do this in development, not in production.
Upvotes: 6