我有從查詢的結果陣列中的控制器,比如,以查看:數據未解析使用模板呈現笨
[...]控制器/ admin.php的:
public function user_email()
{
$this->db->select('user_email');
$this->db->from('user');
$query = $this->db->get();
$data['email'] = $query->result_array();
$this->template->render('admin/user_driver', $data, 'admin');
}
文庫/的template.php:
class Template {
protected $ci;
public $data;
public function __construct()
{
$this->ci =& get_instance();
}
public function render($view, $viewdata = null, $template = 'frontend')
{
$html = $this->ci->load->view($view, $viewdata, TRUE);
$this->data['template'] = $this->parse_blocks($html);
$this->data['_viewdata'] = $viewdata;
$this->ci->load->view("template/{$template}", $this->data);
}
public function parse_blocks($html)
{
$blocks = array();
if(empty($html)) $html = " ";
libxml_disable_entity_loader(false);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_use_internal_errors(false);
libxml_disable_entity_loader(true);
foreach ($dom->getElementsByTagName('template') as $node) {
$section = $node -> getAttribute('block');
$blocks[$section] = $this->DOMinnerHTML($node);
}
return $blocks;
}
private function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
public function block_exists($blockname = NULL)
{
if(empty($blockname))
return false;
return (is_array($this->data) && is_array($this->data['template']) && array_key_exists($blockname,$this->data['template']));
}
}
我var_dump($email)
我user_driver.php
看法,但它拋出Undefined variable: email
。文件user_driver.php
在[...]views/admin/user_driver.php
之下。
我做錯了什麼?
哪個模板庫是你使用? [Phil Sturgeon's?](https://github.com/philsturgeon/codeigniter-template) – MackieeE
我不確定它是什麼,因爲我被要求修改別人的代碼,我添加了我的模板代碼,以便更清晰@ MackieeE – may
啊!謝謝你包括這一點,它已經明確 - 視圖數據正在重新分配=)'$ this-> data ['_ viewdata'] = $ viewdata;' – MackieeE