dousi1906 2012-01-12 06: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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配