SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Include for <head>

    Hello I wanted to know if I could use a php include for the head of my site. I want to keep it clean and not have unnecessary junk in pages that don't need it (for example if I don't need to have jquery in that particular page).

    And I want to be able to change all the pages at once. I'm doing it with the sidebar/ footer right now.

    Let me know if I should explain more... thank you for your time.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Simply create a *.php file that contains all the nessecary script and resource management then include the file are part of the <head>, see the below example:

    PHP Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title></title>
        <meta charset="utf-8">
        <?php include('/path/to/file/resources.php'); ?>
    </head>
    <body>
        
        <!-- Do something here -->
        
    </body>
    </html>

  3. #3
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bless you I wish I could give you a hug. This place has been so amazing and helpful. I wish I'd found it years ago when I started learning. Have a good day , I'll
    do this now.

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You could create the title as well from the page name from the URL or a variable in the page above the header include.

  5. #5
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your very welcome, the example i posted above could be considered as a template action since your including dependencies only required by the current page.

  6. #6
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    You could create the title as well from the page name from the URL or a variable in the page above the header include.
    Thank you for that

  7. #7
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    Your very welcome, the example i posted above could be considered as a template action since your including dependencies only required by the current page.
    Okay

  8. #8
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,822
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @bzcastell

    I use a further improvement to @chris.upjohn's example:
    PHP Code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title></title>
        <meta charset="utf-8">
        <?php if( isset( $jquery_wanted ) && $jquery_wanted ) { include('/path/to/file/resources.php'); } ?>
    </head>
    <body>
        
        <!-- Do something here -->
        
    </body>
    </html>
    The additional overhead is minimal because the $jquery_wanted test is server side.

    edit: added missing bracket

  9. #9
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    @bzcastell

    I use a further improvement to @chris.upjohn's example:
    PHP Code:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title></title>
        <meta charset="utf-8">
        <?php if( isset( $jquery_wanted ) && $jquery_wanted ) { include('/path/to/file/resources.php'); } ?>
    </head>
    <body>
        
        <!-- Do something here -->
        
    </body>
    </html>
    The additional overhead is minimal because the $jquery_wanted test is server side.

    edit: added missing bracket
    Oh! So I can also include whichever scripts are specific to that page? Now my brain needs a second to catch up. I love this.

  10. #10
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    On my site. I have a file called header.php, which has the doctype to the opening container, like:
    PHP Code:
    <!DOCTYPE html >
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link rel="shortcut icon" href="/images/favicon.ico" />
    <title><?php echo $title?></title>

    <style type="text/css">
    <!--
    @import url("/incs/home.css");
    <?php echo $addCSS?>
    -->
    </style>
    </head>
    <body class="yui-skin-sam">
      <div class="container" id="bodyTxt">
      <?php include_once($_SERVER['DOCUMENT_ROOT']."/incs/backlinks.php");
        include_once(
    $_SERVER['DOCUMENT_ROOT']."/incs/navbar.php"); ?>
        <div id="content">
    Then when I want a new page, I start a new file and it looks like:
    PHP Code:
    <?
      $title 
    "xxx"// this is for <title>
      
    $page_title "xxx"//this is for breadcrumbs if I want a custom title other than the default
      
    $addCSS ""//custom CSS for this page only
      
    include_once("/path/to/header.php");
    ?>
     //start typing html/body text here
    <?php include_once("/path/to/footer.php");?>
    Ryan B | My Blog | Twitter

  11. #11
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you RyGuy, It seems as though there are a few ways to do this... I'm in love with PHP. It's so organized. eventually my pages will just be one line, haha!

  12. #12
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You are welcome. I was getting tired of opening a file, copying, going back and making a new file, pasting, removing the content, typing in the new content, save, tweak. Now here's my title, grab the header, type body, grab the footer. On my to do is make a flag, so if wide is true, don't grab the menu (within header) and set the content holder to the width of my container. It would probably take 30 minutes to do, so some day.
    Ryan B | My Blog | Twitter

  13. #13
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I started off like you Ryan but now have a switch in the header which also contains the navigation information. Add a new page save the info into the switch statement and you are done - new button created automaticaly but only realy works for smaller sites.
    I also have an include for the footer so my page is include header - content - include footer.

  14. #14
    SitePoint Enthusiast bzcastell's Avatar
    Join Date
    Jan 2012
    Location
    San Diego
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rguy84 View Post
    You are welcome. I was getting tired of opening a file, copying, going back and making a new file, pasting, removing the content, typing in the new content, save, tweak. Now here's my title, grab the header, type body, grab the footer. On my to do is make a flag, so if wide is true, don't grab the menu (within header) and set the content holder to the width of my container. It would probably take 30 minutes to do, so some day.
    It would be worth it to do now, before you add stuff on top of it. That's what I'm doing and I have to keep going back and fourth. I want to nip it in the bud, though so I don't have to when my website has more pages. I just want everything to be perfect and organized. I don't know what a "flag" is, but i'm going to, lol.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •