user2804599
user2804599

Reputation: 97

Include pages to index.php or create top and bottom part of page and include to all content pages?

I haven't't created a webpage in a long time but I decided to create on now, and probably like everyone else I wanna learn the best way of doing this. But I've stumbled upon a little dilemma on how to process my pages. My first idea was to create one main page with all the CSS and everything needed, then 1 part of the site dedicated to each page's content. Then by using a page variable showing all the content for each site, example. I have index.php as homepage, then visiting index.php?page=aboutme would make index.php include the aboutme.php in the part dedicated to each page's content. And only having text and some pictures etc in the aboutme.php. However I believe this will be a pain when people google my site and finds interest in the aboutme.php so they get linked to example, mypage.com/pages/aboutme.php and only sees the text and pictures but no CSS and not "the front page". The pros of this is of course that editing pages will be easy, I can create links etc in php loops by just checking contents of maps on my page.

The second example is that I take everything in my index.php above the part dedicated to page content, create a separate file for this, calling it top.php. Take all thee parts under the page and call it bottom.php. Then for each new page I create I include the top and the bottom parts. Making the link mypage.com/aboutme.php include the CSS and "the frontpage". Pros being that you actually can google subpages. This seems like the best idea to me, but like I said, I haven't created a lot of webpages lately and I've seen plenty use of both methods.

I've seen both types of webpages so I kinda wondered which one is the best practice?

Upvotes: 1

Views: 3768

Answers (3)

benathon
benathon

Reputation: 7633

I personally like a 3rd approach which is templating. Here are two options for PHP listed in order of my preference:

  1. http://templatepower.codocad.com/manual/index.php
  2. http://www.smarty.net/

Using template, I suggest you create a layout template which contains all the style and "header" and "footer" of your page. Then dynamically generate the "content" using one template per page. This allows you to use your url scheme of "index.php?page=aboutme". Also, by doing this you actually don't expose the bare naked content page to google.

I've found this is the most simple and maintainable way to build dynamic PHP pages with a shared header/footer.

Upvotes: 0

Claas Wilke
Claas Wilke

Reputation: 1991

I would prefer the second option with top and bottom part php files. However, this can become complicate when you need to process context information within them.

For example imagine a top.php containing a table of contents including navigation and highlighting of the currently shown page. I guess, in this situation it would be more appropriate to use your first proposed approach. To alter the table of contents depending on your current page variable (e.g. 'aboutme').

Upvotes: 0

bboysupaman
bboysupaman

Reputation: 1334

I recommend just using php includes for the header, nav, and footer elements and then placing a class (home, about me, contact, etc.) on the body tag (for highlighting nav elements and such). This way the content is on separate pages and gives you more freedom, but saves you from having to retype all of the navigation and stuff each time.

Example:

    <!DOCTYPE html>

<head>
    <title>
        Hello World
    </title>
    <meta name="description" content="Use this area to provide a description of this page.">
    <?php include '_php/_includes/head.php'; ?>
</head>

<body class="home">

<?php include '_php/_includes/header.php'; ?>

<!-- 

Content Goes Here

Remember: 'div' elements should only be used for non-semantic styling purposes. Content should
be placed in either a 'section' or an 'article' tag.

-->

<?php include '_php/_includes/footer.php'; ?>

</body>

</html>

Upvotes: 1

Related Questions