almo
almo

Reputation: 6367

What's the advantage of Helpers in CodeIgniter?

I am starting to learn CodeIgniter. I see some advantages e.g. MVC conventions. But in many cases, I can't see how CI helps me to develop faster.

For example, this

$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);

can be used instead of

<form method="post" 
      accept-charset="utf-8" 
      action="http:/example.com/index.php/email/send"  
      class="email"  
      id="myform" />

I don't see why this should be faster.

Also, there are HTML helpers to create <h1> tags. I don't see the advantage of using a helper here either.

So why should I use these kinds of helpers?

Upvotes: 4

Views: 915

Answers (4)

Jad Joubran
Jad Joubran

Reputation: 2579

One great use case of form helpers are dropdown (select, option) because you'll be able to pass the default value from a variable, without looping through the options and setting 'selected="selected"'

Upvotes: 0

cartalot
cartalot

Reputation: 3148

ha its funny i had the exact same reaction. but even that form open helps.

  • in your example, the form open allows you to specify the code, without having to use or mix in any HTML. cleaner to write and review. less errors.

  • same idea for the different types of form fields. think about if you are passing values from a database - into the form fields. Like a user record that is getting updated. unless you use the form helper its going to be a hideous tangle of php & html & echo statements - for every single form field.

  • the real payoff? especially when you are dealing with multiple forms - you can specify the values for the form fields - in a config file - and then make it available to view. then everything important is in a simple array and you barely have to touch the view.

Upvotes: 0

Technoh
Technoh

Reputation: 1600

I agree there are some helpers that are pretty much useless because some take more code than writing the HTML result they would output. But there are other helpers that make life much easier, url being the first one that comes to mind.

Moreover the helper structure is very useful. If you ever find yourself formatting data the same way over and over again in your views you should think about creating (or extending) a helper and using it instead. While not all helpers are useful, having the possibility of adding to them comes in very handy.

Upvotes: 1

Gabriel C. Troia
Gabriel C. Troia

Reputation: 3330

The helpers are there to make your life easier and even to make you write cleaner code, sometimes at the cost of flexibility.

In your example, there's no major improvement - be it performance wise or code length wise - but that's not all the CodeIgniter helpers are meant to do.

Personally, I don't like the Framework to write the HTML for me, but I want it to help me, compute some data. Think of them as static methods, which can be accessed globally from your controllers, models or even views if you need to. You send data to them as a param, they process it and return the result.

Some Codeigniter Helpers which I cannot live without are the 'url' and 'date' .

Upvotes: 1

Related Questions