Page 1 of 1

replace file_get_contents()

Posted: Sun Apr 29, 2007 1:54 pm
by mullerrwd
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: Select all

<html>
<head>
<title>{titel}</title>
</head>
<body>{main}</body>
</html>
now i am using this:

Code: Select all

$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?

Code: Select all

$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

Code: Select all

<?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

Code: Select all

<?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: Select all

<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.
Thanks in advance
robert ( the noob who just started php=>4months)

Posted: Mon Apr 30, 2007 9:02 am
by mullerrwd
I also found that on curl but it doesn't seem to work either?

Code: Select all

<?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