tags:

views:

71

answers:

4

Hello I have always been thinking which is the best way of writing html in an MVC architecture.Would it be regular html as <div id="id1"> <input type="text" name="tBox" /></div> or echo '<div id="id1"> <input type="text" name="tBox" /></div>';. This is a very small example, but sometimes we can have a single variable or single echo statement having multiple lines of code.

Would we have any performance issues since php has to be parse by the webserver (apache,iis etc), while HTML gets away and is rendered directly by the browser. So would we gain any parsing time by using regular HTML?

+1  A: 

Regular straight HTML is preferable as it can be served statically without processing by PHP. That said however, if you've got an MVC architecture, then you shouldn't be echo'ing HTML in the first place -- that output should be handled by your view layer.

Alex Howansky
In a view layer we still can have place holder variables printing text coming from a datastore(ex: database). Though no echo statements are seen in my controller files, I would still have echo statements in my view layer, and since those are PHP variables, I would have to echo them on the view.
macha
Echoing the contents of a variable and echoing static HTML are two completely separate things. Obviously, you need to echo variables. (Or do some string manipulation to embed them.) But the whole point to using a view layer/templating system is to avoid doing exactly what you're describing.
Alex Howansky
A: 

It doesn't matter at all when the file is a php file (and contains php code somewhere). The server executes it regardless of what's inside and pure plain text is returned the way it is and text that is echoed out is added in between (at the echo location). In the end a coherent plain text is produced and sent to the client (with the correct mime-type) which then makes the browser interpret it.

I usually put in longer blocks of static html outside of <?php .. ?> tags to make it more clear, but as soon as there is some logic involved, I echo everything out, to not break the php flow all the way.

But in the end, it is totally up to you.

poke
why not to divide PHP flow into 2 parts? Data preparation part with no output, and output part, which involves only output logic?
Col. Shrapnel
@Col. Shrapnel: Oh, I didn't mean to put data preparation or similar in there. What I was referring to what more like for example outputting repeated blocks from a for loop where in every line multiple values come directly from the php data (for example think of some table with lots of data - it's much easier to look through the code when you have the php as the main code there instead of having multiple *controlling* php code as inline php code between html).
poke
Not too many developers will agree with you.
Col. Shrapnel
A: 

I agree with Alex. You should keep your data and display layers separate. A popular solution would be to use a template system like Smarty (http://www.smarty.net). We use it at my work. Some people like it and some don't; you can decide that for yourself.

Cavachon
A: 

A static html has a higher performance than embedded html on echo or variables 'cause the html isnt proccessed by the server-side. So a useful implementation is to retrieve only necesary data vars, database rows, iterations, etc from the server-side because everything inside is gonna be proccessed by the php service.

Alejandro
That happens with nowdoc too? http://cl.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
Tae
Of course that implementation behaves like nowdoc... I mean they are for php embedded purposes... There's no php parsing...
Alejandro
So static HTML is more fast than newdoc, and newdoc more fast than echo? Or newdoc is as fast as static HTML? Thanks in advance!
Tae
I dont know about the performance between pure static html declaration, nowdoc or html echoing. But for example is more easier create a sql statement with heredoc than use for example $query = "select * from employees where code = " + $cod; So it depends on what you need
Alejandro