set the page's title after processing the body

Discussion in 'PHP' started by med87, May 2, 2011.

  1. #1
    Hello,

    Is there a way to set the page's title and some other meta tags after processing the page's body where the appropriate information will be (after db queries)

    I know that it can be done via javascript but this is not seo friendly, and it will fail if noscript. so it must be done server-side.

    The best way is php but my page structure is a full page that includes the main content with php include (pseudo frames). and because the main content is after the page's title, we cannot call the variable containing the page title before creating it.

    any suggestions please?

    Thanks.
     
    med87, May 2, 2011 IP
  2. Sepehr

    Sepehr Peon

    Messages:
    568
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might want to try putting your codes in a variable. You can create your <body> by adding it's parts to the variable step by step(depending on how your script works). and at the end add your <head> part to it's beginning.
     
    Sepehr, May 2, 2011 IP
  3. Projekt.Gopher

    Projekt.Gopher Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There are several ways you can do this. Like Sepehr said, you can do your processing first, saving info to variables like $meta[description], and $pageTitle etc... The insert them into your page like this:

    <?PHP
         $pageTitle = "Test Page Title";
         #some other queries and operations
    ?>
    
    <html>
         <head>
              <title><?PHP echo $pageTitle; ?></title>
         </head>
         <body>
              <div>content</div>
         </body>
    </html>
    Code (markup):
    The other way, would be to use a templating system, like XTemplate (not sure if this is still around, since I haven't used it in a while), or Smarty (requires PEAR). This allows you to do all your processing first, then calling separate html files, abstracting your code from your design.
     
    Projekt.Gopher, May 3, 2011 IP
  4. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    its called javascript!!!!!!!!!!!!!!!!!!!!
     
    srisen2, May 3, 2011 IP
  5. meltinzone

    meltinzone Member

    Messages:
    187
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #5
    Projekt Gopher has it down pat. imho.
    I've recently been learning php myself and this is the fastest most efficient way I'm currently using.
     
    meltinzone, May 3, 2011 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    You can use output buffering, and before you flush the buffer, go through and replace the <title></title> section by retrieving the buffer with ob_get_contents() then echoing that before discarding the buffer.
     
    joebert, May 4, 2011 IP