SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2000
    Location
    Rhode Island, USA
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know of a site that might cover how to make a template for site in PHP? I'm looking for something simular to what www.php.net has when you view php "source." I have the basic knowlege of PHP templates, but was wondering if there is something more complex.

    Thanks for your time,
    Alex.

  2. #2
    exit();
    Join Date
    Jul 2000
    Posts
    759
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I don't fully understand what you mean, but you might be interested in using something called FastTemplate which is a feature in PHP I beleive. Check out php.net for more information or the book "Professional PHP Programming" by wrox explains about it a bit.

    ------------------
    ICQ Me! (73902680)

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The way SitePoint does this is as follows. Forgive the simplicity of the design, but you should get the idea. :-)

    First, we create a file called pageLayout.php. This is our template, and is responsible for the page layout that is common to all pages of our site:

    <!-- pageLayout.php -->
    <HTML>
    <HEAD>
    ...
    </HEAD>
    <BODY>
    <H1> SITE TITLE </H1>
    <?php echo($content); ?>
    </BODY>
    </HTML>


    Notice we have echoed a variable named $content in the above code. This variable should contain the content that we want to display on the page. But how does it get its value?

    Well let's consider a page of our site. Call it samplePage.php. In this file, we will define the content to be displayed, then use pageLayout.php to display it:

    <!-- samplePage.php -->
    <?
    $content = '
    <P>This is my site. It\'s built with PHP to make my life easier, but you don\'t have to know that!</P>
    <HR>
    <P><A HREF="mailto:me@myself.com">eMail me!</A></P>
    ';

    include("/path/to/pageLayout.php");
    ?>


    Notice that all apostrophes (') in the content need to be escaped with backslashes (\') because the content is actually one big string, the beginning and end of which are marked by apostrophes.

    The include() function tells PHP to insert the contents of pageLayout.php into the file. This slaps in our page layout, which uses our $content variable to fill the page.

    Using multiple variables, you can build very flexible templates for your site.



    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

  4. #4
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent post Kevin!

    I didn't really understand how to use templates in php, but now I understand perfectly.

    Chris roane

    ------------------
    Web Services: DevStorm | Intersurge
    Web Tools: Web Site Resource | Free Web Site Tools


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
  •