2011-11-01 51 views
2

我目前正在学习CodeIgniter并将其用于一个小型项目。我想制作一个模板,以便我不需要为视图编写重复的代码。我喜欢jruzafa在这个帖子答案:How to Deal With Codeigniter Templates?将视图作为字符串传递时,codeIgniter表单验证不起作用

在控制器:

//Charge the view inside array 
    $data['body'] = $this->load->view('pages/contact', '', true); 


    //charge the view "contact" in the other view template 
    $this->load->view('template', $data); 

考虑的template.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es"> 
<head> 
    <title>Template codeigniter</title> 
</head> 
<body> 
    <div> 
     <?=$body?> 
    </div> 
    <div class="clear"></div> 
    <div>Footer</div> 
    </div> 
</body> 
</html> 

$身体是查看联系人。

但现在我面临一个问题。如果我将form_view作为字符串传递给$ data ['body'],表单验证不起作用。有什么办法可以解决它吗?

谢谢。

+0

你能后的验证设置和接触的视图页。我正在研究一个具有如此初始化的边栏的项目,这些错误工作正常。 –

回答

3

尝试加载模板本身内的正文内容。这应该让你与你的输出更多的选择性:

在控制器:

// Set the path to body content inside the $data array 
$data['path_to_body'] = 'pages/contact'; 
$this->load->view('template', $data); 

考虑的template.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es"> 
<head> 
    <title>Template codeigniter</title> 
</head> 
<body> 
    <div> 
     <? $this->load->view($path_to_body) ?> 
    </div> 
    <div class="clear"></div> 
    <div>Footer</div> 
    </div> 
</body> 
</html> 
+0

谢谢。有用。 – Jason