SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    stalling code until something else has been done

    I know the title of the post is a bit vague, but I did not know how to best phrase this.

    I want the title of a page, which is in something like "header.php" to be determined by something that comes later in the code, like something in "content.php". In particular, I am trying to make the title of a page dynamically generated, through something like:

    PHP Code:
    <title><?php echo $title ?></title>
    But this yields an undefined variable if I assign something to title AFTER this line of code on a page. I'd appreciate any solutions or best practices to use when handling with this issue.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    There's simply no way for you to do it.

    The proper method, is to process all the values before the HTML.

    It's called separation of logic and view.

    I'm guessing you have something like

    Code:
    <html>
        <head><title><?=$title?></title></head>
        <body><?php include 'content.php' ?></body>
    </html>
    In which case there's not really an easy solution, except to rewrite the logic so it's processed (and hold the data in a variable) before the <html>. Then within content.php, you would simply output the processed variable instead of doing the logic there. This is the essence of a template system.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,901
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Alternatively you could use output buffering. Very rough example:

    view.php
    PHP Code:
    <?php
    $title 
    'This is the HTML title!';
    echo 
    'Hello world!';
    template.php
    PHP Code:
    <html>
    <head>
      <title><?php echo $title?></title>
    </head>

    <body>
      <?php echo $content?>
    </body>
    index.php
    PHP Code:
    <?php
    ob_start
    ();
    include(
    'view.php');
    $content ob_get_clean();
    include(
    'template.php');
    Basically this will first execute the file view.php (which sets $title) and store the result in $content. The variable $content can then be used in the template.php.

    Again, the example is very rough but I hope you get the idea. If not I'd be happy to elaborate
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that's enough for me to go on for now. I will come back here if I need more help. Thanks.


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
  •