结合 PHP 和 HTML 的首选方式?
我通过破解 phpBB2 来学习 PHP,甚至向他们的数据库提交了一些 mod,其他人下载并使用了这些 mod。 (我不相信 phpBB3 已经发布这么久了,不再支持 phpBB2,所以 v2 mods 数据库已经不复存在了。)
phpBB 我最喜欢的事情之一是他们的模板系统,它让编辑器完全分离 HTML 和PHP。 PHP 文件包含 PHP:逻辑、数据库查询和激活模板。 TPL 文件包含模板:HTML、模板变量和专门的 HTML 注释,以允许条件或重复块。
然而,每当我在网上看到某人的 PHP 代码时,它要么是使用单个函数等的小片段,要么 PHP 充满了包含 HTML 的字符串(或更糟糕的是,HTML 中散布着 PHP)。 phpBB 是我见过的唯一真正将语言和标记语言分开的 PHP,这表明很少有其他 PHP 代码库(如果有的话)做这样的事情。
我希望再次开始使用 PHP,但这次不是 phpBB 论坛,而是在一个团队中。 根据我的经验,PHP 和 HTML 的分离并不常见(如果我在这方面错了,请纠正我!)。然而,我已经习惯了这条分界线,我讨厌阅读混合的 PHP 和 HTML。
在 PHP 编程的“现实世界”中,首选方法是什么:
- 带有 HTML 字符串的 PHP 文件
- 用 PHP 块分解的 HTML 文件
- PHP 和 HTML 完全分开(我希望?)
- 还有别的吗?
I learned PHP by hacking away at phpBB2, even submitting a few mods to their database, which others downloaded and used. (I don't believe phpBB2 is supported any more with phpBB3 out so long now, so the v2 mods database is no more.)
One of my favorite things about phpBB was their templates system, which let the editor completely separate the HTML and the PHP. PHP files contained PHP: logic, database queries, and activating the templates. TPL files contained templates: HTML, template variables, and specialized HTML comments to allow for conditional or repeating blocks.
However, any time I see someone's PHP code online, it's either a small snippet working with a single function or such, or the PHP is full of strings containing HTML (or worse, HTML with PHP interspersed). phpBB is the only PHP I've looked at which actually separates the language and the markup language, suggesting to me that few, if any, other PHP codebases do such a thing.
I'm looking to start working with some PHP again, but this time it won't be a phpBB forum, and it will be on a team. Based on my experience, separation of PHP and HTML is uncommon (please correct me if I'm wrong on this!). However, I'm so used to that dividing line, I hate reading mixed PHP and HTML.
In the "real world" of PHP programming, what's the preferred method:
- PHP files with strings of HTML
- HTML files broken up with PHP blocks
- PHP and HTML completely separate (I wish?)
- Something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果你想将 PHP 与 HTML 分开,你可以使用模板引擎(例如 smarty http://www.smarty.net /)。
If you want to separate the PHP from the HTML you can use a template engine (Like smarty http://www.smarty.net/).
就我个人而言,当 PHP 已经是一个非常好的解决方案时,我认为没有理由添加模板系统/语言。
我的首选方法是将显示与应用程序的逻辑分开。这可以采用完整的 MVC 框架的形式。然而,这也可能是一个简单的问题,即如何编写代码。
扩展:
自从犯了在 HTML 中散布大量 ASP 代码的错误以来,我一直尝试将页面逻辑与显示分开。在单个页面中,这意味着将所有页面逻辑放在顶部,将要显示的信息存储在变量中,然后在 PHP 文件底部的 HTML 中回显它们。 HTML 部分中出现的唯一逻辑是显示逻辑。换句话说,简单的错误处理、if、循环等。基本上,您会在大多数模板语言中找到相同的内容。
我避免使用模板语言的原因是它是我需要担心的另一种语法形式。有什么意义? PHP 提供的功能超出了我的需要。
同时,您可以通过分离事物来采用简单的 MVC 方法:
controller.php
view.php:
这并非没有缺点和问题。但是,如果您认为可以在应用程序逻辑和显示之间实现完全、清晰的分离,那么您就错了。
Personally, I see no reason to add a template system/language into the mix, when PHP is already a prefectly good solution.
My preferred approach is to separate the display from the logic of the application. This could take the form of a full blown MVC framework. However, it could also be a simple matter of how you go about writing your code.
Expansion:
Ever since making the mistake of interspersing my HTML with copious amounts of ASP code, I have tried to separate page logic from display. Within a single page, this means placing all of the page logic at the top, storing the information to be displayed in variables and then echoing them out within the HTML at the bottom of the PHP file. The only logic which appears in the HTML portion is display logic. In other words, simple error handling, ifs, loops, etc. Basically, the same stuff that you'll find in most templating languages.
My reason for avoiding templating languages is that it's yet another form of syntax that I need to worry about. What's the point? PHP provides more than I need for this purpose.
Meanwhile, you can take a simple MVC approach by separating things:
controller.php
view.php:
This isn't without its drawbacks and issues. However, if you think you can have complete, clean separation between application logic and display, you're mistaken.
我们为 MVC 使用定制的代码点火器基础,只是将逻辑和布局分开。这并不一定意味着我们的 html 中没有 php,只是它不用于逻辑。恕我直言,在模板中使用 php 代码循环记录集是完全可以的,只是不要尝试与业务对象对话并在布局模板中实际执行操作。如果您真的想摆脱所有模板,您当然也可以查看 tal 或其他一百万个模板那里有 php 代码。我有点认为这大多是矫枉过正,除非“在此处插入特殊情况”
编辑修复了拼写错误
We use a customized code igniter base for MVC and just have logic and layout separated. that doesn't necessarily mean there's no php in our html, just that its not used for logic. It's perfectly okay to loop through a recordset with php code in a template imho, just don't try to talk to business objects and actually do stuff in the layout templates. You could ofcourse also look into something like tal or a million others for the templates if you really want to get rid of all php code there. I'm kinda thinking that's mostly overkill though, unless "insert special circumstance here"
edit fixed typo
根据我的经验,许多 PHP 开发,无论是好还是坏,最终都是通过包含内容相互连接的 php 文件,这些文件根据需要打印出 HTML 片段。
但请注意,我的大部分 PHP 经验更多的是“我们只需要一些可行的解决方案”,而不是“我们需要最优雅、最高效的解决方案”
In my experience, a lot of PHP development, whether for better or worse, ends up being php files connected to each other through includes that print out snippets of HTML as needed.
Mind though, most of my PHP experience is for more of a "we just need something that works" solution than a "we need the most elegant and efficient solution"
是的,那里的代码总体质量令人尴尬。
虽然您可以使用自己的成熟模板引擎,但这可能有点过大,并且在许多情况下,当您可以只用 PHP 编写表示逻辑时,它们提供的有限的特定于领域的语言会妨碍您。
如果您仍想编写普通的 PHP,但不想写意大利面条:
将操作代码保留在文件顶部或其他文件中。此处仅放置应用程序逻辑,而不放置任何类型的 HTML 或模板。此阶段生成的任何需要由模板显示的信息都需要放入变量中传递给模板部分,不
打印
在中间打印出来大量业务逻辑。将您的基于标签的模板知识应用到 PHP 中。为 HTML 和 PHP 表示逻辑提供一个正确缩进的代码层次结构,就好像您正在编写“格式良好”的 XML(无论您是否真正使用 XHTML)。不惜一切代价避免将 HTML 放入字符串中。
定义一种更简单的调用
htmlspecialchars()
的方法,因为否则一直输入它会很痛苦,而且如果你不一直输入它,你就会存在潜在的安全敏感错误。总结一下,例如:
哦,在 PHP 编程的“现实世界”中,一般项目都会不假思索地将各种方法混杂在一起。在现实世界中,代码是难以维护的、漏洞百出且不安全的。你不想看行业标准,因为行业标准在各个方面都会被打破。
Yeah, the general quality of code out there is embarrassing.
Whilst you can go to a full-blown templating engine of your own, that may be overkill and in many cases the limited domain-specific-languages they provide will get in your way, when you could just be writing presentation logic in PHP.
If you want to still be writing normal PHP, but without the spaghetti:
Keep action code at the top of the file or in a different file. Put only application logic here, not any kind of HTML or templating. Any information generated in this stage that needs to be displayed by the template needs to go in a variable to be passed to the templating part, not
print
ed out in the middle of a load of business logic.Take your knowledge of tag-based templating and apply it to PHP. Have a single, correctly-indented hierarchy of code for both HTML and PHP presentation logic, as if you were writing ‘well-formed’ XML (whether or not you're actually using XHTML). Avoid putting HTML in strings at all costs.
Define an easier way of calling
htmlspecialchars()
, because otherwise typing that all the time is going to be a real pain, and if you aren't typing it all the time you're going to have potentially security-sensitive errors.To summarise, eg.:
Oh, in the real world of PHP programming, the average project has a mishmash of approaches thrown together without any thought. In the real world the code is unmaintainable, bug-ridden, and insecure. You don't want to look at the Industry Standard, because the Industry Standard is to be broken in every way.
我更喜欢使用模板系统来最大限度地减少 HTML 文件中包含的 PHP 代码量。我确实没有看到任何方法可以将 PHP 与 HTML 完全分离,但我认为将业务逻辑(数据库)与表示逻辑分离就足够了。
我的模板系统是自制的,但我相信您可以通过执行谷歌搜索找到各种可用的模板系统。
I prefer to use a template system to minimize the amount of PHP code contained in the HTML files. I really don't see any way to entirely separate PHP from HTML, but I feel that it is sufficient to separate business logic (database) from presentation logic.
My template system is home-baked, but I'm sure you can find a variety that would work by preforming a google search.
。但许多 PHP 代码是由缺乏经验的开发人员编写的。此外,PHP 并不鼓励,而是阻止编写好的代码,将 HTML 与编程代码混合的能力就是其中的例子之一。
如果您希望编写高质量的代码,以便能够轻松维护和扩展它,我强烈建议使用模板引擎或类似的东西。混合两者会对代码的可读性产生巨大影响,并且随着时间的推移会导致事情变得越来越糟。重构即使不是不可能,也会很痛苦。
现在,您可以选择多种将 HTML 与 PHP 分离的方法
最明显的一种是使用现有的布局引擎。它们有很多,其中一些做得非常好,并且对性能的影响非常低。
您还可以编写自己的引擎。对于大型项目来说,这可能不是一个好主意(为什么要重新发明轮子?),但对于小型项目或当您需要非常具体的东西时,这可能是一个解决方案。
我在大多数项目中使用的最后一种方法是从业务层构建 XML(XML 序列化在 PHP 中非常容易),然后使用 XSLT 将其转换为 HTML 页面。它使得网站更容易维护、更容易理解,而且,顺便说一句,可以在需要时以编程方式访问网站数据(加载 XML 而不是 HTML 页面)。另一方面,它极大地降低了性能,因此不适用于每秒数千次查询的大型网站。
True. But lot of PHP code is written by inexperienced developers. Further, PHP does not encourage, but rather discourage writing good code, and the ability to mix HTML with programming code is one of the examples of this.
If you expect to do high quality code, to be able to easily maintain and extend it, I highly recommend using template engine or something similar. Mixing both has a huge impact on the readability of your code, and will result in something becoming worse and worse over time. Refactoring will be painful, if not impossible.
Now, you have a large choice of the way to separate HTML from PHP
The most obvious one is to use an existing layout engine. There are plenty of them, some very well done and with a very low performance impact.
You can also write your own engine. It may not be a good idea on a big project (why reinvent the wheel?), but can be a solution either on a tiny project or when you need something very specific.
The last way I use in most projects is to build XML from business layer (XML serialization is quite easy in PHP), then to use XSLT to transform this to HTML page. It enables to do websites which are much easier to maintain and more easy to understand, and, by the way, enables to access the website data programmatically (loading XML instead of HTML page) when need. On the other hand, it decreases performance hugely, so is not intended for large websites with thousands of queries per second.
大多数代码都是从 PHP 和 HTML 字符串开始,然后在大规模重写过程中演变成模板系统。预先将内容与逻辑分离需要团队之间的设计、规划和沟通。使用 XML 或 JSON 而不是 PHP 数组作为数据格式使这变得更容易。
Most code starts with PHP and HTML strings, then evolves into a templating system during a big rewrite. Separating content from logic up front requires design, planning and communication among the team. Using XML or JSON instead of PHP arrays as the data format makes this easier.
您可以在 HTML 中混合 PHP,反之亦然,只需在 HTML 之间添加打开
和关闭
?>
PHP 标记即可。有关更多详细信息: https://stackhowto.com/how-to-combine -html-和-php/
you can mix up PHP inside HTML or vice versa simply by adding opening
<?php
and closing?>
PHP tags between your HTML.For more details : https://stackhowto.com/how-to-combine-html-and-php/