是否反对MVC模式调用另一个视图中的视图与变量? [英] Is it against to MVC pattern calling a view in another view with variables?

查看:103
本文介绍了是否反对MVC模式调用另一个视图中的视图与变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑澄清和修改示例伪代码

我试图理解mvc的概念,有时它会给我一些严重的头痛。

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

我面临一个问题,试图想一个解决方案。我使用codeigniter,问题是如何使不同的类别和搜索的不同的页面标题和描述在我的网站。

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):

控制器

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

ad_categories视图:

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文件

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>

实际的标题和描述可能完全不同,可能有一些例外,针对不同类别和不同搜索页面的不同代码。所以,这样做对MVC或有更好的方法来做这个?

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?

这样,我试图避免多次使用相同的foreach循环控制器或视图。
实际的标题和描述可能是完全不同的,可能有一些例外,我可能必须使用不同的代码为不同的类别和不同的搜索页面。

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 ?

推荐答案

编辑:

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'); ?>

在这里你可以看到我已经消除了foreach循环,除非你实际处理每个结果依次。话虽如此,我个人的意见是,你正在采取不要重复自己的咒语一点太远,以至于你牺牲代码的可读性/可维护性的缘故。如果将多个foreach循环组合成一个,使代码更难理解,不要这样做。

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.

我也试图减少PHP的数量,在视图中的变量赋值,这通常是一件好事,但如果推得太远,这也可能不方便。例如,您可能希望通过在视图顶部执行此操作来减少所有 $ results [0] - > 内容:

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. :)

这篇关于是否反对MVC模式调用另一个视图中的视图与变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆