How to efficiently handle a menu template in Codeigniter?

I am looking for the best way to process my template. Right now, my template looks something like this:

View / template.php

$this->load->view('includes/menu', $menu); $this->load->view('includes/content', $main_content); 

The menu in my template uses several variables from the database.

My controller looks something like this:

 function show_pageA() { /* */ /* */ /* */ /* */ /* */ /* 10 lines menu related codes here */ /* mainly to get variables from databas */ /* */ /* */ /* */ /* */ $data['menu'] = array of variables pulled from previous line $data['main_content'] = 'pageA'; $this->load->view('template',$data); } function show_pageB() { /* */ /* */ /* */ /* */ /* */ /* 10 lines menu related codes here */ /* mainly to get variables from databas */ /* */ /* */ /* */ /* */ $data['menu'] = array of variables pulled from previous line $data['main_content'] = 'pageB'; $this->load->view('template',$data); } 

As you can see, each page function has 10 lines of code related to the menu, and this seems superfluous to me.

Can someone suggest me a better way to do this while practicing MVC?

Thanks,

+4
source share
2 answers

There are two different things that I would do to correct this situation.

  • I would wrap these 10 lines of code in Model , because, as you said, it interacts with the database. Perhaps create a Menu_model that has a get() function that returns an array of elements that you need to load into your views. Then calling it in each controller will be as simple as

     $data['menu'] = $this->menu_model->get(); //assuming you autoload this model 
  • But do not stop them, as this is still a repeating line of code in the EVERY controller function. Let's do this in the Controller constructor so that these parameters are loaded globally in all the views that we create.

Assuming you are php5'd

 function __construct() { parent::__construct(); //get menu data $global_data['menu'] = $this->menu_model->get(); //load into all views loaded by this controller $this->load->vars($global_data); } 

Then your controller functions will look like this.

 function show_pageA() { $data['main_content'] = 'pageA'; $this->load->view('template',$data); } 

(Also, if this β€œmenu” logic spans multiple controllers, I would recommend moving the custom MY_Controller.php , which includes this logic, so that you don't repeat again in every controller constructor)

+4
source

Instead of using Codeigniter views, when it comes to templates and partitions, you should consider using the template library. There are many examples that you may find useful, and all of them mainly handle breadcrumbs, looking at partial and other amazing things that you want to do.

I use the Phil Sturgeon template library in my Codeigniter projects because it is light and just works: http://philsturgeon.co.uk/code/codeigniter-template

Colin Williams Template Library is another popular template library: http://williamsconcepts.com/ci/codeigniter/libraries/template/

And some people seem to love using the Ocular template library: https://github.com/lonnieezell/Ocular-Template-Library

+1
source

Source: https://habr.com/ru/post/1338601/


All Articles