0

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?

MikeMurko
  • 2,214
  • 1
  • 27
  • 56
  • When you choose this spaghetti-style coding, the code will be unmaintainable in a short time. Anyways, use templates, it will save you time. – deejayy Nov 07 '11 at 17:27
  • I agree completely. Unfortunately the code is in legacy ASP so I'm not afforded a lot of the niceties. MVC frameworks on all new projects however. – MikeMurko Nov 07 '11 at 18:47

3 Answers3

0

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.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
0

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.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

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.

Polynomial
  • 27,674
  • 12
  • 80
  • 107
  • Though all answers said pretty much the same thing. First case is better, and that's what I had felt as well. – MikeMurko Nov 16 '11 at 21:06
  • The other solution is templates. You can make a bunch of HTML markup with certain identifiers (e.g. `%_PAGETITLE_%`) and have those tokens replaced appropriately, then echo out the templates. That way your logic and markup are separated in your code and you get the best of both worlds in terms of readable style. – Polynomial Nov 17 '11 at 09:32
  • How would you handle logic in template? Or would you have the logic back in the scripting side and then load certain templates based on the logic. What if it's just one single line you want different, make a whole new template? – MikeMurko Nov 17 '11 at 17:11
  • It would probably be a case of having *most* of your code templated, then writing the rest in with your logic using output buffering. Obviously not the cleanest solution, but it'd do the job. – Polynomial Nov 18 '11 at 09:26