I’m working on a project using CodeIgniter. Surely, it’s very useful to have an helper like language as the string you’re going to print can change depending on the language you loaded. So, instead of writing pure php code, you can use the helper.
However, it’s not clear to me which is the sense of helper function like:
echo doctype();
that expands to:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Of course, the first one it’s easier to write, but that code has to be processed every time. You save few seconds of coding at the cost of infinite executions of the same code that has always the same output on each page (and there are many helper like that one, for example the Form Helper is plenty of valid examples where the functions produce static HTML code).
So, the question is: why should I use an helper if I don’t have any dynamic code/variable to add?
For some things it might not make sense. Possible excuse for
doctype()would be if you wanted to change your doctype for several pages, you could do it by changing the output of the function. Personally I use a single master template with the doctype, but different people will do things differently.Same excuse applies to something like
heading('My Title', 1), some day you might need to change the output from<h1>My Title</h1>to<h1><span>My Title</span></h1>, and if you had used the heading function throughout your application, it would be trivial. You could even add extra parameters to make it more flexible.Of course as you surely know, do whatever works best for you for the given situation. Sometimes HTML helpers can save you time, other times they will just get in the way. I wouldn’t worry too much about how long it takes to execute, because it is hardly significant.