Basic php template thing will not display correctly

Discussion in 'PHP' started by HellBomb, Sep 17, 2010.

  1. #1
    Ok, so far this is getting quite fustrating, I am trying to make a very very basic template system to take out some of the repetitive coding for my projects but this code is not working at all.

    Global.php
    
    <?php $header = include('templates/header.php'); ?>
    
    PHP:
    index.php
    <?php $header ?>
    PHP:
    header.php
    This is the header.
    PHP:
    What is output
    This is the header. 1
    Where is that one coming from, I have several other templates and every time I add them with the same procedure it will display the code in the wrong display order and always add an extra 1 at the end of the page.
     
    HellBomb, Sep 17, 2010 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    maybe
    
    <?php $header = file_get_contents('templates/header.php'); ?>
    
    PHP:
    Or AFAIK include can return values but you need to use return keyword inside of included files.
     
    AsHinE, Sep 17, 2010 IP
  3. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #3
    how the php statements in my template no longer works, any solution?
     
    HellBomb, Sep 17, 2010 IP
  4. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just put
    
    <?php include('templates/header.php'); ?>
    
    Code (markup):
    in index.php. You may need to adjust the path for "templates/header.php" though.

    In your case you can even just put
    
    <?php include('templates/header.php'); ?>
    
    Code (markup):
    in Global.php. Although that is not recommended though.
     
    imperialDirectory, Sep 17, 2010 IP
  5. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #5
    yes but I am working with a group of people who no nothing of web design but I am supposed to work with them for a school project. Instead of having them insert that php statement I would want to just make it so all they had to do was insert the $header instead of the $header = includes('templates/header.php');
     
    HellBomb, Sep 17, 2010 IP
  6. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if that's the case then you can use Ashine's code in Global.php
    
    <?php $header = file_get_contents('templates/header.php'); ?>
    
    Code (markup):
    Then use this in index.php
    
    <?php echo $header; ?>
    
    Code (markup):
    This works if header.php only contains text and without any PHP code.

    If you have php code inside header.php, then you can do something like this in Global.php
    
    <?php
    ob_start();
    include('templates/header.php');
    $header = ob_get_contents();
    ob_end_clean();
    ?>
    
    Code (markup):
    Hope that helps
     
    imperialDirectory, Sep 17, 2010 IP
  7. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Thank you, your 2nd solution was the exact thing i needed. :) Now just gotta figure out a more effective method to do this for about 50 different includes, or do I have to do this for each one?

    any way I can make this automatically do this for all files in a directory?
    
    <?php
    ob_start();
    include('templates/header.php');
    $header = ob_get_contents();
    ob_end_clean();
    ?>
    
    PHP:
     
    Last edited: Sep 17, 2010
    HellBomb, Sep 17, 2010 IP
  8. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yes you can. You can even do it if its in different directory. Just need to add a static path or a relative path to it. If all your files already included Global.php, then you only need to put <?php echo $header; ?> on each file.

    e.g. index.php
    
    <?php
    include('Global.php');
    echo $header;
    ?>
    
    Code (markup):
     
    imperialDirectory, Sep 18, 2010 IP
  9. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #9
    that doesn't do what i need it to as it displays the content not where i want it to.
     
    HellBomb, Sep 18, 2010 IP