Welcome Guest, Not a member yet? Register   Sign In
HTML in Helpers?

Hello all!

I've been playing with Codeigniter for some time and I ran into some questions...

For example: Creating a navigation menu (UL > LI) on my site >
I found a tutorial on a site which explained to put the complete menu in a variable and send it to the view.
Is this ok? Because I always thought you had to put most of your HTML in your view instead of helpers.

I now did the following in my Menu helper:
Put each anchor in an array and send it back to the view using the main controller. Then I use a foreach in the view to generate the <li> items with the anchors in them.

Generating the complete menu in the helper cleans up the view a lot, but when I want to change the layout I will have to edit the view AND the helpers...

Now on to a bigger problem... I also have to generate 3 tables with orders. New, Paid and Sent orders. All 3 tables are the same except the content and the paid and sent status.

I already created the model for it which has the following function to get the database content:
function getNew() {
    return $data = $this->_getOrders(0);
function getPaid() {
    return $data = $this->_getOrders(1);
function getSent() {
    return $data = $this->_getOrders(2);

Again I use this data in the view using a foreach loop. But now my view ended up with 3 foreach loops and 3 times the same table html code. Which looks really messy. Just because I try to keep the HTML separate from the PHP code according to the MVC standard.

Can anyone tell me what's ok and what's not?

I think keeping the HTML separate from the logic (business logic) based code is ideal, meaning you wouldn't want a function that gets data from a DB and at the same time write out HTML.

Functions that return objects and arrays are always much more useful, because you can then reuse those same functions for different things/views.

When it comes to PHP in views, I'm flexible, I usually use structural and formatting functions in the views (foreach, while, nl2br, etc) ... it's sometimes just easier to use PHP in your views, but I try to keep the PHP really plain vanilla, no heavy logic.

However, functions that return HTML, I think are fine too, consider this ... your functions in your helper can load view files and return them. Again you keep logic away from the HTML views/templates.

Also consider using "includes" with PHP, if you find yourself using a section of code more than once, put it into an include and use it in multiple locations.

Also take a look the the CI template parser, might be of interest/use ...

Theme © iAndrew 2016 - Forum software by © MyBB