• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI 3 should has partial view or user control

#1
A common requirement of web page development is sharing the same header, footer, sidebar in several pages.

And also, these shared header, footer, sidebar might have logical code to retrieve data from database or other tasks.

So the php include is not enough, it only can include html codes or simple php codes.

Some other framework support partial view / partial controller / user control, these items can be nested into web pages, and the web page only need to implement the main part of the page.
Reply

#2
CodeIgniter already does support this - it's all in how you use it Smile

Check out https://github.com/bcit-ci/codeigniter-website for an example of how this can be achieved.
Reply

#3
Pretty easy. Create a "template" library, where the methods will load the header/footer/etc, and then you just pass it the "main" view for that page. You can use the 3rd parameter of load::view() to return the view instead of directly outputting it. Useful for templating.
Reply

#4
Here is a very code to show the concept.
Code:
class Template {
  private $CI;

  public function __construct()
  {
    $this->CI =& get_instance();
  }

  public function generate($content)
  {
    $header = $this->CI->load->view('header', null, TRUE);
    $footer = $this->CI->load->view('footer', null, TRUE);
    $this->CI->output->set_output($header . $content . $footer); //use output so CI can cache results if caching enabled
  }
}

Code:
$autoload['libraries'] = array('template');

Then in a controller...

Code:
function something()
{
  //...do everything needed to for this page's content
  $data = array();  //data for this contents view
  $content = $this->load->view('content_for_this_page', $data, TRUE); //return view instead of directly outputting it

  //now just pass this pages view to the template, which will automatically generate header/footer
  $this->template->generate($content);
}
Reply

#5
@Cronix
  Thanks for the easy template explanation.  My own code can use an adjustment based on your post.
CI 3.1 Kubuntu 19.04 Apache 5.x  Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply

#6
Glad it helps. The example is very, very basic. I have a lot of different methods in my template library, like for generating admin pages vs. public pages, and it autogenerates the menu in my header file based on the logged in users session data to display links appropriate to that users' level, etc. I also pass an array of other things that need to go in the header file for my sites, like meta keywords, meta description, page title, etc, that go in the <head>.
Reply

#7
(02-09-2015, 01:41 PM)CroNiX Wrote: Glad it helps. The example is very, very basic. I have a lot of different methods in my template library, like for generating admin pages vs. public pages, and it autogenerates the menu in my header file based on the logged in users session data to display links appropriate to that users' level, etc. I also pass an array of other things that need to go in the header file for my sites, like meta keywords, meta description, page title, etc, that go in the <head>.

plz note that , template is not enough.
Template is just a solution of sharing same part in view.
But some times, shared header\footer\sidebar have business logic too.
So the best method is nested a mvc into another mvc.
Reply

#8
(02-10-2015, 06:40 PM)iamybj Wrote:
(02-09-2015, 01:41 PM)CroNiX Wrote: Glad it helps. The example is very, very basic. I have a lot of different methods in my template library, like for generating admin pages vs. public pages, and it autogenerates the menu in my header file based on the logged in users session data to display links appropriate to that users' level, etc. I also pass an array of other things that need to go in the header file for my sites, like meta keywords, meta description, page title, etc, that go in the <head>.

plz note that , template is not enough.
Template is just a solution of sharing same part in view.
But some times, shared header\footer\sidebar have business logic too.
So the best method is nested a mvc into another mvc.

Sure, all my business logic for the header, and other partial views, gets generated in the template library. As I mentioned they're dynamic and get generated based on the users level and other factors. What you describe can easily be done without "mvc into another mvc". Sure you can use HMVC as well, but I've never needed to.
Reply

#9
How about this https://github.com/jenssegers/codeignite...te-library ?
Reply

#10
(02-10-2015, 06:40 PM)iamybj Wrote: plz note that , template is not enough.
Template is just a solution of sharing same part in view.
But some times, shared header\footer\sidebar have business logic too.
So the best method is nested a mvc into another mvc.

Common elements like that can always be pulled together in a MY_Controller that gathers the required info from different libraries and/or models.

Additionally, you can put together a small function that can do the "widget" thing for you. I implemented one a while back in response to someone else needing something like that and thinking you had to have HMVC, called Bays.

Or, if you're wanting full HMVC solutions, you can use WireDesignz' HMVC solution or one of the other HMVC solutions to get just what you're asking for.

Finally, if you namespace your classes, it's simple to add a static method to your modular classes to return the HMVC for that method. Then, in your views, just call that method, something like:

Code:
<?= \My\Namespaced\Library::show_sidebar_block() ?>

There's lots of possible ways to do what you're looking for. The one closest to what you've mentioned is the HMVC solution, and it's Modules::run() method, or using CI's packages and something like my Bays.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.