Hello everyone.
Can anyone point me in the direction of a discussion on indentation techniques for mixed PHP and HTML?

My code is mostly pretty good- the PHP and HTML source is nicely indented and the generated page is also fairly nicely indented, but there are a few major irritations, which only become worse as the complexity of the code increases (see last section of this post).

What would be good is if I can somehow define rules for PHP to follow regarding indentation on the output page. In particular I would be able to make it look as if I'd typed out the HTML myself if I could just get it to keep the same level of indentation. Given the following ("--->" represents one level of indentation):

<?php
function print_form() {
--->for ([stuff]) {
--->--->if (something) { ?>
--->--->---><input name="something">
--->--->---><input name="something else">
--->---><?php }
--->}
}
?>
<div>
---><form action="">
--->---><?php print_form();?>
---></form>
</div>

This produces:

<div>
---><form action="">
--->--->--->--->---><input name="something">
--->--->---><input name="something else">
--->--->---><input name="something">
--->--->---><input name="something else">
---></form>
</div>

What we want is both inputs to be at 2 levels of indentation. But instead they both take on 3 levels of indentation from the nesting inside the function, for loop and if statement, and what's worse the first one also takes on an extra 2 levels of indentation from where the function was actually called.

I cannot presently see any sane workarounds for the nesting problem.
The solution (this would fix both problems) is actually pretty simple: When the parser enters PHP mode, it should remember what level of indentation the <?php was at, and offset the indents of all printed lines to make the minimum level of printed indentation equal to that (remembering that HTML may be nested inside).

Currently, the only sensible solution to the offset first line is to make the function call appear at indentation level 0, like so:

<div>
---><form action="">
<?php print_form();?>
---></form>
</div>

(You could put the <?php straight after the <form> tag, but I don't think that's sensible).

That's not really too much of a problem for a simple print call, but with anything remotely more complex it starts to become very messy, especially if, as I do, you have lots of short sections of HTML in functions rather than doing includes for all of them.

Also (and this is the real reason for posting in the first place), how would you format the following section of code?

class Select extends OptionField
{
	public function printField() {
?>
		<select name="<?php print $this->requestName(); ?>">
<?php		foreach ($this->options as $value => $text) { ?>
			<option value="<?php print $value; ?>"<?php
			if ($value == $this->getValue()) {
				?> selected="selected"<?php
			}
			?>><?php print $text; ?></option>
<?php	} ?>
		</select>
<?php
	}
}

Most of the problem here is the W3C's silly checked="checked" thing, where if I want it to be unchecked I can't specify checked="false"; the checked attribute simple can't be there.

Thanks in advance.
Simon

    If the [man]Tidy[/man] extension is available, you can use it to format your output, something like:

    <?php
    ob_start(); // start output buffering
    // your PHP script
    $html = ob_get_clean(); // save all output to variable
    // Tidy stuff:
    $config = array('indent' => TRUE,
                    'output-xhtml' => TRUE,
                    'wrap' => 200);
    $tidy = tidy_parse_string($buffer, $config, 'UTF8');
    $tidy->cleanRepair();
    echo $tidy;
    ?>
    

      Is it possible to add tidy to PHP5 without recompiling the whole thing?
      In any case, it seems that tidy is almost always not available and I'd prefer to avoid relying on it.

        Write a Reply...