2

Does using conditional blocks in my view defeat the purpose of the MVC architecture?

I have a controller method that loads a view and I want to have a different button group based on the page id that was passed as a paramater to that method.

Everything works fine but in my view I have several:

<?php if($pageID == n) : ?>
    html content...
<?php endif; ?>

Is this a bad habit and if so what's a better solution? Calling a library method from my view seems wrong as well.

tereško
  • 58,060
  • 25
  • 98
  • 150
Cameron Chapman
  • 796
  • 9
  • 19
  • It really depends on scale. If there's one or two IDs with different html, I'd leave it - if you have 15, it's time to think of a better solution. – Sam Dufel Sep 13 '12 at 20:05
  • I'm right around 15 but I would like to be scalable. I'm just sure there's a better way. – Cameron Chapman Sep 13 '12 at 20:27
  • With that many cases, maybe a database driven approach makes sense? – wallyk Sep 13 '12 at 20:28
  • There are several id's but only a few cases where I want to display something. I try to do the bulk of my work in the model but I was just getting a little spun around on this particular issue. – Cameron Chapman Sep 13 '12 at 20:32

2 Answers2

2

There are several ways to avoid that solution, but it really depends on the scale of issue.

A better solution could possibly be something like the follwoing.

View

<?php echo $conditional_html; ?>

Controller

switch($pageID)
{
  case 1:
        $data['conditional_html'] = $this->load->view('the_first_id_html', TRUE);
        break;
  case 4:
  case 7:
  case 8:
        $data['conditional_html'] = $this->load->view('some_special_html', TRUE);
        break;
  case 13:
  case 18:
        $data['conditional_html'] = $this->load->view('the_secret_menu_html', TRUE);
        break;
  default:
    $data['conditional_html'] = $this->load->view('the_default_html', TRUE);
}
$this->load->vars($data);

If you don't mind loading views within your views, another simpler way of doing it could be like this:

View

<?php $this->load->view($conditional_html); ?>

Controller

switch($pageID)
{
  case 1:
        $data['conditional_html'] = 'the_first_id_html';
        break;
  case 4:
  case 7:
  case 8:
        $data['conditional_html'] = 'some_special_html';
        break;
  case 13:
  case 18:
        $data['conditional_html'] = 'the_secret_menu_html';
        break;
  default:
    $data['conditional_html'] = 'the_default_html':
}
$this->load->vars($data);

In the end, you can do this in many different ways, but I hope this could shed some light on some alternative ways to do it.

Repox
  • 15,015
  • 8
  • 54
  • 79
  • I'll try loading the view within my view based on the value my controller passes to it from the switch. Thanks for the point in the right direction! – Cameron Chapman Sep 13 '12 at 20:30
  • 1
    @chapman84: I don't think that's what was meant. The controller is the usual view selector. That keeps the control and view separate. A smart viewer is, of course, possible. But that doesn't seem as clean. – wallyk Sep 13 '12 at 20:31
  • I might have worded that wrong. So you are saying the controller should do that work? – Cameron Chapman Sep 13 '12 at 20:35
1

No, not really. Views are supposed to contain the presentation logic in MVC design pattern.

The problems is, that in CodeIgniter you do not have real views only templates. It's related to the fact, that the original aim of CodeIgniter was to mimic RubyOnRails, instead of implementing MVC.

If someone forced me to use CodeIgniter, I would use something like this in the "view":

<?php

$pagelist = array(
   1 => 'foo/b',
   2 => 'foo/a',
   3 => 'foo/r',
   4 => 'lorem/ipsum'
);

$pageID = isset( $pagelist[ $pageId ]) ? $pageID : 1;

?>

<!DOCTYPE html>
<html>

    <!-- some stuff here -->
    <?php include '/path/to/views/' . $pagelist[ $pageId ]; ?>
    <!-- a bit more HTML -->

</html>

The $this->load->view() in (what CI calls) controller is basically just a glorified include/require. There is nothing magical about that method.

Also, you might find this interesting: http://codeigniter.com/forums/viewthread/62366/

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Thanks for the approach and the link. Ruby or Python is on of the next things I plan on experimenting with and sounds like I should try Rails out since I do like the idea of the MVC structure. – Cameron Chapman Sep 13 '12 at 21:36
  • @chapman84 You missed the point there. I was saying that RoR does not implement MVC. The ROR framework was created for fast prototyping (generation of throw-away code). That is why instead of proper model layer the opted for ActiveRecord-based ORM instances and the presentation layer was collapsed so that controller was merged with view and templates were rebranded as "views" to make up for missing part of MVC. – tereško Sep 13 '12 at 21:44
  • @chapman84 As result of this transformation the RoR framework is extremely good at what it was meant for - prototyping. But in real-world applications it suffers from anemic model and bloated controllers, which now have to deal with both presentation logic and leaks of domain business logic due to nature of ActiveRecord. – tereško Sep 13 '12 at 21:46
  • 1
    @chapman84 If you want to research MVC design pattern, then I would suggest to instead start by reading [GUI Architectures](http://martinfowler.com/eaaDev/uiArchs.html) by *Martin Fowler* and maybe reading [PoEAA](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420) book. Also reading/watching anything you can find from *Robert Cecil Martin* and *Miško Hevery* might give you some additional insight in MVC and OOP in general. – tereško Sep 13 '12 at 21:51
  • I was just going to ask your opinion on some good starting points as to get a better understanding of the MVC pattern. I've heard the general idea which appeals to me which pointed me towards the various frameworks (CodeIgniter, etc.). I don't plan on being strictly a PHP developer and would be well served to get a deeper understanding of MVC at it's base. Thanks for your help. I'll definitley check out your suggestions as well as some of your other posts on S.O. about the topic. (like http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000). – Cameron Chapman Sep 13 '12 at 21:57