leuthrick
Gebruiker
- Lid geworden
- 17 sep 2008
- Berichten
- 454
hey,
ik heb deze class om tpl files uit te lezen:
class.tpl.php
class.gastenboek.php
index.php
ik krijg deze error:
Fatal error: Call to a member function newBlock() on a non-object in C:\xampp\htdocs\tpl\include\class\class.gastenboek.php on line 4
dat is deze regel:
alvast bedankt
Mvg leuthrick
ik heb deze class om tpl files uit te lezen:
class.tpl.php
PHP:
<?php
class TemplateParser {
var $file = 'start.tpl'; //Het standaard bestand, deze gebruikt hij als er geen bestand wordt opgegeven
var $errors = ''; //Hier komen de errors in
var $getfile = false; //Komt nog...
var $tpl = ''; //Komt nog...
var $blockcontent = array(); //Komt nog...
public function __construct($file = false) {
if($file != "" && $file != false) { //Kijken of $file niet leeg is.
if(!preg_match("#(.+?).tpl#si", $file)) { //Kijken of het bestand de exentie .tpl heeft
$this->errors .= "<b>TemplateParser Error:</b> Het bestand moet de exentie .tpl hebben!<br />";
}
if(!file_exists($file)) { //Kijken of het bestand bestaat
$this->errors .= "<b>TemplateParser Error:</b> het bestand ".$file." bestaat niet!<br />";
}
$this->file = $file;
}
}
public function getfile() {
if(file_exists($this->file)) { //Kijken of bestand bestaat
$this->tpl = file_get_contents($this->file); //Inhoud verkrijgen
$this->getfile = true; //Bestand is opgehaald
}
}
public function getBlock($blockname) {
$regex = "#\[start-block ".$blockname."\](.+?)\[end-block ".$blockname."\]#s";
preg_match($regex, $this->tpl, $matches); //Inhoud verkrijgen
return $matches[1]; //Inhoud returnen
}
public function newBlock($blockname, $content) {
$this->blockcontent[$blockname] .= $this->getBlock($blockname); //Block inhoud ophalen
foreach($content as $pattern=>$replacement){
$this->blockcontent[$blockname] = preg_replace("#\{".$pattern."\}#si", $replacement, $this->blockcontent[$blockname]); //Inhoud in block veranden
}
}
public function set($pattern, $replacement) {
if($this->getfile == false) { //Kijken of het bestand al gelezen is.
$this->getfile(); //Zo niet, bestand inlezen.
}
$this->tpl = preg_replace("#\{".$pattern."\}#si", $replacement, $this->tpl); //{iets} wordt veranderd in iets.
}
public function parse() {
if($this->errors == '') { //Kijken of de errors leeg zijn
if($this->getfile == false) { //Als het bestand nog niet gelezen is, laten lezen.
$this->getfile();
}
foreach($this->blockcontent as $blockname=>$block) {
$regex = "#\[start-block ".$blockname."\](.+?)\[end-block ".$blockname."\]#s";
$this->tpl = preg_replace($regex, $block, $this->tpl); //De inhoud aan de pagina toevoegen.
}
return $this->tpl; //De inhoud returnen
}else{
return $this->errors; //Als er errors zijn, deze returnen.
}
}
}
?>
class.gastenboek.php
PHP:
<?php
class gastenboek {
public function test(){
$template->newBlock("showpost", array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.'));
}
}
?>
index.php
PHP:
<?php
require("include/class/class.tpl.php");
require("include/class/class.gastenboek.php");
include("include/settings.php");
$template = new TemplateParser("template/standaard/index.tpl"); //Bestand index.tpl verkrijgen
$template->set("titel", "Titel van de pagina"); //titel veranderen
$template->set("tekst", "Tekst van de pagina"); //tekst veranderen
$gastenboek = new gastenboek();
$gastenboek->test();
echo $template->parse();
?>
ik krijg deze error:
Fatal error: Call to a member function newBlock() on a non-object in C:\xampp\htdocs\tpl\include\class\class.gastenboek.php on line 4
dat is deze regel:
PHP:
$template->newBlock("showpost", array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.'));
alvast bedankt
Mvg leuthrick