是否反对使用变量在另一个视图中调用视图的 MVC 模式?
编辑澄清并修改示例伪代码
我试图理解 mvc 的概念,有时它让我感到非常头痛。
我面临着一个问题并试图思考解决方案。我正在使用 codeigniter,问题是如何为我的网站中的不同类别和搜索制作不同的页面标题和描述。
这是我想到的解决方案(我知道这不是演示它的最佳方式,但不要拘泥于细节,只需看看基本思想):
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>
实际的标题和描述可能会有很大不同,可以有有一些例外,我可能必须对不同的类别和不同的搜索页面使用不同的代码。那么,以这种方式反对 MVC 或者有更好的方法吗?
这样,我试图避免在控制器或视图中多次使用相同的 foreach 循环。 实际的标题和描述可能会有很大不同,可能会有一些例外,我可能必须对不同的类别和不同的搜索页面使用不同的代码。那么,这样做是针对 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于 MVC 是一种设计模式而不是语法规则,因此您在这里拥有一定程度的自由。我认为在视图中使用视图没有任何问题,不仅在 CI 中可行,而且经过深思熟虑,您可以调用不同的视图并嵌套它们,而不必将其数据集作为视图传递给“辅助”视图已缓冲,并且无论如何它们都可供所有人使用。
所以你可以这样做:
controller:
In ad_details.php:
如果你 var_dump() 它,你会看到 header.php 和 footer.php 现在都有一个变量 $results,而不必再次传递它。
Since MVC is a design pattern and not a syntax rule, you've a certain degree of freedom here. I see nothing wrong in using a view inside a view, and not only it's possibile in CI, but it's so thought out that you can call different views and nest them withouth having to pass the "secondary" view its set of data, as views are buffered and they will available to all anyways.
SO you can do something like:
controller:
In ad_details.php:
If you var_dump() it, you will see that both the header.php and footer.php will have a variable $results now, without having to pass it again.
MVC 的存在是为了让您的生活更轻松、开发更快,如果您无法从控制器级别调用它并且允许在视图内调用它,那么就这样做。
MVC is there to keep your life easier and your development faster, if you cannot call it from the controller level and it's permitted to call it inside the view, do so.
编辑:我从阅读评论中了解到,您不能在 CodeIgniter 中使用
$results[0]
,您必须使用$results- >row()
显然 - 我保留了我的答案,因为它不应该是 100% CI 特定的,但请记住这一点。这是我编写您的视图的方式:
在这里您可以看到我已经消除了 foreach 循环,除非您实际上依次处理每个结果。话虽如此,我个人的观点是,您对“不要重复自己”的口头禅有点太过分了,以至于您为此牺牲了代码的可读性/可维护性。如果将多个 foreach 循环合并为一个会使代码更难以理解,则不要这样做。
我还尝试减少视图中的 PHP 和变量分配量,这通常是一件好事,但如果做得太过分,也会带来不便。例如,您可能希望通过在视图顶部执行此操作来减少所有
$results[0]->
内容:但话又说回来,您可能不会这样做。不管你的鸭子嘎嘎叫什么。 :)
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:
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:Then again, you might not. Whatever quacks your duck. :)
通常,您希望拥有一种模型 - 一种视图,其关系和数据/流通过某个控制器进行控制。一个控制器可以管理多个模型和视图,但每个不同的视图应该有一个相应的模型。
阅读 ASP NET MVC 是个好主意。
Generally you want to have one model - one view with the relationship and data/flow controlled via some controller. A controller can manage multiple models and views but each distinct view should have a corresponding model.
It would be a good idea to read up on ASP NET MVC.
在控制器中处理您的逻辑。
一般来说,这意味着决定显示内容的循环和条件不会出现在视图中。视图可以使用逻辑来决定如何显示内容、循环创建表等。
将所有这些放入控制器中。
Handle your logic in the controller.
in general, this means loop and conditionals which decide what to display stay out of the view. the view can use logic to decide how to display stuff, looping to create tables, etc.
put all this in the controller.