首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想了解这个编码是如何工作的[php]

问我想了解这个编码是如何工作的[php]
EN

Stack Overflow用户
提问于 2017-01-28 03:09:04
回答 1查看 38关注 0票数 0

这是tpl文件中用来显示GUI的东西。我都贴出来了。此代码如何读取tpl文件并显示HTML显示。执行此操作的是哪个命令。

代码语言:javascript
复制
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;

} 

     }

      ?>

我有一个使用这个类的文件,代码是

代码语言:javascript
复制
     $tpl->set('customer_details', $customer_details);
     $tpl->set('customer_addresses', $customer_addresses);
     $tpl->set('countries', $countries);

      //Display the template
       $tpl->display('edit_account');

上面的代码使用了tpl和设置值。而是如何设置和显示这些值

EN

回答 1

Stack Overflow用户

发布于 2017-01-28 03:21:52

这很简单。类代码使用php include。这些模板可以像任何php文件一样自由地混合php和HTML,并且可以插入变量。该类使用输出缓冲来存储输出,然后使用echo返回输出。类中使用的变量通常使用set方法提供,该方法将所有变量放入名为“values”的类数组变量中,该变量在模板文件中无疑会被引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41901302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档