8

My web application produces HTML5 output as a concatenation of a variable number of views. The end result is a mess of indentation:

            </div>
        </div>

        <div id="content">

<div id="question-header">
    <h1>

I want to indent the code to obscure the origin of individual views and to make the output easier to follow.

I have looked into the Tidy PHP extension but all my attempts to make it work with HTML5 have produced improper indenting.

4
  • Why would you want to obscure the origin of views? (Just curious!) The only thing I would have suggested would have been Tidy executed at the final rendering/post-filtering stage; I guess it'll be updated for HTML5 as time goes by... Commented Feb 25, 2011 at 11:25
  • 1
    Now that's atypical for WebMVC frameworks in PHP, but the View could very well be an object structure still. Building a DOM tree instead of HTML string assembly is feasible, but don't know of any PHP framework that actually does that, and it would be an overkill solution for this purpose anyway. Commented Feb 25, 2011 at 11:36
  • @mario: Indeed. @Matt Gibson: Like I said it isn't critical, but if I can make my output indent correctly, why not? Commented Feb 25, 2011 at 12:33
  • you can if you create a domdocument from it, but php markup will make problems Commented Feb 25, 2011 at 12:37

2 Answers 2

7

If you want to obscure the origin of individual views, I suggest you to minify the HTML. This will have the added benefit of reducing the document size.

As for making the HTML output easier to follow, browsers come with debug utilities that parse and render out DOM tree in an indented format, e.g. https://trac.webkit.org/wiki/WebInspector, http://getfirebug.com/.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, Tought about that, and I'll probably move towards this solution if no real alternative presents.
I went with this approach, the only notable problem I ran into is the leading white space elimination in HTML elements such as <pre> or <code>.
2

The closest to what you are looking for in the PHP land is Dindent, https://github.com/gajus/dindent. Dindent is a HTML beautifier that uses regular expressions to indent the markup. This is different from Tidy, that acts as a DOM parser.

From the documentation:

There is a good reason not to use regular expression to parse HTML. However, DOM parser will rebuild the whole HTML document. It will add missing tags, close open block tags, or remove anything that's not a valid HTML. This is what Tidy does, DOM, etc. This behavior is undesirable when debugging HTML output. Regex based parser will not rebuild the document. Dindent will only add indentation, without otherwise affecting the markup.

Dindent sole purpose is to indent the HTML markup. It allows to configure what elements to treat as inline and what elements to treat as block.

1 Comment

There is no disclosure on this answer either. Please edit both answers to satisfy Stack Overflow requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.