Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Templates and PHP

    I'm about start developing a new site using templates. I have no experience using templates so after searching the forum and reading about it I think i got the general idea. I made this simple example to illustrate how I think using templates might look like:

    index.php

    <?php

    class Collect_Data
    {

    var $title;
    var $content;

    function Collect_Data()
    {
    $this->title = 'Template Test';
    $this->content = 'Here is content';
    $this->Print_Template();
    }

    function Print_Template()
    {include('index.tpl');}

    function Print_Title()
    {echo $this->title;}

    function Print_Content()
    {echo $this->content;}

    }

    new Collect_Data();

    ?>

    index.tpl

    <html>
    <head>
    <title><?php $this->Print_Title(); ?></title>
    </head>
    <body>
    <h1><?php $this->Print_Content(); ?></h1>
    </body>
    </html>

    This would allow me to change the appearance of the site without changing my php code. Now I wonder if I've gotten this right!? Is this how templates should be used?

    Any thoughts are appreciated

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Not quite.

    In your template file what you want to do is not have to use PHP or any other programming language except for either just HTML/CSS etc.

    What you do is put markers into the page. Something like this:

    PHP Code:

    <html>
    <
    head>
    <
    title>{pagetitle}</title>
    </
    head>
    <
    body>
    {
    content}
    </
    body>
    </
    html
    Then when you need to use that template you use PHP to open and parse that file and replace the markers with whatever they are supposed to be replaced with and then send it to the browser.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I've come up with this. What you think about it? Any drawbacks with it??

    index.php

    <?php
    //This function fills the template with data
    function getTemplate($tpl_name, $replaces)
    {
    $tpl = @implode('', @file('templates/'.$tpl_name));

    foreach($replaces as $search => $replace)
    {
    $tpl = str_replace('((('.$search.')))', $replace, $tpl);
    }

    return $tpl;
    }

    //Initializing body template
    $body_content = array();
    $body_content['headline_body'] = 'My template engine';
    $body_content['content_body'] = 'Works like a charm';
    $body = getTemplate('body.tpl', $body_content);


    //Initializing main template
    $index_content = array();
    $index_content['title_index'] = 'My template engine';
    $index_content['body_index'] = $body;
    $index = getTemplate('index.tpl', $index_content);

    echo $index;

    ?>

    index.tpl

    <html>
    <head>
    <title>(((title_index)))</title>
    </head>
    <body>
    (((body_index)))
    </body>
    </html>


    body.tpl

    <h1>(((headline_body)))</h1>
    <br><br>(((content_body)))<br><br>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •