Welcome Guest, Not a member yet? Register   Sign In
Template views!
#1

[eluser]deadfrog[/eluser]
Hi all - I came to learn of CI when I started looking for a PHP version of Ruby on Rails, and I have a query which may be easy to answer.

In Rails, you can have a single global template that all views use by default (or you can specify a different template in the controller if required. If memory serves, the template file had a 'yield' command in the area where the view content per page should appear.

If anyone knows what I'm talking about, my question is whether there is a similar thing in CI or do you have to create a view from scratch for each page (or use traditional PHP includes).

Cheers and apologies for the poor description Smile
#2

[eluser]exodus7[/eluser]
The wiki lists a few different ways this can be accomplished using template engines, visit http://codeigniter.com/wiki/Category:Lib...E_ENGINES/

As an example, the 'Most Simple Template Library' lets you load a view file and a template file as simple as doing
Code:
$this->template->load('template_file', 'view_file', $data);
#3

[eluser]Rick Jolly[/eluser]
Forget template engines and use a simple generic solution - inheritance. There are a few ways to extend the controller, here's an example:
Code:
class Base extends Controller
{
  var $template = 'site_default'
  var $content;
  
  function Base()
  {
    parent::Controller();
    // set default content
    $this->content = ...
  }
}

require_once(APPPATH . 'controllers/base.php');

class SomeController extends Base
{
  var $template = 'some_different_template';
  
  function SomeController()
  {
    parent::Base();
  }
  
  function index()
  {
    $this->content = ...
    $this->load->view($this->template, $this->content);
  }
}
#4

[eluser]deadfrog[/eluser]
Thanks for the replies guys, I've decided to just go with calling other views via controller (header,footer etc) and leaving some formatting in the indivdual views as it's much cleaner for me at this stage!




Theme © iAndrew 2016 - Forum software by © MyBB