Welcome Guest, Not a member yet? Register   Sign In
Question about the template logic (Phil's template library)
#1

[eluser]victorche[/eluser]
Phil's template library is working fine for me (http://philsturgeon.co.uk/code/codeigniter-template)
But until now I was only dealing with different controllers and their views. Today decided to work on some general stuff and here is the case...
I have header_tpl.php which I am loading all the time with the help of MY_Controller.php
Code:
function MY_Controller()
    {
        parent::Controller();

        $this->template->set_layout('page_tpl');
        $this->template->set_partial('header', 'header_tpl', FALSE);
        }

So far, so good... But now I want to add a menu in this header. Navigation menu. And here is my problem. The menu will be generated from the info from my database. For example I have a table "categories" and in this table I have 1 => "movies", 2 => "music", 3 => "images"
(this is because different users will have different options in the menu)
and my question is:
I can make a function get_menu_items(), but where should I place it? In MY_Controller.php I think it is not soo good idea Sad
And in this function there will be db stuff (model maybe)?
And second ... how to add the recieved data in the template? set_partial does not have a forth parameter $data

Phil template is good, but the documentation and examples are poor Sad
#2

[eluser]victorche[/eluser]
Still no success ...

The problem is really hard to explain (and maybe not exactly CI related). I am really learning php and the problem is:

I can understand the model-view-controller idea and if we are talking about a single page (like blog, news), I can manage with the stuff. Creating the controller, then a model to handle with all the functions which are database related.

But I have common parts on my site (e.g. header and footer). And yes, I've read all the Wiki articles, about different approaches. I've read the "Keep it DRY" topic by Phil. Tried and tried ... No result.

As adviced, I created MY_Controller.php and ... Added there some common template stuff. Only a few lines about my partials (header, footer). I was playing with some inner parts of my site, but 2 days ago I decided to work on the header part. I have a nav menu there and I need a small function to take the links and their titles from my db. So I can put this function in a model, but I don't want it to repeat in every single model. That's why I've made MY_Model.php and put the function there. And if I call it from any of my controllers, it works. But the situation is the same. Why I have to repeat this code in every single controller? Just to show the menu links. I want to call this function in MY_Controller.php but seems to be impossible for me Sad

Any ideas, please?
#3

[eluser]clip[/eluser]
Ive not personally used this library but I think this will work based on what I have read about it. You can try something like this:

Code:
//in your controller set the variable
$this->data['nav_menu'] = $this->my_model->get_stuff_from_db();

//in your partial template add your variable
{nav_menu}
//the library might use a $
{$nav_menu}

Link I said, I "think" this will work but I'm not 100% sure. Just thought I would throw you a bone.
#4

[eluser]victorche[/eluser]
@clip, it works ... And thanks, but this is not what i am asking for ...

simply because this menu will be everywhere... No matter if you are in /blog/ or /news/ or /contacts/

It will change only depending on the user (logged or not, admin or not)

So I don't want to call this in every single controller. I want to escape the code duplication. That's why I was reading about MY_Controller.php, just for using some general and repeating code in it. And it works (sort of).

The problem is that I want to manage the menu links now. And I am taking them from a db table with a simple funcion. And here the problems begun Sad

This funcion takes info from the database, so (i think) it should be in a model. But as I don't want to repeat parts of code in the controllers, i also don't want to repeat this small function in every model i have (like blog_model, news_model, links_model ...). That's why i've made a MY_Model.php and put this little function in there.

Anyway, if i call it from a controller, everything is Ok ... But how can i call it from MY_Controller.php ?

I want to call it only once, to generate my menu links only once and to do it it MY_Controller.php

I don't want to call this in every controller and I don't want to copy and paste this small function in every model too.

Please, this makes me crazy already Smile
#5

[eluser]clip[/eluser]
Just set it in your MY_Controller, you will just need to make sure the template library is also loaded from MY_Controller.
#6

[eluser]victorche[/eluser]
the template library is autoloaded ...
You mean in MY_Controller.php to use $this->my_model->function_name_something();

Hmm ... will try it when i go back home, thanks!
#7

[eluser]clip[/eluser]
Yes, set the results of "your" db query to the template variable as I stated above and process as necessary Smile just make sure you assign it to $this->data so it's available to all of your controllers that extend your MY_Controller.
#8

[eluser]victorche[/eluser]
I will try it for sure, but anyway I am asking about the logic in general ... Specific controller -> using the specific model ... those little functions, with taking data from the db, where is their place?

In MY_Model.php maybe?
#9

[eluser]victorche[/eluser]
clip, not working ... as expected, MY_Controller.php is not loading MY_Model.php, so ... the result is:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Blog::$my_model

The whole thing is that ... it does not look right. I am not a php guru, but I feel that I am making big mistake as logic here. Please, someone ... Sad
#10

[eluser]clip[/eluser]
Sorry you mis understood me. Replace "my_model" with the name of your actual model that contains the method your wanting to call to get your info out of the database.




Theme © iAndrew 2016 - Forum software by © MyBB