Hi there,
I would like to hear your opinion about what is a better approach / best practice for mixing HTML and PHP code in a view, for example for building a form?
Here we have approach one, taken from Yii startup app, therefore probably promoted by Yii Dev Team:
<div class="row">
<?php echo $form->labelEx2($model, 'MAIL'); ?>
<?php echo $form->textField($model, 'MAIL', array('size'=>49, 'maxlength'=>49)); ?>
</div>
And this is second approach - generation of exactly the same part of form, done my way:
<?php
echo('<div class="row">');
echo($form->labelEx2($model, 'MAIL'));
echo($form->textField($model, 'MAIL', array('size'=>49, 'maxlength'=>49)));
echo('</div>');
?>
Which one is better in your opinion.
I strongly prefer second one! Why? Well, there is one, simple, but not so obvious advantage of it. If I need to temporarily turn off execution of that code part I simply comment-out with // each line (in NetBeans I do it within less then second by selecting whole part and pressing Ctrl+Shift+C). While, in first approach I have to manually comment out each PHP line with // (inside <?php ?>, as it wouldn’t be complicated enough) and each HTML line with <!-- -->. For me, a huge waste of time.
It would work smudgily, if I could comment-out with <!-- --> the whole <?php ?> block. But it doesn’t have any effect (at least in my server / PHP configuration) as PHP seems to be ignoring <!-- --> signs and tries to execute code between them (which is some kind of stupidity for me, or maybe <!-- --> were developed only to comment out static HTML code and this is normal).
What do you think about this?