Php 模板引擎{var}不工作
我制作了一个模板系统,但是{var}没有输出值。 它只是输出{var} 这是我的模板类:Php 模板引擎{var}不工作,php,template-engine,Php,Template Engine,我制作了一个模板系统,但是{var}没有输出值。 它只是输出{var} 这是我的模板类: <?php class Template { public $assignedValues = array(); public $tpl; function __construct($_path = '') { if(!empty($_path)) { if(file_exists($_path))
<?php
class Template {
public $assignedValues = array();
public $tpl;
function __construct($_path = '')
{
if(!empty($_path))
{
if(file_exists($_path))
{
$this->tpl = file_get_contents($_path);
}
else
{
echo 'Error: No template found. (code 25)';
}
}
}
function assign($_searchString, $_replaceString)
{
if(!empty($_searchString))
{
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
function show()
{
if(count($this->assignedValues) > 0)
{
foreach ($this->assignedValues as $key => $value)
{
$this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
}
}
echo $this->tpl;
}
}
?>
下面是我对索引执行的操作:
<?php
require_once('inc/classes/class.template.php');
define('PATH', 'tpl');
//new object
$template = new Template(PATH.'/test.tpl.html');
//assign values
$template->assign('title', 'Yupa');
$template->assign('about', 'Hello!');
//show the page
$template->show();
?>
我真的需要一些帮助,如果你能帮助我,我将非常感激。而不是排队:
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
你应该:
$this->assignedValues[$_searchString] = $_replaceString;
它会起作用的
当然,我假设您的模板文件中包含以下内容:
{title}{about}
你应该改变
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
为此:
$this->assignedValues["{".$_searchString . "}"] = $_replaceString ;
这只会将您的关键字替换为值。请将您的代码发布在此处,而不是其他地方。哦,对不起。感谢您的编辑。可能您的问题是您的所有密钥都是strtoupper,而在您的模板中它们不是。。。?!我已经试着将其资本化,但没有成功。你只需要从代码中删除strtoupper,它就会成功的。谢谢!你帮了我很多。