How to Make A Kohana Template

November 6, 2012

We all know that there are parts of a page in a website that need to be on every page. Most of the time, the info on the header, footer, and the menu will be the same across all pages. What you really don’t want to do is copy and paste that code onto each page. Otherwise updating a link on one page will require you to update code on multiple pages.

The secret is to make a page that will call views with these elements and inject them into the main view. The best way is to make a method that will call something like this:

public function template() {
$options = ""; // whatever options you need set here.
$menu = new View('menu');
$menu->set('options', $options);
$header = new View('header');
$footer = new View('footer');
$view_container = new View('template');
$view_container->set('header', $header->render());
$view_container->set('menu', $menu->render());
$view_container->set('footer', $footer->render());
return $view_container;
}

Once you have made your method, you need only to call it and you have the entire template loaded. If you need to set variables within the menu, you can set those either before or after and pass them to the function. To call the function, simply call the following from your model:

$view_container = ORM::factory('[model-name]')->template();

You can then add whatever items to your template as you desire, before you call:

$view_container->set('[variable-to-be-set]', $variable);
$this->response->body($view_container->render());

The rest is all making views that will be compiled with these simple calls. I trust this has been a helpful post for you.

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive