Jump to content

Dynamic PHP Website Style


Zeradin

Recommended Posts

When I first was learning php code on my own I figured to do it like this:

 

each page would look like

<?php include('header.php')?>
<!-- where header had everything that was static on every page down to the container -->
content of page
<?php include('footer.php')?>
<!-- the end of the page, close the header and footer -->

 

I then took an internship and they taught me to do it this way:

<html>...<body>
<?php
$page = $_GET['p'];
if($page != NULL)
{
include($page.'.php');
}
else
{
home page
}
</body></html>

 

The latter way seems to be more widely used but I'm finding the first way is a lot easier. I run into problems titling pages the second way and a lot of other things that could be done much easier when using the first method. I've even started making mySQL entries for each page when I use the second way so I can grab a bunch of style variables out of the database from the page.

 

I'm wondering what you use or recommend and what the advantages/disadvantages are that I'm not thinking about.

Link to comment
Share on other sites

The second method only works if rendering is done at the very last in your application. You could then for example create a head object that holds head information like meta, title, link, script, base, ..

 

$head->setTitle('Title Here');

 

Or you could use something like Smarty but instead of using Smarty directly modify it your needs:

 

$smarty->setTitle('Title Here');
$smarty->addMeta(..);
$smarty->addScript(..);

 

There are many ways you can go about it what also makes it so easy/hard to develop an application because you have so many possibilities and you can not just select just one without experiencing the (dis-)advantages of it first.

 

My best advice would be experiment try things see what works for you. If something gives you trouble or something takes more work then actually should be required try to solve it in the most simplest manner.

Link to comment
Share on other sites

The second method only works if rendering is done at the very last in your application. You could then for example create a head object that holds head information like meta, title, link, script, base, ..

 

$head->setTitle('Title Here');

 

Or you could use something like Smarty but instead of using Smarty directly modify it your needs:

 

$smarty->setTitle('Title Here');
$smarty->addMeta(..);
$smarty->addScript(..);

 

There are many ways you can go about it what also makes it so easy/hard to develop an application because you have so many possibilities and you can not just select just one without experiencing the (dis-)advantages of it first.

 

My best advice would be experiment try things see what works for you. If something gives you trouble or something takes more work then actually should be required try to solve it in the most simplest manner.

 

What do you mean if the rendering is done last? I mean... I guess I know what you mean, but how is this accomplished? Can you point me in the right direction?

Link to comment
Share on other sites

Well your first approach load the header.php file which lead to the title and meta information already sent to the browser. Your second approach is more dynamic but still does not allow you to modify title nor meta information. The only way for you to maintain the possibility for modification is to keep your html file from being sent to the browser you can for example create an object (DOMDocument, Smarty, ..) that will hold your entire website layout until you believe you are completly done and ready to sent it to the browser. This approach allows you to modify everything until it is sent to the browser. You can even cancel the processing and start a new one by redirecting the user to another section of your website.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.