php 内部不支持 HTML 的情况?

发布于 2024-10-31 14:24:10 字数 267 浏览 5 评论 0原文

我知道控制内容应该与视图内容分开,但在很多情况下我知道使用 html 模板执行操作会更慢且过于复杂。

我的问题是……哪里可以接受?

我正在谈论的示例...输出非常动态的表格的函数、创建输入元素的函数以及格式化文本的函数。

除了“丑陋”之外,我能想到的唯一缺点是我没有在这些函数内部进行制表符和换行符,这使得输出的 HTML 有点丑陋。大多数时候,当我面对这个问题时,我只是说忘记约定——在大多数情况下,功能胜出。

有人对这个话题有什么见解或意见可以分享吗?

I know that control stuff should be separated from view stuff, but there's so many cases where I know it's slower and over-complicating to do things with html templates.

My question is... where is it acceptable?

Examples of what I'm talking about... functions that output very dynamic tables, functions that create input elements, and functions that format text.

The only con I can think of, other than it being "ugly," is that I don't tab out and newline inside of these functions, which makes the output HTML a bit ugly. Most of the time when I face this, I just say forget convention - functionality wins in most cases.

Anyone have any insight or opinions on the topic to share?

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

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

发布评论

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

评论(2

没有伤那来痛 2024-11-07 14:24:10

这取决于您想在多大程度上分离您的关注点。一般规则是您应该能够切换页面的显示而不影响幕后逻辑。

考虑一下您正在编写的内容是否属于站点的表示层或业务逻辑。

如果您正在编写的部分内容与其显示方式完全分开,那么它可能应该位于其他位置。

但同样,这很大程度上取决于项目的开发人员和范围。对于小型项目,将逻辑与表示(HTML)混合并不是一件坏事,因为它可以节省时间。但是,如果您正在开发大型应用程序,则需要更加严格地进行关注点分离,以便从长远来看使维护变得更加容易。

It depends on how much you want to separate your concerns. The general rule is you should be able to switch out the display of your page without affecting the behind-the-scenes logic.

Think of whether what you are writing falls under the presentation layer of your site, or the business logic.

If part of what you are writing is completely separate from how it will be displayed, it should probably be in another place.

But again, this is very much up to the developer and scope of the project. For small projects, having logic intermixed with presentation (HTML) is not a bad thing as it saves time. However if you are developing a massive application, you need to be much more strict with separation of concerns to make maintenance easier in the long run.

月光色 2024-11-07 14:24:10

我知道这需要更多工作,但从长远来看,MVC 模式更易于维护。即使对于动态表,最好创建一个可以创建任何大小的表/列表/div 的类,然后将数据传递给它,而不是在模型/控制器中查看数据。

我有时会在非常小的项目上作一些欺骗,并将业务逻辑与演示混合在一起,但如果项目不断增长,通常需要更多的工作来纠正它。因此,除非我真的很懒,否则以 MVC 方式相当严格地实现代码并不难。

I know it's more work, but the MVC pattern is much more maintainable in the long run. Even for dynamic tables, it would be better to create a class that can create any size table/list/divs and then pass your data to it than have view data in your model/controller.

I sometimes cheat a bit on very small projects and mix in business logic with presentation, but if the project ever grows it usually is more work correcting it down the road. So unless I'm being really lazy, it's not that hard to be fairly strict with implementing code in an MVC manner.

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