Darko
Darko

Reputation: 375

PHP MVC - handling views

I've found a framework that handles the views in a different way than just plain html. To be honest, I've no idea how are the big frameworks handling this issue because I've mostly worked with some tiny frameworks, so that's why it was an interesting thing for me. A quick example:

Instead of writing

<h2>Click on this <a href="www.example.com">example</a></h2>

You'd have to type

echo $html->h2('Click on this' . $html->link('www.example.com', 'example'));

So, my question is which one is better? Isn't plain html going to be faster? And let's say more understandable for others? Or let's leave the understandable part away, I want to know more about the performance and maybe other stuff that people know and can be mentioned on this issue.

Upvotes: 1

Views: 920

Answers (4)

dvir
dvir

Reputation: 5115

As everything in the programming world, it's a matter of taste.

Though, I'm strongly against it, for a few reasons:

  • It makes writing your views harder for non-PHP-programmers.
  • Annoying to debug later on
  • I like to see my views the same way as the browser will.
  • Adds a lot of overhead to pretty simple stuff
  • Looses the pretty (and logical) HTML indentation
  • UGLY!

HTML issues are easy to spot anyways, if you are indenting your HTML markup correctly. So for me - plain HTML all the way.

Though, some view helpers are great - creating links, handling element classes, validation, etc. In my opinion, try to find the sweet spot - don't clutter your view with too much PHP, but don't give up on making sure your code is as dynamic as it could be.

Upvotes: 2

Arjun Abhynav
Arjun Abhynav

Reputation: 543

I have personally worked with the Yii PHP Framework, and even that has its way of handling HTML views(CHtml), though it was my choice to use either. The interpretation that rendering plain HTML is faster than writing it in some code which is interpreted by the framework and then generated as HTML make sense, and as a matter of fact, I do think plain HTML is faster.

However this allows better flexibility in creation of dynamic HTML content, like form support methods (validation, etc..), and other helper methods. Also I think this has evolved more from the Object Oriented Programming obsession.

But it'll only be a matter of time before you get used to it. And if you think about performance, think that it isn't really going to make a difference.

Upvotes: 1

Anwar
Anwar

Reputation: 672

You just cant say so... Usually, MVC framework divides codes into modules which makes the site clean by separating database actions, logical actions and views. There are also other cases like sending mails, paginations, image processing and manipulations etc which are complex tasks in plain programming and can be done in few lines in MVC because every thing is predefined. The example you defined above is only to follow the pattern but you can use it other way too.. So, it an alternate. You can use it either way. so, whenever you find that in certain cases the MVC could be complex, you can do that in your own way. So, I guess its more flexible by giving us the option of both ways.

Hope that answers your question.

Upvotes: 1

Ayush
Ayush

Reputation: 42450

CodeIgniter is one of the frameworks that provides an HTML helper to allow you to generate HTML through PHP code (like the latter case in your question).

While this will be marginally slower, the advantage is it ensures valid HTML is output. Take this example:

<h2>This is a header without a closing tag

The above is a common mistake which will fail silently. However, you cannot make the same mistake if you use the HTML helper function

echo heading('This is a header',2);

As for readability, anyone familiar with PHP should be able to understand either code fragment with little to no difficulty.

Upvotes: 0

Related Questions