开发者

CodeIgniter: tips for using templates

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

Is it poss开发者_开发知识库ible to create a view and just load it whenerever necessary?

Thanks,

Jonesy


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜