0

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

iamjonesy
  • 24,732
  • 40
  • 139
  • 206
  • 1
    Duplicate of http://stackoverflow.com/questions/3957000/what-codeigniter-template-library-is-best. – treeface Jan 06 '11 at 17:39

3 Answers3

1

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.

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
0

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.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
0

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.

Michael B
  • 1,743
  • 4
  • 21
  • 35
  • So say I had a view called header I can just load header within another view? – iamjonesy Jan 06 '11 at 19:29
  • Yes, exactly. That's generally how it's done. Basically use in your views. The view is the same thing as a standard PHP file with the exception of the fact that you never access them directly. :) – Michael B Jan 06 '11 at 19:38
  • 1
    @iamjonesy: No, you shouldn't have to use the `include` syntax in your views. If you want to load a view within another view, use CodeIgniter's view loader: `$this->load->view('header')`. – treeface Jan 06 '11 at 20:35
  • True, yes. That is likely better; it keeps logic/calls within the controller. – Michael B Jan 06 '11 at 21:07