为什么将MVC中的视图拆分为视图类和模板 [英] Why split the View in MVC into a view class and a template

查看:120
本文介绍了为什么将MVC中的视图拆分为视图类和模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对设计模式还比较陌生,但是我觉得我对MVC模式以及这种代码分离带来的好处有了很好的了解.

I'm relatively new to design patterns but I feel that I've got a good understanding of the MVC pattern and the advantages that this separation of code brings.

但是,我两次都看到MVC模式在起作用(Magento和Joomla!),还有进一步的专业化, view 由视图类(Magento块)和一个视图类组成. PHP模板文件.如果有人可以解释这种拆分的好处,我将不胜感激.

However, both times I've seen the MVC pattern in action (Magento and Joomla!), there is further specialization, with the view consisting of both a view class (Magento block) and a PHP template file. I would appreciate it if someone could explain the benefit of this split.

我也不知道如何在视图类和模板文件之间拆分我的代码.有时我发现自己写了一个似乎是多余的视图类(在Joomla中!),它仅访问模型,然后仅使数据可用于模板.模板中应显示什么代码,视图类中应显示什么代码?

I'm also at a loss as to how to split my code between the view class and the template file. Sometimes I find myself writing what seems to be a redundant view class (in Joomla!) which simply accesses the model and then just makes the data available for the template. What code should appear in the template and what code should appear in the view class?

推荐答案

在MVC启发的设计模式下,视图负责您所有的UI逻辑.他们应该从模型层请求信息,并根据收到的信息选择应使用哪些模板来创建响应.甚至不需要任何渲染(视图也可以发送HTTP标头).

View, in MVC-inspired design patterns, are responsible for all of you UI logic. They should be requesting information from model layer and, based on what they receive, choosing which templates should be used for creation of the response. Or even if any rendering is required (view can just as well just send a HTTP header).

您可以说在经典MVC和Model2 MVC模式中,视图仅从模型层读取,而控制器仅向模型层写入.

如果从模型层收到一些错误状态,则视图将采用主布局模板并对其进行补充,该模板包含错误消息的HTML片段.然后,它将整个节目组装成一个展示给用户(如果是Web应用程序则是浏览器).

If you receive some error state from model layer, the view takes the main layout template and supplements it template, which is contains HTML fragment for error messages. Then it assembles the whole thing an shows to the user (which, in case of web applications is the browser).

在基本的Web应用程序中,模板只是包含标签和php变量的简单文件.

Templates, in your basic web application, are just simple files with a mix of tags and php variables.

这篇关于为什么将MVC中的视图拆分为视图类和模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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