Is it OK to put conditional logic on CodeIgniter Views? [closed] Is it OK to put conditional logic on CodeIgniter Views? [closed] codeigniter codeigniter

Is it OK to put conditional logic on CodeIgniter Views? [closed]


In Codeigniter,you can put if and else statement in view.

if($this->session->userdata('your_session_variable')){    ?>    <a href="profile_page_link">Profile</a>    <?php}else{    ?>    <a href="login_page_link">Login</a>    <?php}


The controller is for calculating and manipulating data and passing the results to the view, and the view takes the results and render them to HTML.

If you should use if statement in the view to show or hide some markup, you are allowed!

but if the changing section contains a lot of information, I suggest using partial views and passing their content as a variable to the main view. And doing all these in controller.

To do that in CodeIgniter:

Controller:

class Foo extends CI_Controller {    public function bar()    {        // prevent getting error.        $data['partial'] = '';        // check if user is logged in.        if ($this->session->userdata('user_id') == TRUE) {            $data['partial'] = $this->load->view('partial/baz', '', TRUE);        } else {            $data['partial'] = $this->load->view('partial/qux', '', TRUE);        }        $this->load->view('my_view', $data);    }}

Assumption: user_id is set in CI session when user log in.

View:

<?php echo $partial; ?>


It's obviously possible to have logic embedded in a View, but the idea on a MVC is to have all the logic in the Controller, so I guess you'll be defeating the purpose of using Codeigniter. The views in general are used to render content passed on by the controller.