Good template system but small help need

Discussion in 'PHP' started by KingCobra, Mar 30, 2009.

  1. #1
    The Class (template.class.php)

    <?
    class Template {
    public $template;
    function load($filepath) {
    $this->template = file_get_contents($filepath);
    }
    function replace($var, $content) {
    $this->template = str_replace("#$var#", $content, $this->template);
    }
    function publish() {
    eval("?>".$this->template."<?");
    }
    }
    ?>

    The Template File (design.html)

    <html>
    <head>
    <title>#title#</title>
    </head>
    <body>
    <h3>Hello #name#!</h3>
    <p>The time is: #datetime#</p>
    <? echo "<p>Embedded PHP works too!</p>"; ?>
    </body>
    </html>

    Usage (index.php)

    <?
    include "template.class.php";
    $template = new Template;
    $template->load("design.html");
    $template->replace("title", "My Template Class");
    $template->replace("name", "William");
    $template->replace("datetime", date("m/d/y"));
    $template->publish();
    ?>

    Output code of index.php

    <html>
    <head>
    <title>My Template Class</title>
    </head>
    <body>
    <h3>Hello William!</h3>
    <p>The time is: 03/10/04</p>
    <p>Embedded PHP works too!</p>
    </body>
    </html>

    =====================================

    This template system works ok. But I need few extra feature.

    Suppose, I add a variabe with no value to index.php
    $news = "";
    then add the line to same file
    $template->replace("news", $news);
    Then add following lines to design.html
    #if news#
    #news#
    #endif news#

    (Like 4images template system)

    As a result I want nothing will display for #news# . Cause $news is blank. How it possible?
    (note: now for me its display #if news# #endif news#

    Please help me to solve this problem
    And also is this template system ok?
    And anything if required.
     
    KingCobra, Mar 30, 2009 IP
  2. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    Please help me

    Its really urgent.
     
    KingCobra, Mar 30, 2009 IP
  3. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you need to write code to interpret it as php code...I wrote a template class once, resembled smarty actually...You should use smarty...if possible. otherwise you need to write code-blocks.

    preg_replace the #if (.*)#

    Hit me up on messenger...i'll send the template class i wrote.
     
    NatalicWolf, Mar 30, 2009 IP
  4. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    please help me
     
    KingCobra, Apr 2, 2009 IP
  5. ngcoders

    ngcoders Active Member

    Messages:
    206
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Please look into a templating system called btemplate , that should help you add the basic constructs like Looping , conditional statements etc in your template system.

    Btemplate is a very simple system i had used long time back , look through its code to see the implementation of the construct required.
     
    ngcoders, Apr 2, 2009 IP
  6. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    in index.php
    here load only one 1 template: design.html
    but I want to load 2 or more html template with the main template

    $template->load("design.html");

    how it possible?

    -----------------------------------------------------
    in index.php
    can i take following lines in a array. if yes, HOW?

    $template->load("design.html");
    $template->replace("title", "My Template Class");
    $template->replace("name", "William");
    $template->replace("datetime", date("m/d/y"));

    suppose,
    $template->replace((
    "title", "My Template Class",
    "name", "William"));
    or something like this.
     
    KingCobra, Apr 7, 2009 IP
  7. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Help me please
     
    KingCobra, Apr 8, 2009 IP