SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    template engine problem

    hello everyone,
    I am currently building a simple template engine(parser) not one that is big and a lot of installing as smarty or pat.
    I have everything done, but have the following problem:
    I have strings in my template.tpl like this:
    Code:
    HTML Code:
    <html>
    <head>
    <title>{titel}</title>
    </head>
    <body>{main}</body>
    </html>


    now i am using this:
    php:
    $Template->Replace("main", "$main");
    to replace {something} by something ($something), but i want to include documents from the file inc/ so I think I use include. It works thats a fact but the script pastes a number at the and, say $main = "blabla"
    output is then blabla 1, then someone told me I should use file_get_contents() or Curl(). But i am still a noob in php just started and how would I build this into the parser? Would it be like this?
    php:
    $Template->Replace("main", file_get_contents(inc/main.php));
    ? If I do so I dont even see "blabla" in the output it is just blanc.
    this is the full script:
    ---classes/parser.php
    php:
    PHP Code:

    <?php
    class Template {
        var 
    $page "";
        var 
    $load false;
       
        function 
    LoadTemplate($url){
            
    $this->page "";
            if(
    file_exists($url)){
                
    $this->page file_get_contents($url);
                
    $this->load true;
            }else{
                
    $this->load false;
                
    $this->page "Couldn't load the template file!";
            }
        }
       
        function 
    Replace($var$what){
            if(
    $this->load){
                
    $var '{' $var '}';
                
    $this->page str_replace($var$what$this->page);
            }
        }
       
        function 
    PrintTemplate(){
            echo(
    $this->page);
        }
    }
    ?>
    ---index.php
    php:
    PHP Code:
    <?php
    include("classes/parser.php");
    $file "tpl/index.tpl";
    $titel "Voorbeeld";
    $main file_get_contents("inc/main.php")
    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel"$titel);
    $Template->Replace("main"$main);
    $Template->PrintTemplate();
    ?>
    ----tpl/index.tpl
    Code:
    HTML Code:
    <html>
    <head>
    <title>{titel}</title>
    </head>
    <body>{main}</body>
    </html>

    as you see I tried to use the file_get content but i diddn't work, but i like to keep script small & simpel, thats what php all about isn't? That doesnt work, but I also don't want 3 apart line but all together:
    $Template->Replace("main", function(to add file)); see what I mean,
    I can't fugure out what I am doing wrong or how I hould do this but this is my first php project to learn from. So if you know how it should be please explain why it is done so.
    I also found that on curl but it doesn't seem to work either?
    php:
    <?php
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, 'http://example.com');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);

    // display file
    echo $file_contents;
    ?>

    I keep getting a blank page

    And with include I keep getting the number "1"at the end of the function?

    So my question is how should/can I replace {something} by a file called for example (/inc/)something.php instead of $something

    Thanks in advance
    robert

  2. #2
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to check out 'inc/main.php'. I tried your example with a very simple file as 'inc/main.php' and it worked.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What way did you use? incude() file_get_content() curl() or require()?

  4. #4
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file_get_contents()

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    include("classes/parser.php");
    $file = "tpl/index.tpl";
    $titel = "Voorbeeld";
    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel", $titel);
    $Template->Replace("main", file_get_contents(inc/main.php));
    $Template->PrintTemplate();
    ?>
    Like this?

  6. #6
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I took the example from the first post

    PHP Code:
    <?php
    include("classes/parser.php");
    $file "tpl/index.tpl";
    $titel "Voorbeeld";
    $main file_get_contents("inc/main.php")
    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel"$titel);
    $Template->Replace("main"$main);
    $Template->PrintTemplate();
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope doesn't work. get blank: test.rwdweb.nl

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    index.php
    PHP Code:
    <?php
    /**
     * @author mullerrwd
     * @copyright 2007
     * @website    rwdweb.nl
     */
    include("classes/parser.php");
    $file "tpl/index.tpl";
    $titel "Voorbeeld";
    $main file_get_contents("inc/main.php")
    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel"$titel);
    $Template->Replace("main"$main);
    $Template->PrintTemplate();
    ?>
    index.php
    HTML Code:
    <!-- 
     * @author mullerrwd
     * @copyright 2007
     * @website	rwdweb.nl
    -->
    <html>
    <head>
    <title>{titel}</title>
    </head>
    <body>{main}</body>
    </html>
    classes/parser.php
    PHP Code:
    <?php
    /**
     * @author mullerrwd
     * @copyright 2007
     * @website    rwdweb.nl
     */
    class Template {
        var 
    $page "";
        var 
    $load false;
       
        function 
    LoadTemplate($url){
            
    $this->page "";
            if(
    file_exists($url)){
                
    $this->page file_get_contents($url);
                
    $this->load true;
            }else{
                
    $this->load false;
                
    $this->page "Couldn't load the template file!";
            }
        }
       
        function 
    Replace($var$what){
            if(
    $this->load){
                
    $var '{' $var '}';
                
    $this->page str_replace($var$what$this->page);
            }
        }
       
        function 
    PrintTemplate(){
            echo(
    $this->page);
        }
    }
    ?>
    inc/main.php
    PHP Code:
    <?php
    /**
     * @author mullerrwd
     * @copyright 2007
     * @website    rwdweb.nl
     */
    echo "hahah";

    ?>
    Do you see something wrong here?

  9. #9
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mullerrwd View Post
    inc/main.php
    PHP Code:
    <?php
    /**
     * @author mullerrwd
     * @copyright 2007
     * @website    rwdweb.nl
     */
    echo "hahah";

    ?>
    Do you see something wrong here?
    OK, 'file_get_contents' will just grab the actual code rather than executing it. What you'll need here is output buffering.

    index.php
    PHP Code:
    <?php
    include("classes/parser.php");
    $file "tpl/index.tpl";
    $titel "Voorbeeld";
    ob_start();
    include(
    "inc/main.php");
    $main ob_get_clean();
    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel"$titel);
    $Template->Replace("main"$main);
    $Template->PrintTemplate();
    ?>

  10. #10
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, but everything else except 'inc/main.php' has been verified to work in my php5 test environment. That suggests the problem probably lies in 'inc/main.php' rather than in your template logic. I'll show you what I put in 'main.php' (which seemed to work):

    PHP Code:
    <p>This is a generic paragraph.</p

  11. #11
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, I see you posted 'main.php' while I was typing. Please disregard my last request.

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what is my problem?

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aah ok, gonna test it.
    And if I want to add more functions it would be like this:
    PHP Code:
    <?php
    include("classes/parser.php");
    $file "tpl/index.tpl";
    $titel "Voorbeeld";
    ob_start();
    include(
    "inc/main.php");
    $main ob_get_clean();
    $main file_get_contents("inc/main.php");
    $thing1 ob_get_clean();
    $thing1 file_get_contents("inc/thing1.php");
    $thing2 ob_get_clean();
    $thing2 file_get_contents("inc/thing2.php");
    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel"$titel);
    $Template->Replace("main"$main);
    $Template->Replace("thing1"$thing1);
    $Template->Replace("thing2"$thing2);
    $Template->PrintTemplate();
    ?>
    but like this i will get an huge ammount of code. I've looked on php.net but thay don't have any example on how to use 1 command ob_get_clean for more strings?

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By the way finally it works:P tnks for that. you learned me more.I owe you:P

  15. #15
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad to be of help. I haven't looked at output buffering in a while so it was good to go over it again.

    Regarding adding more includes think of it this way:
    PHP Code:
    <?php
    include("classes/parser.php");
    $file "tpl/index.tpl";
    $titel "Voorbeeld";

    // Grab the buffer from 'main.php'
    ob_start();
    include(
    "inc/main.php");
    $main ob_get_clean();

    // Grab the buffer from 'thing1.php'
    ob_start();
    include(
    "inc/thing1.php");
    $thing1 ob_get_clean();

    // Grab the buffer from 'thing2.php'
    ob_start();
    include(
    "inc/thing2.php");
    $thing2 ob_get_clean();

    $Template = new Template;
    $Template->LoadTemplate($file);
    $Template->Replace("titel"$titel);
    $Template->Replace("main"$main);
    $Template->Replace("thing1"$thing1);
    $Template->Replace("thing2"$thing2);
    $Template->PrintTemplate();
    ?>
    Last edited by Trent Reimer; May 1, 2007 at 12:46.

  16. #16
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you would like to learn more on templating you can download my framework, Kayzio Framework, and look at my code. It is a little more advanced than what you have but not as advanced as Smarty. I feel it is also a little easier to read as it is tabbed out and organized. Sorry there is no online documentation but I plan to have it up by this weekend (maybe even tonight).
    Kayzio - We don't hesitate, we accelerate.

  17. #17
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha ok good, I am alot further. thnx very much.


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
  •