MVC 框架是否混淆了视图和模板?

发布于 2024-09-30 09:33:06 字数 699 浏览 5 评论 0原文

我正在学习 MVC 来开发一个网站,我发现与“理论”和当前广泛实现的(明显?)差异令人困惑。

原始Martin Fowler 的文章 MVC 概念似乎倾向于“智能”视图,它直接与模型交互以检索数据显示,并自行决定如何呈现数据。在网络上,这应该是输出格式(HTML、JSON、PDF 等)。

另一方面,当前框架中的视图,例如 ASP. NETZend 似乎只不过是HTML 模板。

这对我提出了一个问题:我可以为每种格式创建一个视图,但是我应该在这些框架中的哪里决定显示什么视图?控制器应该决定加载哪个视图吗?但是,控制器有责任知道要显示的正确格式吗?这似乎与理论相悖。

当您使用MVC并且需要输出不同的格式时,您有什么经验?

I'm learning MVC to develop a website, and I'm finding confusing the (apparent?) differences from the "theory" and the current widespread implementations.

In the original and Martin Fowler's article the MVC concept seems to favor a "smart" View, which interacts directly with the Model to retrieve the data to display, and decides by itself how should the data be presented. On the web, that should be the output format (HTML, JSON, PDF, etc).

On the other hand, the View in current frameworks like ASP.NET and Zend seems to be little more than an HTML template.

This to me raises a question: I can create a View for each format, but then where should I in these frameworks decide what View to display? Should the controller decide which View to load? But then, is it the controller's responsibility to know the right format to display? That seems to be against the theory.

What is your experience when using MVC and you need to output different formats?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-10-07 09:33:07

我对真正的 MVC 的体验仅限于 Zend Framework,而且我还很新手。但这是我的观点(无论其价值如何):

在 Fowler 的文章中,他描述了一个 MVC,其中 C 和 V 完全不知道彼此。人们普遍认为 C 和 V 可以相互作用。请参阅 Dean Helman 的 解释和 ZF 的解释

根据我的经验,控制器可以告诉视图,例如:“我想要此数据为 JSON”或“将此数据添加到导航”或“这是数据,我不在乎你用它做什么” ”。

编辑:我认为你可能会混淆 MVC
与 MVP。看
什么是 MVP 和 MVC,有什么区别?< /a>


My experience with a true MVC is limited to the Zend Framework and I'm still pretty green. But this is my opinion(for what its worth):

In Fowler's article, he describes an MVC where where the C and V are completely unaware of each other. It is widely excepted that the C and V can interact. See Dean Helman's explanation and ZF's explanation.

It has been my experience that the controller can tell the view, for example: "I want this data as JSON" or "Add this data to the navigation" or "Here is the data, I don't care what you do with it."

EDIT: I think you may be confusing MVC
with MVP. See
What are MVP and MVC and what is the difference?

尽揽少女心 2024-10-07 09:33:07

MVC 之间有空格连接,这也很重要。

这个V有什么用?访问模型的视图助手是 M 还是 V?布局或占位符也是吗?

没有严格的定义,也没有人期望有严格的定义。这都是关于分离的,而且分离的严格程度可能会有所不同。

Fatmuemoo 注意到可能会让您感到困惑,或者也许是框架或福勒。

区别在于一个让控制器决定,另一个让视图助手做决定。但真正的事情是由请求决定的。你可以在渲染视图之前或之后处理它,这取决于你想做什么,而且它仍然是 MVC。

There is whitespace which joins M, V and C and this is also very important.

What's this V is for? Are view helpers that access models M or V? Are layouts or placeholders too?

There is no strict definition, and nobody expects strict one. It's all about the separation, and it may vary how strict it is.

As Fatmuemoo noticed it may be you confusing, or maybe the frameworks or Fowler is.

What's the difference is one makes controller decide, other one view helper do. But the real thing is the request decides. You may process it before rendering the view or after, depending what do you want to do, and it is still MVC.

盗梦空间 2024-10-07 09:33:06

我认为您忽略了这样一个事实:当前框架中的视图做了很多工作来与模型通信以检索要显示的数据,并且这些视图从根本上是模态锁定的(即,HTML 视图从根本上显示 HTML 等)。 )。决定如何显示数据的“智能视图”的概念也有点混乱;例如,在 ASP.NET MVC 中,视图实际上会根据用户使用的浏览器以不同的方式呈现 HTML,因此存在一些这种情况,但它们并没有完全改变视图的模式。呈现数据(例如视觉与听觉)。

I think you're missing the fact that the Views in current frameworks do a lot of the work to communicate with the model to retrieve the data to display, and which are fundamentally modally locked (i.e., the HTML View fundamentally displays HTML, etc.). The concept of a "smart view" which determines how to display the data is a little bit muddled, as well; for example, in ASP.NET MVC, the Views will actually render your HTML differently based upon what browser the user is using, so there's a bit of that, but they don't go so far as to completely change the mode in which the data is presented (visual vs. auditory, for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文