1

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.

Darko
  • 355
  • 3
  • 17
  • In MVC views are not templates. Views actually should be classes, responsible for presentational logic. And they should be juggling multiple templates. Also, such HTML helpers are useless. – tereško Jun 10 '12 at 11:34
  • Can you give me some good reference on views classes? – Darko Jun 12 '12 at 12:12
  • You might find [this article](http://r.je/mvc-php-front-controller.html) useful. Just don't take ANY article on internet as gospel. For example, in this article has really well explained some concepts, **but** the "model" is way too simplified. – tereško Jun 12 '12 at 12:18
  • you might find [this answer](http://stackoverflow.com/a/16596704/727208) relevant to the subject. – tereško May 30 '13 at 07:42

4 Answers4

2

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.

Dvir
  • 5,049
  • 1
  • 23
  • 32
  • Excatly! That's my point. It's not that I find it difficult, but it's just ugly as hell. Thank you for your opinion. – Darko Jun 05 '12 at 16:44
1

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.

Anwar
  • 672
  • 4
  • 16
  • Well, I'm not saying that it's complex for me. I understand it very well, it's pretty straightforward. My point was is that actually needed? And that's why I mentioned the performance issue... – Darko Jun 05 '12 at 16:33
1

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.

Arjun Abhynav
  • 543
  • 4
  • 15
  • I think the same about the performance. Do you use html helper yourself? Do you find it usefull sometimes? – Darko Jun 05 '12 at 16:36
  • @Darko I have used it, but I wouldn't say that I use it. It is useful in some cases (mainly forms), when you need to trigger some asynchronous AJAX events (using helper methods), and there it reduces the complexity of the code. Also these frameworks are meant to be user-friendly to ones who can think object-oriented but who are not seasoned developers with knowledge of HTML, etc.. I believe it is more to facilitate abstraction of the HTML code implementation. Anyways, never mind. This is good :) – Arjun Abhynav Jun 05 '12 at 17:44
0

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.

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • So, you want to say that it's not going to make a big difference on the performance side? How about you, do you actually use html helper in your projects? – Darko Jun 05 '12 at 16:35
  • @Darko: It will make a difference in that using the helper will be slower. However the difference will be very, very tiny to the point where I would call it a micro-optimization. I use helpers for some elements such as anchor tags and form fields since the helper normalizes the URL etc. For headings etc., I find it easier to just use vanilla HTML. – Ayush Jun 05 '12 at 16:37