简体   繁体   English

静态HTML或PHP生成的HTML?

[英]Static HTML or PHP generated HTML?

I've been reading a lot of PHP tutorials lately, and I keep seeing examples of "helper classes", which are mostly used to generate typical HTML components. 我最近一直在阅读很多PHP教程,我一直在看“辅助类”的例子,它们主要用于生成典型的HTML组件。 Forms seems to be popular. 表格似乎很受欢迎。

I can accept that this would probably make the code a little clearer, as I am all too familiar with the "klunky-ness" of meshing html with php. 我可以接受这可能会使代码更清晰,因为我对使用php进行网格化html的“klunky-ness”非常熟悉。 Is there any "other" reason? 有没有“其他”原因? Some kind of performance benefit? 某种表现有什么好处? I can see why it would be nice if your website was going to have 100 forms in various places, but if you only have one or two it seems like more work than is necessary. 我可以看到为什么如果你的网站在不同的地方有100个表格会很好,但是如果你只有一两个表格,那么它似乎比必要的工作更多。

Ease of use and rapid development come to mind. 我想到了易用性和快速发展。 It would actually be slower performance generating HTML with PHP, but I doubt it would ever become the bottleneck of your application (and you could cache most of the HTML output anyway). 使用PHP生成HTML实际上会慢一些,但我怀疑它会成为你的应用程序的瓶颈(你可以缓存大部分的HTML输出)。

For example, which of these looks easier to implement... 例如,哪些看起来更容易实现......

<?php 
 $name = 'abc';
 $options = array('a', 'b', 'c');
 $selected = 2;
?>

Example 1 例1

<?php echo form::select($name, $options, $selected); ?>

Example 2 例2

<select name="<?php echo $name; ?>">
<?php foreach($options as $value => $node): ?>
   <option
     value="<?php echo $value; ?>"
     <?php echo ($selected === $index) ? ' selected="selected"' : NULL; ?>
   >
   <?php echo $node; ?>
   </option>
<?php endforeach; ?>
</select>

I think you will find the first example much easier to read, less to type and guaranteed to work every time (so long as you coded it correctly). 我认为你会发现第一个例子更容易阅读,更少的类型和保证每次都工作(只要你正确编码)。


Also, if your boss says... 另外,如果你的老板说......

Hey zdkroot, we now need to make all password input elements have a red border in IE6. 嘿zdkroot,我们现在需要让所有密码input元素在IE6中都有一个红色边框。

...you might think to yourself... ......你可能会想到自己......

I know, I'll use input[type="password"] { ... } . 我知道,我会使用input[type="password"] { ... } Oh wait, IE6 doesn't support that? 哦等等,IE6不支持吗?

...so you can go into your input generating functions and add a class there which you can reference in your CSS. ...所以你可以进入你的input生成函数并在那里添加一个你可以在CSS中引用的class It will also make changing from /> to > a synch too (if you decide to). 它也会从/>更改为>同步(如果您决定)。

Consider the example you gave - a form builder. 考虑一下您给出的示例 - 表单构建器。

The thing with a form is that you need more than just the HTML. 表单的内容是您需要的不仅仅是HTML。 You also need to map those input fields to variables, you need to validate them, you need to save them to a database, you need to pre-populate them with data from the DB. 您还需要将这些输入字段映射到变量,您需要验证它们,您需要将它们保存到数据库,您需要使用来自数据库的数据预先填充它们。

A good form builder class will handle most of these. 一个好的表单构建器类将处理其中的大部分。 If you built the HTML code manually, you'd still be able to modify that easily enough, but you'd have to write your own validation and processing routines. 如果您手动构建HTML代码,您仍然可以轻松地修改它,但您必须编写自己的验证和处理例程。

That's the benefit of using a pre-built class. 这是使用预建类的好处。

There are down-sides as well, of course. 当然还有缺点。 Typically, a class like this will make it very easy to create a form that meets the design criteria of the people who created the class. 通常,像这样的类将使创建满足创建类的人员的设计标准的表单变得非常容易。 But if your requirements are slightly different to that, it can be quite tricky to force it to do what you want. 但是如果你的要求与那些略有不同,强迫它做你想做的事情可能会非常棘手。

Most of the time it does work well, though. 但大多数时候它确实运作良好。

For a selection of reasons why a form helper or library might be helpful you can checkout a question I recently posted A PHP and jQuery form creation and validation library available? 有关表单助手或库可能有用的原因的选择,您可以查看我最近发布的问题PHP和jQuery表单创建和验证库可用吗?

The often give you: 经常给你:

  • An easy way to validate 一种简单的验证方法
  • Sometimes client side validation is included 有时包括客户端验证
  • Form re-usability 形成可重用性
  • Faster speed of development 更快的发展速度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM