Welcome Guest, Not a member yet? Register   Sign In
Confusing the MVC model with some libraries
#1

[eluser]parham90[/eluser]
Hello everyone,

I have known CodeIgniter for a few days, and I find it lovely! However, because of my lack of knowledge when it comes to the MVC model, I have gotten a question.

It is said in various places in the tutorial that loading libraries in a view file is not a good idea. However, there are libraries that create output, such as the table library, or the calendar library. So, how should I use these? Should I load them in the view? If not so, how can I show the table through the view file, as that seems to be the correct option?

Thanks!
#2

[eluser]Jan_1[/eluser]
A question of structur - its one of these MVC-Things... If you place it in the controller you are able to use different views without loading that library in every view.
Or do I misunderstand your question?
http://en.wikipedia.org/wiki/Model–view–controller
#3

[eluser]parham90[/eluser]
It is likely that I have written my question in the wrong way. What I meant to say, is that it is said in the tutorial that you do not program in views, so usually libraries and stuff are not loaded in them. Also, you do not echo stuff from the controller; you pass them to your views, and let them take care of it. All fine so far. However, what can I do when a library outputs something? Where should I put it? It wouldn't be in the right place when I put it in the controller, since it would output something, and I have no way of putting it in a particular place on the loaded page. However, if I put it in the view, I can exactly say where to put the table, but I have to, say, connect to a database with a view, which I think would not be a good practice at all.

Thanks.
#4

[eluser]The Hamburgler[/eluser]
Taking the table library example you have two ways of generating the table.

You should load your libraries as well as another interactions like $this->table->add_row() within your controller.

The common method is then to return your generated table into a varaible that can be passed into your view

e.g. in your controller
Code:
$this->load->library('table');

$this->table->add_row('Fred', 'Blue', 'Small');
$this->table->add_row('Mary', 'Red', 'Large');
$this->table->add_row('John', 'Green', 'Medium');

$data['my_table'] = $this->table->generate();

$this->load->view('my_view', $data);

You can then echo out the table code in you view with
Code:
<?=$my_table?>

An alternative approach would be to move the generate call into your view, as you can still refer to instances of the controllers global variables ($this->table).

So the following code would echo out the table in your view
Code:
<?=$this->table->generate()?>

I'm not sure what the purists think of this but as long as you are not altering the library with your view code I think its ok.
#5

[eluser]parham90[/eluser]
Ah. Wonderful. So the $this->table->generate() or $this->calendar->generate() do not actually output things. You have to pass them and then echo them in your views, or just generate and echo them in your view. Thanks a bunch!
#6

[eluser]jedd[/eluser]
[quote author="parham90" date="1260577747"]
Ah. Wonderful. So the $this->table->generate() or $this->calendar->generate() do not actually output things. You have to pass them and then echo them in your views, or just generate and echo them in your view. Thanks a bunch!
[/quote]

You can capture the output of things - that's the important thing - and later echo these very long HTML strings out where you want them (ie, in a default.php view file, that contains the main html tags, and a few echos of the various bits and bobs you've accumulated on your way).

What I really wanted to say here is that the Table Class could be argued to break the MVC paradigm, by generating markup (HTML) within the controller. This is one of the two common reasons that some people avoid using the CI Table Class - the other is that once you try to do something a little out of the ordinary, it becomes a bit painful - and it's usually just easier to write up your own table view partial.
#7

[eluser]parham90[/eluser]
Well, I was thinking instead of writing a foreach in a view over and over for pulling data out of a table, I could just do it in two very simple lines. Well, three. Smile

However, yes, you are right. If things would be a bit less ordinary, I would rather not use functions to generate a table. I am more comfortable typing the tags and things.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB