iamjonesy
iamjonesy

Reputation: 25122

CodeIgniter: tips for using templates

Just started using Codeigniter (yesterday) and am wondering what templating features people are using?

Is it possible to create a view and just load it whenerever necessary?

Thanks,

Jonesy

Upvotes: 0

Views: 685

Answers (3)

Igbanam
Igbanam

Reputation: 6082

Another way to do this is the following.

In your controller, load your template like so

$template_data = array('contains', 'data', 'for', 'template',
                       'while', 'the', 'specific' => array('may', 'contain',
                       'data', 'for', 'the', 'view_file'));
$this->load->view('template/needed.php');

In your template, you now have the $template_data array to populate it [if need be!]. You may now load the specific view like so

<div id="yield">
  <?php echo $this->view('specific/viewer.php', $template_data['specific']); ?>
</div>

Note:

  1. The template/needed.php should be in the application/views folder.
  2. The specific/viewer.php file should also be in your views directory (i.e. the path to this file should be something like WEB_ROOT/application/views/specific/viewer.php)

The beauty of this is that any view file could be used as a template if need be.

Upvotes: 0

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

The idea of templating is to create a shared layout with a common header. footer etc and then just have a "body" that changes per-page.

At the most basic level you can just include header and footer inside each of your views like this:

load->view('header'); ?>

This is my page.

load->view('footer'); ?>

That can be fine but start building an application of any real size and you'll find problems.

There are million ways of doing templating, but the way I have used for years is this Template library. It's seen me through 20-30 projects varying projects and is used by many so you know it's tried and tested.

Upvotes: 1

Michael B
Michael B

Reputation: 1763

Is it possible to create a view and just load it whenerever necessary?

Yes. This is the typical behavior of the MVC structure, not just in CI. Your views are presentation layers that should be mostly devoid of logic/processing.

Upvotes: 0

Related Questions