2013-07-23 49 views
0

我使用的是CodeIgniter。我想在其他视图中加载视图。我怎样才能做到这一点?CodeIngiter:在另一个视图中加载视图

例子:

比方说,我有一个 “视图” 称为 “CommentWall”。在CommentWall中,我想要一些“评论”视图。我在我的网站上使用“评论”的观点!

我该怎么做?看来CodeIgniter只允许我按顺序加载视图,考虑到我在其他视图中使用可重用视图INSIDE,这有点奇怪!

我可以在我的评论墙内看到吗?还是有其他方式可以可重复使用意见里面一个视图?

+0

你可以做到这一点,什么是你遇到的问题? – Matthew

+1

在控制器中做这些事情会更好,这可能有助于:http://stackoverflow.com/questions/17317115/is-it-ok-to-put-conditional-logic-on-codeigniter-views/17317621#17317621 –

回答

1

您可以从控制器很容易做到这一点,只需加载主视图,例如CommentWall

$this->load->view('CommentWall'); 

要添加子视图CommentWall视图中,您可以将您的CommentWall视图内以下行

$this->view('Comment'); 

例如,如果您从您的控制器加载CommentWall这样的视图

$data['comments'][] = 'Comment one'; 
$data['comments'][] = 'Comment two'; 

// load the parrent view 
$this->load->view('CommentWall', $data); 
CommentWall(父视图)

现在,如果你把这个

foreach ($comments as $comment) { 
    $this->view('Comment', array('comment' => $comment)); 
} 

而在你Comment(子视图),如果你有这样的

echo $comment . '<br />'; 

那么你应该得到的输出类似此

Comment one 

Comment two 

更新: Alos,check this answer

0

尝试

class Main extends CI_Controller { 

    function __construct() 
    { 
     parent::__construct(); 

     $data->comments =$this->load->view('comment'); 
      $this->load->vars($data); 
    } 

,并在每个视图尝试

echo $comments; 
0

只需加载“注释”争夺,在控制器串并把它传递给“CommentWall”的看法。

你可以这样说:

//Controller: 

public function load_comment_wall($param) { 

     $comments_view = ""; //String that holds comment views 

     //here load the comments for this wall as follows: 
     //assuming $comment_ids is array of id's of comment to be put in this wall... 
     foreach($comment_ids as $comment_id) { 
      $temp = $this->load->view("comment",array('comment_id'=>$comment_id),TRUE);  //Setting last parameter to TRUE will returns the view as String 
      $comments_view = $comment_views.$temp; 
     } 

     $data['comments'] = $comments_view; 

     //load comment wall 
     $this->load->view('comment_wall',$data); 
} 

//在评论壁上观,添加以下行

echo $comments; 
相关问题