dousi1906 2012-01-11 22:19
浏览 23
已采纳

是否反对使用变量在另一个视图中调用视图的MVC模式?

edited for clarification and modified example pseudo-code

I was trying to understand the concept of mvc and sometimes it gives me some serious headaches.

I was facing with a problem and trying to think a solution. I am using codeigniter and the problem is how to make different page titles and descriptions for different categories and searches in my web site.

Here is the solution I thought (I know it's not the best way the demonstrate it but don't stuck in details just look at the basic idea):

controller

 $data['results'] = call model and get results
 this->load->view(ad_details,$results);

ad_categories view:

foreach ($results as $key => $row) {
        $ad_title = $row->title; 
        $ad_id = $row->id;
        $ad_price = $row->price;
        $ad_status = $row->status;
        $ad_city = $row->city;
        $ad_user = $row->user;
        if ($key<1) {
          // let's be sure even customers enter same info we got unique titles and descriptions for search engines 
         $data['title'] = "$ad_title $ad_id $ad_price";
         $data['description'] = "Second Hand Autos for $ad_status from $ad_user in $ad_city";
         this->load->view(header,$data);
         <body>
        }
      $ad_description = $row->description; 
      <h2>$ad_title</h2>
      <p>$ad_description</p>
      }
      </body>
     <? this->load->view(footer); ?> 

header_view file

<!doctype html>
    <head>
        <meta charset="utf-8">
        <title><?=$title?></title>
        <META NAME="description" CONTENT="<?=$description">
        <META NAME="keywords" CONTENT="<?=$keywords?>" >    
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <link rel="stylesheet" href="css/style.css">
        <script src="js/libs/modernizr-2.0.6.min.js"></script>              
    </head>
     <body>

The actual titles and descriptions can be quite different, there can be some exceptions and I may have to use different codes for different categories and different search pages. So, doing it in that way against MVC or is there better way to do that?

In that way, I’m trying to avoid using same foreach loops multiple times in controller or in views. the actual titles and descriptions can be quite different, there can be some exceptions and i may have to use different codes for different categories and different search pages. So, is doing it in that way against mvc or is there better way to do that ?

展开全部

  • 写回答

5条回答 默认 最新

  • duancan1950 2012-01-16 02:36
    关注

    Edit: I've learned from reading the comments to this that you can't use $results[0] in CodeIgniter, you have to use $results->row() apparently - I'm leaving my answer as it was because it wasn't supposed to be 100% CI-specific, but bear it in mind.


    Here's the way I'd have written your view:

    <?php $this->load->view('header', array(
      'title' => $results[0]->title.' '.$results[0]->id.' '.$results[0]->price,
      'description' => 'Second hand autos for '.$results[0]->status.' from '.$results[0]->user.' in '.$results[0]->city
    )); ?>
    <body>
      <h2><?php echo htmlspecialchars($results[0]->title); ?></h2>
      <p><?php echo htmlspecialchars($results[0]->description); ?></p>
    
      <?php foreach($results as $row): ?>
        <!-- Deal with results -->
      <?php endforeach; ?>
      </body>
     <? this->load->view('footer'); ?>
    

    Here you can see I've eliminated the foreach loop except where you're actually dealing with each of the results in turn. Having said that, my personal opinion is that you're taking the "don't repeat yourself" mantra a little bit too far, to the extent that you're sacrificing code readability/maintainability for its sake. If combining multiple foreach loops into one makes the code more difficult to understand, don't do it.

    I've also tried to cut down on the amount of PHP and variable assignments in the view, which is usually a good thing, but this too can be inconvenient if pushed too far. For instance, you might want to cut down on all the $results[0]-> stuff by doing this right at the top of your view:

    $r = $results[0];
    

    Then again, you might not. Whatever quacks your duck. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部