如何从配置文件中获取$var以显示在模板中

How can I get a $var from a config file to display in my template?

本文关键字:显示 var 配置文件 获取      更新时间:2023-09-26

我正在尝试从配置文件中提取变量以显示在模板中。以下是我的文件相关..

索引.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);
?>

模板.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;
    }
}


?>

索引.tpL.php

<?php include '../includes/config.php'; ?>

{标题}

<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>

配置.php

<?php
// Script Info
$script     = 'BASE';
$version    = '0.0.1 Dev.';
?>

我已经用谷歌搜索过它,但我似乎无法搜索正确的术语。我知道它就在那里,但我只需要找到它。对不起,提前感谢!

[已解决]

我在模板中创建了一个函数.class.php

function added (){
        echo $script;

    }

在索引中.php我是这样称呼的。

$template->assign('content', ''. $script.' TEST');

感谢维杰·维尔马