Best Method of Including Views Within Views in Codeigniter

Best method of including views within views in CodeIgniter

Views within other views are called Nested views.
There are two ways of including nested views in CodeIgniter:

1. Load a nested view inside the controller

Load the view in advance and pass to the other view. First put this in the controller:

<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
?>

Then put <?=$menu?> in your view at the point you want the menu to appear.

2. Load a view "from within" a view

First put this in the controller:

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

Then put this in the /application/views/home.php view:

<?php $this->view('menu'); ?>

<p>Other home content...</p>

About best method, I prefer the 1st method over 2nd one, because by using 1st method I don't have to mix up code, it is not like include php. Although indirectly both are same, the 1st method is clearer & cleaner than 2nd one!

What is best option to load multiple views using Codeigniter?

You could use $this-load->view from inside each of your views, not only controllers.

So in your index.php view file, at the top you could load the header, and at the bottom the footer.

I would not use include() in CodeIgniter at all. CI has loaders for everything you need to use inside it.

A better way to do it would be to define a library or model that would load the template.This way, if you want to change the file included at the top or bottom you don't need to change every view. Even more, you can use this load js,css, seo metadata etc. based on each controller.


The way to achieve this is (let's go the model route):

  1. Create a model called TemplateModel with a function render that receives $data.
  2. In $data you can define the keys: js, css, seo, content etc.
  3. Load the layout from inside the model with $this->load->view('template', $data).
  4. In your controller, return your view as data (for example index.php) and send it to the model as $data['content'] : https://www.codeigniter.com/user_guide/general/views.html#returning-views-as-data
    example:

    $this->load-model('templateModel', 'template');
    $data['content'] = $this->load->view('index', '', TRUE);
    $this->template->render($data);
  5. In the view rendered by your model, print content where it should display (btw header and footer).

Codeigniter multiple views in one view

one approach is to have a template view that has the elements you require.
see this pseudo-code example...
so, template_view.php has:

$this->load->view('header',$header);    
$this->load->view('quicksearch',$quickssearch);
$this->load->view('body',$body);
$this->load->view('footer',$footer);

your single controller then calls the template with the parameters for each view.

$data = new stdClass();
$data->header = ....
$data->quickssearch = ....
$data->body = .....
$data->footer = .....

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

serving different views in code igniter

There isn't a hard rule for this. You can structure your view files however you like, and call $this->load->view() to load different view files for different outcomes in your controller. From my experience, CodeIgniter adapts very openly to how you organize your application's files.


In your example, perhaps I'd divide my system/application/views folder into two subfolders: main for desktop browsers, and mobile for mobile browsers:

system/
application/
views/
main/
index.php
some_page.php
...
mobile/
index.php
some_page.php
...

In an early part of your controller, say the constructor, you can decide what user agent is requesting it and then pick main or mobile based on that, then show your views accordingly from your controller actions.

Some quick code snippets to give you a better idea since you're new...

// Place this just below the controller class definition
var $view_type = 'main';

// Controller constructor
function MyController()
{
parent::Controller();

if ($this->agent->is_mobile())
{
$this->view_type = 'mobile';
}
else
{
$this->view_type = 'main';
}
}

// Example action
function some_page()
{
// ...

// This comes from the 'var $view_type;' line above
$this->load->view($this->view_type . '/some_page');
}

And some helpful references for you to explore:

  • Views in CodeIgniter
  • User Agent Class

Hope my explanation helps, and hope you have fun with CodeIgniter :)

Codeigniter: Best way to structure partial views

I can't vouch that this is the best approach, but I create a base controller like this:

class MY_Controller extends CI_Controller {

public $title = '';
// The template will use this to include default.css by default
public $styles = array('default');

function _output($content)
{
// Load the base template with output content available as $content
$data['content'] = &$content;
$this->load->view('base', $data);
}

}

The view called 'base' is a template (a view that includes other views):

<?php echo doctype(); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php $this->load->view('meta'); ?>
</head>
<body>
<div id="wrapper">
<?php $this->load->view('header'); ?>

<div id="content">
<?php echo $content; ?>
</div>

<?php $this->load->view('footer'); ?>
</div>
</body>
</html>

What this achieves is that every controller wraps its output in the base template, and that views have valid HTML instead of opening tags in one view and closing in another. If I'd like a specific controller to use a different or no template, I could just override the magic _output() method.

An actual controller would look like this:

class Home extends MY_Controller {

// Override the title
public $title = 'Home';

function __construct()
{
// Append a stylesheet (home.css) to the defaults
$this->styles[] = 'home';
}

function index()
{
// The output of this view will be wrapped in the base template
$this->load->view('home');
}
}

Then I could use its properties in my views like this (this is the 'meta' view that populates the <head> element):

echo "<title>{$this->title}</title>";
foreach ($this->styles as $url)
echo link_tag("styles/$url.css");

I like my approach because it respects the DRY principle and the header, footer and other elements get included just once in the code.



Related Topics



Leave a reply



Submit