SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php template system

    I am having trouble with my php template system.

    I have a file containing all my html that is repeated(header and layout etc). I include this file in all my html pages so I don't have to repeat all the code in every page.

    ***html file***
    Code:
    <?php $title = "Tango Monkeys"; ?>
    <?php
    include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/includes/file.php';
    ?>
    ***file.php***
    Code:
    <?php if(isset($title)) { echo $title; } else { echo "Death rabbit bear"; } ?>
    It works fine until I want to have different html tags in my variable.
    Code:
    <?php $title = " 
    
    Tango Monkeys
    firedragon
    <h1>applesauce</h1>
    
    "; ?>
    Above works fine, but if I put any divs inside that variable I get a parse error.
    Obviously this is a problem because my html pages need html tags.

    I am using variables in file.php, because if cannot use a function to get file contents because each webpage is in a different place and I only want a section of content from the current file anyway.

    Before I was using multiple includes such as:
    include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/includes/header1.php';
    include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/includes/body1.php';
    include $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . '/includes/footer.php';
    And then inserting my html content between body and footer.
    This worked fine, but as you can see I am using multiple includes which scatters my repeated content accross different include files which is messy.

    So I am working on having only 1 include file called upon by each webpage and in this include file, having variables for different content and in the webpage are the content for the variables.

    I also notice that I have to declare the variables before I include the php file or it does not work. So I would have a list of html content assigned to php variables at the top, and then import the php file which has the page structure and calls upon the variables at the right places in the template(repeated) html content in that file.

    As I said though, I am having trouble with assigning long html content with html tags in the variables.

  2. #2
    SitePoint Guru cyjetsu's Avatar
    Join Date
    May 2008
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fixed it with the magical EOF function

    $title = <<< EOF

    <div id="apple"></div>
    Tango Monkeys
    firedragon
    <h1>applesauce</h1>

    EOF;

    Took me a while though, I kept getting syntax error until I removed the space after EOF; on the same line.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can learn more about proper string syntax in php here
    http://www.php.net/manual/en/language.types.string.php


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
  •