2011-02-24 22:26:09 +02:00
# Kostache
2010-05-17 04:55:59 +03:00
2011-02-24 22:26:09 +02:00
Kostache is a [Kohana 3 ](https://github.com/kohana/kohana ) module for using [Mustache ](http://defunkt.github.com/mustache/ ) templates in your application.
2010-05-17 04:55:59 +03:00
Mustache is a logic-less template class. It is impossible to embed logic into mustache files.
2010-12-09 21:04:27 +02:00
## Example
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
Did you know the pagination view in Kohana is terrible? We are going to fix it:
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
### How it exists now:
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
< p class = "pagination" >
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
<?php if ($first_page !== FALSE): ?>
< a href = "<?php echo $page->url($first_page) ?>" rel = "first" > <?php echo __ ('First') ?> </ a >
<?php else: ?>
<?php echo __ ('First') ?>
<?php endif ?>
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
<?php if ($previous_page !== FALSE): ?>
< a href = "<?php echo $page->url($previous_page) ?>" rel = "prev" > <?php echo __ ('Previous') ?> </ a >
<?php else: ?>
<?php echo __ ('Previous') ?>
<?php endif ?>
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
<?php if ($i == $current_page): ?>
< strong > <?php echo $i ?> < / strong >
<?php else: ?>
< a href = "<?php echo $page->url($i) ?>" > <?php echo $i ?> < / a >
<?php endif ?>
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
<?php endfor ?>
2010-05-17 04:55:59 +03:00
2010-12-09 21:04:27 +02:00
<?php if ($next_page !== FALSE): ?>
< a href = "<?php echo $page->url($next_page) ?>" rel = "next" > <?php echo __ ('Next') ?> </ a >
<?php else: ?>
<?php echo __ ('Next') ?>
<?php endif ?>
2010-05-18 03:37:53 +03:00
2010-12-09 21:04:27 +02:00
<?php if ($last_page !== FALSE): ?>
< a href = "<?php echo $page->url($last_page) ?>" rel = "last" > <?php echo __ ('Last') ?> </ a >
<?php else: ?>
<?php echo __ ('Last') ?>
<?php endif ?>
2010-05-18 03:37:53 +03:00
2010-12-09 21:04:27 +02:00
< / p > <!-- .pagination -->
2010-05-18 03:37:53 +03:00
2010-12-09 21:21:30 +02:00
Wow, look at all that logic in there! How do you plan on effectively maintaining that?!?
2010-05-18 03:37:53 +03:00
2010-12-09 21:04:27 +02:00
### Our new View Class (classes/view/pagination/basic.php)
2010-05-18 03:37:53 +03:00
2010-12-09 21:04:27 +02:00
< ?php defined('SYSPATH') or die('No direct script access.');
2010-05-18 03:37:53 +03:00
2010-12-09 21:04:27 +02:00
class View_Pagination_Basic extends kostache {
protected $pagination;
protected function items()
{
$items = array();
// First.
$first['title'] = 'first';
$first['name'] = __ ('first');
$first['url'] = ($this->pagination->first_page !== FALSE) ? $this->pagination->url($this->pagination->first_page) : FALSE;
$items[] = $first;
// Prev.
$prev['title'] = 'prev';
$prev['name'] = __ ('previous');
$prev['url'] = ($this->pagination->previous_page !== FALSE) ? $this->pagination->url($this->pagination->previous_page) : FALSE;
$items[] = $prev;
// Numbers.
for ($i=1; $i< =$this->pagination->total_pages; $i++)
2010-05-18 03:37:53 +03:00
{
2010-12-09 21:04:27 +02:00
$item = array();
$item['num'] = TRUE;
$item['name'] = $i;
$item['url'] = ($i != $this->pagination->current_page) ? $this->pagination->url($i) : FALSE;
$items[] = $item;
2010-05-18 03:37:53 +03:00
}
2010-12-09 21:04:27 +02:00
// Next.
$next['title'] = 'next';
$next['name'] = __ ('next');
$next['url'] = ($this->pagination->next_page !== FALSE) ? $this->pagination->url($this->pagination->next_page) : FALSE;
$items[] = $next;
// Last.
$last['title'] = 'last';
$last['name'] = __ ('last');
$last['url'] = ($this->pagination->last_page !== FALSE) ? $this->pagination->url($this->pagination->last_page) : FALSE;
$items[] = $last;
return $items;
2010-05-18 05:42:48 +03:00
}
}
2010-12-09 21:04:27 +02:00
Yum, logic in a class, where it belongs :)
2010-05-18 05:42:48 +03:00
2010-12-09 21:04:27 +02:00
### Our mustache template (templates/pagination/basic.mustache)
2010-05-18 05:42:48 +03:00
2010-12-09 21:04:27 +02:00
< p class = "pagination" >
{{#items}}
{{#url}}< a href = "{{url}}" { { # title } } rel = "{{rel}}" { { / title } } > {{/url}}{{#num}}< strong > {{/num}}{{name}}{{#num}}< / strong > {{/num}}{{#url}}< / a > {{/url}}
{{/items}}
< / p >
2010-05-18 05:42:48 +03:00
2010-12-09 21:04:27 +02:00
Holy cow, that's more maintainable :)
2010-05-18 05:42:48 +03:00
2010-12-09 21:04:27 +02:00
## Partials
2010-09-04 18:09:46 +03:00
To use a partial in your template you use the greater than sign (>) and the name, e.g. {{>header}}.
You must define partials within the $_partials array in your view class. The key is the name that you use in your template and the value is a path to your partial file.
protected $_partials = array(
'header' => 'header', // Loads templates/header.mustache
'footer' => 'footer/default', // Loads templates/footer/default.mustache
);
2010-12-09 21:04:27 +02:00
## Using the View_Layout class
2010-08-04 22:14:19 +03:00
2011-02-24 22:26:09 +02:00
Kostache comes with a View_Layout class instead of a template controller. This allows your layouts to be more OOP and self contained, and they do not rely on your controllers so much.
2010-08-04 22:14:19 +03:00
To use it, have your view extend the View_Layout class. You can then specify your own layout file by placing it in templates/layout.mustache. At a minimum, it needs to have a {{>body}} partial defined in it.
2010-09-06 05:48:19 +03:00
If you have a view that extends the View_Layout class, but wish to render only the template and not the entire layout, you can set the public $render_layout property to FALSE. This is useful if you want to use the same view class for external requests and HMVC requests.
$view = new View_Post_List;
if ($this->request !== Request::instance) // Is internal request
{
$view->render_layout = FALSE;
}
2010-05-18 03:37:53 +03:00
For specific usage and documentation, see:
2010-05-17 04:55:59 +03:00
[PHP Mustache ](http://github.com/bobthecow/mustache.php )
[Original Mustache ](http://defunkt.github.com/mustache/ )