Mixing HTML and PHP in a view: Best approach

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?

I am personally prefer first approach. I think a view should contain as much native HTML as possible, because it is much easier to read and change it, especially for non-programmer (css designer, for example).

The problem with disabling the view code can be solved with other methods.

  1. Disable all view code:



<?php return; ?>

<div class="row">

        <?php echo $form->labelEx2($model, 'MAIL'); ?>

        <?php echo $form->textField($model, 'MAIL', array('size'=>49, 'maxlength'=>49)); ?>

</div>



  1. Disable part of view



<?php if (0): ?>

<div class="row">

        <?php echo $form->labelEx2($model, 'MAIL'); ?>

        <?php echo $form->textField($model, 'MAIL', array('size'=>49, 'maxlength'=>49)); ?>

</div>

<?php endif; ?>

//other code here



Thanks for your ideas!

That is very important point, especially in MVC world!

Good idea, but impossible to be used, if we are talking about disabling part of the code, inside file, where return is impossible due to other code parts (below disabled) that should be executed.

Also interesting approach, but in the matter of coding time / speed this acquires as much time as commenting out line-by-line and might be hard to find after a few days.

Does anyone know what is the reason beyond idea that PHP ignores <!-- --> comments signs and tries to execute code between them while regular browsers seems to be complying them and treats anything between as it would not exist (and as it is supposed to). Is there any way to turn this off? As possibility of commenting out whole blocks of PHP code by surrounding <?php ?>with <!-- --> would be the fastest way here.

Just surround it with this:


<?php /*

code here...

*/ ?>

This solve only problem with commenting out PHP parts. See my initial post. In my second approach proposed there I can comment out with one click whole block - i.e. PHP and HTML parts. If I use, what you are proposing I’ll get HTML parts still rendered.

In example I gave this is not a problem, but in other, it could be.

And, again, is there a way to force PHP not to execute code inside <!-- -->?

I was using the second option (outputing all from PHP)… but later read some articles… and made some tests… and actualy the first option is faster than the secon one… so for the last year I’m using the first option… and now when i look at my old code it’s like OH MY GOOD how have I managed to write a code like that :)

What do you mean?

You can use php comments in large blocks of view code:


<?php /*

<div class="row">

        <?php echo $form->labelEx2($model, 'MAIL'); ?>

        <?php echo $form->textField($model, 'MAIL', array('size'=>49, 'maxlength'=>49)); ?>

</div>

*/ ?>

Personally, I prefer approach one, not php spaghetti code, in the views.

The goal is to keep the php code blocks truly minimal.

Now I got your point! :) Yes, this is indeed the beat approach! Thank you!

Another great advantage is to have the html highlit sintax in view file.

Take a loot at this wiki about where to put wich code. In theory the view should not contain any "operational code", no insert/update/delete, nothing that changes status of the application. Following this rule you are free to comment part of view or the entire view itself if you want.

Create many subview if you have big view, this will allow you to comment the renderPartial and exclude part of views in a easy way, if needed.

Yes, that is a great idea! Thanks! I haven’t thought about it. This will not be in my current project, as it is too small, but for future one it is at least worth considering. Just like the Gii CRUD generator, which uses _form.php and _search.php subviews for reusabillity purpose.