dthdlv9777 2010-02-20 13:42
浏览 43
已采纳

mVc世界中的HTML缩进[重复]

This question already has an answer here:

Here is a question that has been bugging me for a while, nowadays it's considered a good practice to indent HTML code but I've some reservations indenting code in a MVC pattern, here is a (silly) example:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<title>Testing MVC Indentation</title>
</head>

<body>

<?php View('h1', 'I am a Level 1 Header'); ?>

    <table>
        <tr>
            <td>
                <?php View('h1', 'I am a Level 1 Header Inside a Table'); ?>
            </td>
        </tr>
    </table>

</body>
</html>

To indent correctly, the first call to the h1 view (or partial) should return:

\t<h1>I am a Level 1 Header</h1>

While the second call to the h1 view should return:

\t\t\t\t<h1>I am a Level 1 Header Inside a Table</h1>

The h1 view however, has no idea of the indentation scope it's in, so how the hell can it return the data properly indented? Also, ignoring indentation in views can disclose part of the application logic (check the HTML source code of this page after <div id="content"> for a real-world example):

<body>

<h1>I am a Level 1 Header</h1>

    <table>
        <tr>
            <td>
<h1>I am a Level 1 Header Inside a Table</h1>
            </td>
        </tr>
    </table>

</body>

Not indenting at all solves all problems, but it also makes it harder to read and maintain:

<body>

<h1>I am a Level 1 Header</h1>

<table>
<tr>
<td>
<h1>I am a Level 1 Header Inside a Table</h1>
</td>
</tr>
</table>

</body>

The only feasible solution I see to this problem is by using Tidy and output buffering, but I wonder if it's worth the effort since it will make processing and loading unnecessarily (?) slow. Also, this will not make it easier the maintain the HTML code since it only indents the output, not the source.

I'm sorry for the "basic" question, but I've been focusing on business logic for the last years and I've been kinda disconnected with the presentation world - in the good old days my HTML code was all unindented, but then again I also used tables to design the layout - just trying to catch up now.

Any solutions / insights on this subject?


Related Questions:

</div>
  • 写回答

5条回答 默认 最新

  • doutui6241 2010-02-20 15:08
    关注

    As mentioned there is a difference between source code indentation and output indentation. The indentation of the source code can be different from the indentation of the output, because the output is constructed dynamically. When writing templates, the readability of the source code is most important. You should focus on that first! Usually the HTML indentation of the output is a non-issue.

    Having said that, two practical approaches to get rid of indentation mismatch in your output:

    Use a templating language that can properly indent the output

    One example of the first is Haml, a templating language for Ruby. It takes a template like this:

    %body
      %h1 I am a Level 1 Header
      %table
        %tr
          %td
            %h1 I am a Level 1 Header Inside a Table
    

    The output will be neatly formatted HTML similar to your example, all properly indented.

    One obvious disadvantage of this approach is that you're not really writing HTML in your templates, you're writing in a different language with HTML-like semantics. This can be a problem, depending on who will maintain the views.

    Strip all superfluous whitespace from the output, don't indent at all

    Some template languages have options to remove all superfluous whitespace. Smarty for PHP has a whitespace trimming plugin that does this job nicely, for example. It completely works around the output beautification problem by purposely making all output equally non-indented. It also saves a very marginal amount of bandwidth.

    A PHP-only solution would be to use ob_start() with its $output_callback handler (which is documented here). You can write a simple callback that strips the excessive whitespace, similar to the Smarty plugin. Contrary to using Tidy in the output callback, this will still allow you to flush the buffer before the end of the page to speed up long/slow pages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型