Question

More of a stylistic question really, but how do you mix HTML tabs with scripting logic tabs in your source code? That might not be the best way to state the question, but I think a simple example will demonstrate it well. Note the tabbing:

<table>
    <tr><td>This is the first row</td></tr>
    <? if ($test == true) { ?>
        <tr><td>Only display the second row if some test is true</td></tr>
    <? } ?>
</table>

vs

<table>
    <tr><td>This is the first row</td></tr>
    <? if ($test == true) { ?>
    <tr><td>Only display the second row if some test is true</td></tr>
    <? } ?>
</table>

I know in PHP you can be crafty with the if syntax and do "if(something):" and then do an "endif;" later. But it amounts to the same thing.

Should your code tab based on the HTML or based on the logic conditions. I ask this because I am working on a project (not in PHP, but similar scripting language) where there is no MVC, so there are loads of logic mixed in with the HTML and it can get quite messy in terms of the tabs. If you tab based on both your code ends up too far from the left-margin. Are there any standards to follow here?

Was it helpful?

Solution

There's no set standard, but I'd say that properly tabbed PHP code files is more important than properly tabbed HTML. You're going to be spending significantly more development time reading the PHP than the HTML source.

The other solution is to use buffered output with ob_start() and have the HTML code "prettified" before it gets written out to the page, so you get both. But that might make your code more confusing, so it's probably not worth the effort.

OTHER TIPS

What do you find easier to read? What do you expect others will also find easier to read?

Those are the two main questions that spring to mind whilst reading your question.

For me, the first case is much clearer.

If you come back in 6 months time, what would be the easier to read and update if necessary?

Personally I would go for option 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top