cabita
cabita

Reputation: 852

Using HTML instead of CodeIgniter helpers in views

In the views in codeigniter, we can write code for forms using codeigniter. For example for an url the code in codeigniter is:

<?php
anchor('site/myfunction','Send');
?>

My question is whether is better write this code with html in the views:

<a href="site/myfunction">Send </a>

It's an example, but the question is with all HTML helpers for views. CodeIgniter's user guide suggests to use PHP functions rather than code html. The php code requests to the server while html does not. Is better use the CodeIgniter for HTML? I don't know if when I use CodeIgniter's helpers, the framework has contemplated these requests.

I apologize for my english. Thanks for your answer.

Upvotes: 2

Views: 793

Answers (2)

MikeMurko
MikeMurko

Reputation: 2233

The reason you want to use CodeIgniters library is for the ability to quickly modify your HTML elements site-wide with very little work. For instance, let's say you wanted all <a> tags on your site to have a class added called "ajax". Using the anchor helper, you can accomplish this easily.

That said, I don't really foresee many solutions where you will be changing HTML elements site-wide. With semantic HTML, CSS, and Javascript I think you will be perfectly fine without having to use CodeIgniters HTML helpers. Also in my opinion your code will be much more readable. Use HTML.

Regarding performance

When you say "code php does requests to the server while html, no" you're wrong because whenever someone visits your site they are requesting the server. The question here is how much work the PHP engine is doing versus just your normal webserver. In this case, a function call is trivial for PHP and shouldn't be considered performance wise.

Regarding urls

The answer by Pi is focused on the fact that URL resolution in CodeIgniter can be weird, but with proper .htaccess or web.config configurations you should be able to use vanilla hrefs without using CodeIgniter functions.

Upvotes: 5

PiTheNumber
PiTheNumber

Reputation: 23542

You should not use

<a href="site/myfunction">Send </a>

Because it might not work everywhere. But if you use this:

<a href="<?= site_url('site/myfunction') ?>">Send </a>

There will be no big difference. Pure HTML is a bit faster but unless you have a high traffic website that does not matter. Using the CI function is nice if you are in a library because you do not want to mix PHP and HTML to much to keep up the Model-View-Controller concept. What you use in a view is a matter of style what you like more.

Personally I think the codeigniter form functions are not very good and I am often use html instead.

Upvotes: 2

Related Questions