I've been stuck up on this topic for a while. As a project to help me learn MySQL, PHP, and database security a while back, I created a lightweight blog system. It blew up on me and a few of my fellow programmer buddies wanted to use it and now I am working on improving on its features.
The way it works, simply put, is that a blog post is uploaded, the title, author name, and date are stored in a database, but the actual content is used to dynamically generate an html page which is where the actual blog post is viewed. In the database, the path to each blog post is saved so that it can be used to access and view the blog post page. One of the features of my blog system is that the generated HTML is fully customizable, so you aren't limited by a specific template. The way I am currently doing that is by using a function that takes parameters of the values and then returns a HEREDOC with the format desired, which is then written to a file whose name is based on the post title.
function get_full_post_html($title, $author, $date_posted, $text) {
return <<<EOT
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<h3>$author</h3>
<h4>$date_posted</h4>
$text
</body>
</html>
EOT;
}
Not the cleanest solution, which is one of the reasons I am looking for a better way. The other reason why I need to rethink this, is because I am currently adding support for comments, which means the html page that is generated now needs to be dynamic, with my PHP method for getting the comments. Whereas before the blog posts were static content, now with comments enabled, the page becomes dynamic.
I had a look at this question, but frankly the answers were vague and didn't make much sense. The main question I am asking is, how are dynamic pages typically generated in PHP? For example, take this blog post on A List Apart: http://alistapart.com/article/tweaking-the-moral-ui. It has a physical page for the post, tweaking-the-moral-ui
, but still has dynamic features like comments, ads, etc. How is this done?
Here is the link to this entire project on GitHub, if you are interested in understanding how it works in depth.
` ... etc, exactly like you think, but full html page should be as a static template and not in a function, every time the page is accessed your functions should assign variables in that template and display it. That's what PHP is for.
– Mihai Iorga Dec 22 '14 at 09:45