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:
---index.phpPHP 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);
}
}
?>
php:
----tpl/index.tplPHP 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();
?>
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





Bookmarks