Welcome Guest, Not a member yet? Register   Sign In
question about Template Library for CodeIgniter
#1

[eluser]luben[/eluser]
Hello, I'm new to the Code Igniter framework and I look at it for a couple of days. From what I saw, I can tell it's great, intuitive and easy to use.

The problem I want to present is common:
I do not want to include the HEADER, SIDEBAR and FOOTER views manually in all controllers and methods.

That's why I looked at the Template Library created by one of the forum members here.

However, I cannot get the sense and advantages of it. My question is, what am I missing?

Here is example of my confusion:

Case 1: Normal usage without Template Library
Code:
function index()
{
  $data = 'some header data';
  $this->load->view('global/header', $data);

  $data = 'some content data';
  $this->load->view('current-page-view', $data);

  $data = 'some footer data';
  $this->load->view('global/footer', $data);
}

function another()
{
  $data = 'other header data';
  $this->load->view('global/header', $data);

  $data = 'other content data';
  $this->load->view('current-page-view', $data);

  $data = 'other footer data';
  $this->load->view('global/footer', $data);
}

Case 2: Usage with Template Library
Code:
function index()
{
  $data = 'some header data';
  $this->template->write_view('header', 'global/header', $data, TRUE);

  $data = 'some content data';
  $this->template->write('content', $data);

  $data = 'some footer data';
  $this->template->write_view('footer', 'global/footer', $data, TRUE);

  $this->template->render();
}

function another()
{
  $data = 'other header data';
  $this->template->write_view('header', 'global/header', $data, TRUE);

  $data = 'other content data';
  $this->template->write('content', $data);

  $data = 'other footer data';
  $this->template->write_view('footer', 'global/footer', $data, TRUE);

  $this->template->render();
}

The code with Template Library is even one line longer and again, I have to manually call the views I would like to include in every controller and function. I guess, I didn't get the usage of the library well, and hope to find some help in case you have found the way to use it properly.

Thank you and best regards,
luben
#2

[eluser]Mark Croxton[/eluser]
The Template library provides a bit more than that if you read the docs, but essentially I agree with you that it is basically a wrapper for load->view. I prefer to nest views within views.

Code:
// container view
<html>
<head>
<?php
$this->load->view('meta');
?>
</head>
<body>
<?php
$this->load->view('header');
$this->load->view('menu');
$this->load->view($page);
$this->load->view('footer');
?>
</body>
</html>

//controller

function index() {
   $data['page_title'] = 'Your title';
   $data['page'] = 'content'; // pass the actual view to use as a parameter
   $this->load->view('container',$data);
}

The important point is that variables passed to the container view will carry through to any nested views.




Theme © iAndrew 2016 - Forum software by © MyBB