如果您在 Web 应用程序中使用 MVC,那么您不需要使用 Smarty(TemplateEngine) 对吗?
我只是想了解模板(系统)。如果您在 Web 应用程序中使用 MVC,那么您不需要使用 Smarty(模板引擎)之类的东西,因为您已经通过使用 MVC 将应用程序代码与表示代码分开了,对吗?请纠正我?
那么我认为它是 MVC 或模板是正确的吗?或者您在应用程序中同时使用这两种方法吗?如果有人可以详细解释这一点,那就太好了。
先感谢您;-)
I'm just trying to understand the Templating(system). If you use MVC in your web application then you don't need to use something like Smarty(template engine) as you are already separating application code from presentation code anyway by using MVC right? please correct me?
So am i correct in thinking it's MVC OR Templating or do you use both in your apps?If any one could explain this in detail it would be great.
Thank you in advance;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
MVC 具有三个主要组件。模型 - 视图和控制器。视图与控制器和模型进行通信,这就是它必须完成的方式,如果您的视图不与其他两个组件通信,那么您就有了称为 PAC(表示-抽象-控制)的东西。
这意味着视图具有您可能想到的更多功能,这样您就可以使用多种技术来完成视图的生成。
直接回答问题,您可以(并且应该)为您的视图使用模板引擎。这是一个简短的示例,您可以看到 CakePHP(PHP MVC 框架)如何处理它
假设您转到 http://www.example.com/books/get/2
这是由控制器处理的,控制器知道用户正在请求操作“get”(带有参数 2,可能是 id ),模型“Books”。在那里,它必须查找 id=2 的书并检索它。
控制器执行数据库查找,获取相应的书籍,并填充变量。短代码:
最后一行(%this->set)将为视图分配一个变量。更准确地说,“get”视图将有一个名为 bookVariable 的变量,其中包含书籍的内容。视图的简短代码:
然后您可以看到这两个组件如何交互,您可以或不能使用模板系统,它与 MVC 无关。再次,我建议您使用它。
MVC has three main components. Model - View and Controller. Views camunicate with controller and model, that's the way it has to be done, if you're view isn't talking with the other two components, then you have something called PAC (Presentation-abstraction-control).
That means that the view has more functionality that you may think, and that's way you can use several techniques to acomplish the generation of views.
Going straight to the answer, you may (and you should) use a template engine for your views. Here is a short example, you can see how it's handled by CakePHP (PHP MVC framework)
Supose you go to http://www.example.com/books/get/2
That is handled by the controller, who knows that the user is requesting for the action "get" (with parameter 2, that might be the id), of the model "Books". There, it has to look for the book with the id=2, and retrieve it.
The controller performs a database lookup, get the corresponding book, and populate a variable. Short code:
The last line (%this->set) will assign a variable to the view. More precisely, the "get" view will have a variable called bookVariable with the contents of the book. Short code for the view:
Then you can see how this two components interact, you can or cannot use a template system, it has nothing to do with MVC. Again, i recommend you to use it.
两者都用就可以了。模板化仅与应用程序中的视图相关。
您的数据仍然来自模型和控制器,它们将值等传递给模板引擎,模板引擎包括您的视图文件并渲染它们,传递您的数据,以便它可以显示在您的视图中。
It's OK to use both. Templating only pertains to the views in your application.
Your data still comes from the model and controller, which pass values and such to the template engine, which includes your view files and renders them, passing your data along so it can be displayed in your views.
就我而言,模板系统就是我的“视图”。
它完全愚蠢,与“控制器”对话,验证输入并生成输出等。
Well in my cases, the template system is my "view".
Its completely dumb and talks to the "controller" which validates input and generates the output and such things.
您必须记住,MVC 只是一种模式。在我看来,这是一种思维方式。如果您在 MVC 中使用模板解析器,这一切都取决于您,只要您将应用程序的逻辑和视图分开即可。
我更喜欢只解析 PHP 的解析器。它有一些优点(不需要学习特定的模板语言,速度更快一些,因为实际上不需要解析任何内容)。
You've got to keep in mind that MVC is just a pattern. It is, in my opinion, it is a way of thinking. It is all up to you if you use a template parser in your MVC, as long as you keep the logic and the view of your application seperate.
I prefer a parser that just parses PHP. It has a few advantages (no specific template language needed to learn, a bit faster because nothing actually need to be parsed).
MVC 模式侧重于数据和流程控制的责任。谁可以处理数据和流程路径重定向。无论如何实现视图层(是否使用Smarty),您的视图都不应修改数据或直接控制工作流程。
The MVC pattern focuses on the responsibility of data and flow control. Who can handle the data and the the flow path redirect to. No matter how to implement the view layer (using Smarty or not), your view sholudn't modify the data or control the work flow directly.
Smarty(很像其他第三方模板引擎)是 MVC 视图部分的可能实现之一。您不必使用它,但如果您愿意,也可以使用它。
MVC 将数据与表示分离,但并不完全分离。除非您生成完全静态的 html,否则您必须在模板中放置一些动态生成的数据。整个想法是,您的控制器计算该数据并将其放入一个或多个 PHP 变量中,然后由您的模板使用。
假设您想问候一个用户,该用户的名字是从某个地方(数据库、会话等)获取的。您的控制器将执行其操作并最终将用户名传递给您的视图:
$view 是代表您的视图的对象。无论是使用 Smarty、vanilla PHP 还是其他模板系统实现,此时都无关紧要。语法可能有所不同,但逻辑是相同的。
此时您希望模板使用该变量。它将如何执行取决于您的视图的工作方式。使用普通 PHP,您可能让 $view 在内部执行以下操作:
并让您的模板像这样获取:
或者您可以使用 Smarty,在这种情况下您的模板将如下所示:
逻辑是相同的,实现是变化的。
最后,如果您正在考虑是否应该在视图中使用 Smarty,我个人建议您不要使用它,因为它没有做任何普通 PHP 做不到的事情,这实际上使它变得多余。这是另一件需要担心的事情,更不用说您必须确保它已安装在您的应用程序需要运行的每台服务器上。
Smarty (much like other third-party templating engines) is one of the possible implementations of the View part of MVC. You don't have to use it, although you can if you want.
MVC separates data from presentation, but not completely. Unless you're producing completely static html you'll have to place some dynamically generated data in your template. The whole idea is that your controller calculates this data and places it in one or more PHP variables which are then used by your template.
Let's say you want to greet a user whose name you've fetched from somewhere (database, session, whatever). You controller will do its thing and finally pass the name of the user to your View:
$view is an object representing your View. Whether it's implemented using Smarty, vanilla PHP or some other templating system is irrelevant at this point. The syntax might vary, but the logic is the same.
At this point you want your template to use that variable. How it will do so depends on how your View works. With plain PHP you might have $view do the following internally:
and have your template fetch this like so:
Or you could use Smarty in which case your template will look like this:
The logic is the same, the implementation is what changes.
Finally if you are considering whether you should use Smarty in your View, I'd personally recommend against it as it does nothing that plain PHP can't do, which effectively makes it excess baggage. It's one more thing to worry about, not to mention you'll have to make sure it's installed it on every server that your app will need to run on.
模板化是一种可以在 MVC(模型-视图-控制器)的视图部分中使用的工具。
Templating is a tool you can use in the View part of MVC (Model-View-Controller).