views:

92

answers:

1

I wonder if anyone could please help me I have been using HTML tidy and eclipses built-in function to tidy up my code. I am having great trouble with the following situations...

  1. when HTML is split between files via includes, having result structured with correct indentations helps with debugging via browser tools.

  2. PHP and HTML when used together. for example PHP if statements around HTML code where you wont the correct indentation for both the PHP and HTML. (automating this http://stackoverflow.com/questions/1155799/how-to-properly-indent-php-html-mixed-code/1155811#1155811)

Situation one i can live with and there are ways around it. However, I would be grateful if anyone could offer a solution around situation two.

Tools I use eclipse 3.6, Aptanna 2.05 PDT 2.2

+2  A: 

You could use HTML Tidy from within PHP to clean up your output. Use ob_start() and friends to get the whole HTML output as a string, then send it through Tidy. You might want to use som sort of caching if you do this, though.

<?php

    function callback($buffer)
    {
        // Clean up

        $config = array(
            'indent'         => true,
            'output-xhtml'   => true,
            'wrap'           => 200);

        return tidy_repair_string($buffer, $config, 'utf8');
    }


    // Do some output.

    ob_start("callback");
    ?>
        <html>
            <body>
                <p>Outputting stuff here</p>
                <p>
                    Testing a broken tag:
                    <span> This span should be closed by Tidy.
                </p>
            </body>
        </html>
    <?php
    ob_end_flush();

?>
geon
Thanks that's a really nice solution for dynamic output. But what about embedded PHP and HTML in source? for example...<?php if (statement) : ?> <div> <p> This is an example. </p> </div><?php endif; ?>HTML tidy fails to indent PHP in this situation correctly, I believe.
andicrook
reposted example http://pastebin.com/c9Z55cvN
andicrook
That's the point of the output buffering with callback. You buffer up the entirety of the script's out put and then run it through Tidy as the LAST thing done before sending it to the client. At that point the entire page has been generated so you get the whole thing nicely formatted.
Marc B
Sorry to be a bit slow. So do you mean run the code with HTMLtidy callback grab the HTML output and then paste formatted HTML back into the formatted PHP code? .. Therefore both HTML and PHP appear formatted correctly in the source and the output.
andicrook
This is what i am trying to automate for 2.http://stackoverflow.com/questions/1155799/how-to-properly-indent-php-html-mixed-code/1155811#1155811
andicrook