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>Code: Select all
$Template->Replace("main", "$main");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));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);
}
}
?>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();
?>Code: Select all
<html>
<head>
<title>{titel}</title>
</head>
<body>{main}</body>
</html>$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)