我正在尝试从配置文件中提取变量以显示在模板中。以下是我的相关文件..
的index.php
<?php
//Setting up important stuff
require_once('class/template.class.php');
define('INCLUDES_PATH', 'includes');
require_once(INCLUDES_PATH.'/config.php');
require_once(INCLUDES_PATH.'/css.php');
require_once(INCLUDES_PATH.'/js.php');
define('TEMPLATES_PATH', 'templates');
define('PARTIALS_PATH', TEMPLATES_PATH.'/partials');
//Instanciate new object
$template = new Template(TEMPLATES_PATH.'/index.tpl.php');
//Assign values
$template->assign('title', 'Home');
$template->assign('content', '<?php echo $script; ?> TEST');
$template->assign('footer', 'This is my Footer');
//Use a Partial
$template->renderPartial('table_here', PARTIALS_PATH.'/table.part.html', array('username' => 'Blood_Wolf89', 'age' => 25));
//Showing content
$template->show(true);
?>
template.class.php
<?php
class Template {
private $assignedValues = array();
private $partialBuffer = '';
private $tpl = '';
function __construct($_path = ''){
if(!empty($_path)){
if(file_exists($_path)){
$this->tpl = file_get_contents($_path);
}else{
echo "<b>Template Error:</b> Base File Inclusion Error. <b>File: '$_path'</b><br />";
}
}
}
function assign($_searchString, $_replaceString = ''){
if(!empty($_searchString)){
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
function renderPartial($_searchString, $_path, $_assignedValues = array()){
if(!empty($_searchString)){
if(file_exists($_path)){
$this->partialBuffer = file_get_contents($_path);
if(count($_assignedValues) > 0){
foreach ($_assignedValues as $key => $value){
$this->partialBuffer = str_replace('{'.strtoupper($key).'}', $value, $this->partialBuffer);
}
}
$this->tpl = str_replace('['.strtoupper($_searchString).']', $this->partialBuffer, $this->tpl);
$this->partialBuffer = '';
}else{
echo "<b>Template Error:</b> Partial Inclusion Error. <b>File: '$_path'</b><br />";
}
}
}
function show($debug = false){
if(count($this->assignedValues) > 0){
foreach ($this->assignedValues as $key => $value){
$this->tpl = str_ireplace('{'.$key.'}', $value, $this->tpl);
}
}
if($debug){
$this->tpl .= '<!--'.date('d-m-Y H:i:s').'-->';
}
echo $this->tpl;
}
}
?>
index.tpl.php
<?php include '../includes/config.php'; ?>
{TITLE}
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- START NAV -->
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">FAQ</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>
<!-- END NAV -->
<div class="container">
<h2>{CONTENT}</h2>
</div>
<footer class="footer">
<div class="container">
<br />
<p class="text-muted">{FOOTER}</p>
</div>
</footer>
的config.php
<?php
// Script Info
$script = 'BASE';
$version = '0.0.1 Dev.';
?>
我用谷歌搜索它,我似乎无法搜索正确的术语。我知道它在那里,但我只需要找到它。对不起,谢谢!
答案 0 :(得分:0)
[解决]
我在template.class.php中创建了一个函数
function added (){
echo $script;
}
在index.php中,我这样称呼它。
$template->assign('content', ''. $script.' TEST');
感谢Vijay Verma