• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best Practice for conditional code and looping

#1
[eluser]Crazy88s[/eluser]
Hi,

Working on my first application using CI. What's the best practice in regards to conditional code and loops. Is it better to do it in the View or in the Model.

For example, in my model I have the following code:

Code:
function getSermonsForSideBar ()
    {
        $this->db->orderby('daterecorded desc');
        $query = $this->db->get('sermons');
        if ($query->num_rows() > 0) {
            $output = '<ul>';
            foreach ($query->result() as $sermon) {
                $output .= '<li><a href="/podcasts/'.$sermon->FileName.'">'.$sermon->Title.'</a></li>';
            }
            $output .= '</ul>';
            return $output;
        } else {
            return '<p>Sorry, no sermons on file.</p>';
        }
    }

And in the controller I have:

Code:
function index()
    {
        $data['SermonSideBarContent'] = $this->sermon->getSermonsForSideBar();
        $this->load->view('common\header');
        $this->load->view('common\index', $data);
        $this->load->view('common\footer');
    }

As you can see, I'm building the unorder list here in the model and passing the complete result back to the controller.

OR

In the view I could do:

Code:
&lt;?php
                if($query->num_rows() > 0):
                   foreach ($query->result() as $row):
            ?&gt;
            <ul>
                <li>
                     &lt;?php echo $row->Title; ?&gt;
                </li>
            </ul>
            &lt;?php
                endforeach;
                endif;
            ?&gt;

The view method seems kinda messy considering I could just put &lt;?= $SermonSideBarContent;?&gt; in the view and let the model handle the rest.

What's the best practice here?

Thanks!

#2
[eluser]xwero[/eluser]
Hardcoding html in your model isn't a good way to go. If you want to keep your view files clean you could do this
Code:
// model
function getSermonsForSideBar ()
    {
        $this->db->orderby('daterecorded desc');
        $query = $this->db->get('sermons');
        return $query->result();
    }
// controller
function index()
    {
        
        $this->load->view('common\header');
        $content = $this->sermon->getSermonsForSideBar();
        if(count($content) == 0)
        {
            $data['content'] = $this->load->view('nosermons','',true);
        }
        else
        {
          $data['content'] = $this->load->view('sermons',$content,true);
        }
        $this->load->view('common\index', $data);
        $this->load->view('common\footer');
    }
If you do this in the view file all the html code is there and you don't have to load microviews.

#3
[eluser]James Gifford[/eluser]
I think that a true MVC approach requires absolutely no view data within a model. The model should simply retrieve information from the data source and pass it to the controller. The controller then can then make whatever modifications are necessary to the data before passing it to the view. There's nothing wrong with having conditionals within the view, I'd just try to avoid any data manipulation at that point.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.