我想了解这种编码是如何工作的[php]
这是用于显示GUI的tpl文件。我都贴了。这段代码如何读取tpl文件并显示HTML显示。哪个命令正在执行此操作我想了解这种编码是如何工作的[php],php,templates,Php,Templates,这是用于显示GUI的tpl文件。我都贴了。这段代码如何读取tpl文件并显示HTML显示。哪个命令正在执行此操作 class Template { /** * Config * * @access private */ private static $config; /** * Path templates * * @access private */ private $tpl_path = null; /** * Values * * @access priv
class Template {
/**
* Config
*
* @access private
*/
private static $config;
/**
* Path templates
*
* @access private
*/
private $tpl_path = null;
/**
* Values
*
* @access private
*/
private $values = array();
/**
* Constructor
*
* @access public
*/
public function __construct($tpl_path) {
self::$config = config_load('template');
$this->tpl_path = $tpl_path;
}
/**
* Set a template variable
*
* @access public
*/
public function set($name, $value = null) {
if(is_array($name)) {
foreach ($name as $key => $value) {
$this->values[$key] = $value;
}
} else {
$this->values[$name] = $value;
}
}
/**
* Display the template file
*
* @access public
*/
public function display($template) {
if ($this->values) {
extract($this->values);
}
if (file_exists($this->tpl_path . $template . self::$config['template_extension'])) {
ob_start();
include($this->tpl_path . $template . self::$config['template_extension']);
$output = ob_get_contents();
ob_end_clean();
} else {
die('Template file '. $this->tpl_path . $template . self::$config['template_extension'] . ' not found.');
}
if ($output) echo $output;
}
}
?>
我有一个使用这个类的文件,代码是
$tpl->set('customer_details', $customer_details);
$tpl->set('customer_addresses', $customer_addresses);
$tpl->set('countries', $countries);
//Display the template
$tpl->display('edit_account');
上述代码使用tpl和设置值。但是如何设置和显示值非常简单。类代码使用php。模板可以像任何php文件一样自由混合php和HTML,并且变量将被插入。该类使用输出缓冲存储输出,然后使用echo返回输出。类中使用的变量通常使用set方法提供,该方法将它们全部放入名为“values”的类数组变量中,该变量无疑在模板文件中引用。然后,您应该重新研究任何一行您不理解的代码提示:
include
是对模板文件的访问,echo是输出。如果($this->values){extract($this->values);}这些行在做什么David…一切都从这里开始,记住你在php手册中自愿支持uderstand extract。我知道这是在做什么。但我想知道发生了什么。self::$config=config_load('template')///→他的代码正在做什么配置加载('TEMPLATE'))我知道基本知识,但无法理解程序的流程。self::$config行正在使用名为config_load的函数设置静态类变量的值。您没有提供所有代码,但包含了此函数,以便在类中全局可用。这可能在涉及的任何引导代码中都有。不是很好的设计或实践,但这就是它的工作原理。稍后只需找到函数config_load(…)的声明,该变量用于查找特定的文件扩展名字符串(self:$config['template_extension'])。