2

我目前正在学习 CodeIgniter 并将其用于一个小项目。我想制作一个模板,这样我就不需要为视图编写重复的代码。我喜欢 jruzafa 在这篇文章中的回答:如何处理 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> 

$body 是视图联系人。

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

谢谢。

4

1 回答 1

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> 
于 2011-11-01T00:55:39.387 回答