Класс шаблона не может работать

Я пытался исправить свой класс шаблона, но он не работает,

что не сработало: [@title] не заменяется.

Код:

protected $file;
protected $values=array();

public function __construct($file){
$this->file = $file;
}

public function set($key, $value){
$this->values[$key] = $value;
}

public function output() {
if (!file_exists($this->file)) {
return "Pagina kan niet gevonden worden.";
}
$output = file_get_contents($this->file);

foreach ($this->values as $key => $value) {
$tagToReplace = "[@$key]";
$output = str_replace($tagToReplace, $value, $output);
}
return $output;
}

Надеюсь, кто-нибудь может мне помочь, заранее спасибо.

0

Решение

$ — начальный символ для переменной php.
Просто убежать от этого.

$tagToReplace = "[@\$key]";
$output = str_replace($tagToReplace, $value, $output);

//редактировать
Я проверил код. В моей тестовой среде все работает нормально.

$test = new Tpl(__DIR__."/test.txt");
$test->set("test", "huhu123");
$test->set("asdf", "xcv");
var_dump($test->output());

TestFile:

kjasdfgkashf [@test]

Выход:

string(20) "kjasdfgkashf huhu123"
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector