我正在尝试在CodeIgniter中为我的应用程序开发一个小部件系统。我有一个模板列的数据库表,我循环。从该循环中,然后我遍历该列的小部件表。一旦加载了窗口小部件的数据库信息,我就需要确定为当前窗口小部件加载了哪个文件,并将其添加为窗口小部件的主体。加载的视图文件存储在小部件数组中的body变量中。
问题是在加载主视图文件之前显示窗口小部件内容。窗口小部件的内容需要位于主视图文件中。我做错了什么?
我的控制器:
$widget_types = $this->widget->type_array();
foreach($columns as $column){
$column_results[$column['column_id']] = $column;
$widgets = $this->widget->retrieve($column['column_id']);
foreach($widgets as $widget){
$widgets[$widget['id']]['body'] = $this->load->view('widgets/'.$widget_types[$widget['type']]['view'], array('widget' => $widget));
}
$column_results[$column['column_id']]['widgets'] = $widgets;
}
$data['column_results'] = $column_results;
$this->load->view('index_body', $data);
小部件模型:
<?php
class Widget extends MY_Model {
var $info = array();
var $error = NULL;
function __construct(){
parent::__construct();
$this->error = NULL;
}
function get_info($widget_id){
$this->db->from('template_widgets')->where('widget_id', $widget_id)->limit(1);
if($query = $this->db->get()){
if($query->num_rows() > 0){
$widget = $query->row_array();
$info = array(
'id' => $widget['widget_id'],
'title' => $widget['title'],
'column_id' => $widget['column_id'],
'position' => $widget['position'],
'options' => unserialize($widget['options']),
'type' => $widget['type']
);
$this->info = $info;
}
}
}
function retrieve($column_id){
$widget_results = array();
$this->db->from('template_widgets')->where('column_id', $column_id)->order_by('position', 'asc');
if($query = $this->db->get()){
if($query->num_rows() > 0){
foreach($query->result_array() as $widget){
if(empty($widget_results[$widget['widget_id']])){
$this->get_info($widget['widget_id']);
if($this->error == NULL){
$widget_results[$widget['widget_id']] = $this->info;
}
}
}
}
}
return $widget_results;
}
function type_array($type = ''){
$widget_types = array(
0 => array(
'name' => 'Category Feed',
'view' => 'category_feed'
),
1 => array(
'name' => 'Members Feed',
'view' => 'members_feed'
),
2 => array(
'name' => 'Events Feed',
'view' => 'events_feed'
),
3 => array(
'name' => 'Category List',
'view' => 'category_list'
),
4 => array(
'name' => 'Testimonials',
'view' => 'Testimonials'
),
5 => array(
'name' => 'Events Calendar',
'view' => 'events_calendar'
),
6 => array(
'name' => 'Content',
'view' => 'content'
),
7 => array(
'name' => 'Statistics Feed',
'view' => 'statistics_feed'
)
);
return ($type == '') ? $widget_types : $widget_types[(int) $type];
}
}
小部件视图文件的示例:
<?php
$str = '';
$CI =& get_instance();
$CI->load->model('member');
$members = $CI->member->result_array($widget['options']['max_results'], $widget['options']['order_column']);
foreach($members as $member){
if($widget['options']['display_method'] == '1'){
$str .=
'<div class="col-sm-6 col-md-4">
<div class="thumbnail">
'.img(array('src' => attachment_url($member['avatar']['file_name']), 'width' => '192', 'height' => '108', 'border' => '0', 'alt' => 'Article Thumbnail', 'class' => 'img-thumbnail')).'
<div class="caption">
<h4>'.anchor('members/'.url_title($member['display_name']).'/'.$member['id'], $member['display_name']).'</h4>
<p>Joined on '.$member['date_joined'].'</p>
</div>
</div>
</div>
';
} else {
$str .=
'<div class="media">
<div class="media-left">
'.img(array('src' => attachment_url($member['avatar']['file_name']), 'width' => '192', 'height' => '108', 'border' => '0', 'align' => 'left', 'alt' => 'Article Thumbnail', 'class' => 'img-thumbnail')).'
</div>
<div class="media-body">
<div class="media-heading">
<h4>'.anchor('articles/'.url_title($member['display_name']).'/'.$member['id'], $member['display_name']).'</h4>
<p>Joined on '.$member['date_joined'].'</p>
</div>
</div>
</div>
';
}
}
echo $str;
索引正文视图文件:
<?php
$CI =& get_instance();
$str = '
<div class="container">
<section id="content">
<div class="row">
';
if(count($column_results) > 0){
foreach($column_results as $id => $column){
$str .= '<div class="col-md-'.$column['width'].'">
';
if(count($column['widgets']) > 0){
foreach($column['widgets'] as $id => $widget){
$str .= '<div class="panel panel-default">
<div class="panel-heading">
<h3>'.$widget['title'].'</h3>
</div>
<div class="panel-body">
'.$widget['body'].'
</div>
</div>
';
}
}
$str .= '</div>
';
}
}
$str .= '</div>
</section>
</div>
';
echo $str;
答案 0 :(得分:0)
所以不必在变量中加载视图,我不得不使用输出缓冲区,然后将我的视图加载到ob内容中,如此...
ob_start();
$this->load->view('inner_view_file', array());
$output = ob_get_contents();
ob_end_clean();
$data['inner_view'] = $output;
$this->load->view('outer_view_file', $data);
完美无缺。有点不整洁,但完成工作。
答案 1 :(得分:0)
使用输出缓冲区提供的答案是codeigniter在后台执行此操作的方式,唯一的区别是您重复可能会对性能产生影响的过程。最简单的解决方案是使用:
$data['inner_view'] = $this->load->view('inner_view_file', array(), TRUE);
传递TRUE
作为第3个参数告诉codeigniter您不想输出视图文件并且您希望以字符串形式返回。
<强> NB 强>
在您的视图文件中,您应该能够访问$this
而不是$CI = &get_instance();
最后,即使创建小部件的方式没有那么大的错误,也许值得研究使用HMVC和codeigniter。