diff --git a/README.markdown b/README.markdown index d365533..9f85e09 100644 --- a/README.markdown +++ b/README.markdown @@ -1,193 +1,120 @@ -KOstache -============ +# KOstache -KOstache is a kohana module for using [Mustache](http://defunkt.github.com/mustache/) templates in your application. +KOstache is a [Kohana 3](https://github.com/kohana/kohana) module for using [Mustache](http://defunkt.github.com/mustache/) templates in your application. Mustache is a logic-less template class. It is impossible to embed logic into mustache files. -Usage & Simple Example ------ +## Example -View classes go in classes/view/ +Did you know the pagination view in Kohana is terrible? We are going to fix it: -classes/view/example.php +### How it exists now: - - class View_Example extends Kostache - { - public $foo = 'bar'; - } + + + + + -Template files go in templates/ + + + + + -templates/example.mustache + - This is a {{foo}} + + + + + -In your controller, just do: + - $view = new View_Example; - echo $view; + + + + + -And you get: + + + + + - "This is a bar" +

-Complex Example ------ +Wow, look at all that login in there! How do you plan on effectively maintaining that?!? -Model (This example uses [AutoModeler](http://github.com/zombor/Auto-Modeler)): - - '', - 'name' => '', - 'value' => '', - ); - - protected $_rules = array( - 'name' => array('not_empty'), - 'value' => array('not_empty'), - ); - } - -View: - - fetch_all() as $test) - { - $tests[] = $test->as_array(); - } - return $tests; - } - } - -Template: - - - - - - {{title}} - - -

{{title}}

-

Here are all my {{things}}:

- - - - -Controller: - - '', - 'name' => '', - 'value' => '', - ); - - protected $_rules = array( - 'name' => array('not_empty'), - 'value' => array('not_empty'), - ); - } - -View: - - TRUE); - - public $thing_id = NULL; - public $title = 'Testing'; - - public function thing() - { - return new Model_Test($this->thing_id); - } - } - -Template: - - - - - - {{title}} - - -

{{title}}

-

This is just one thing:

-

{{thing.id}}

- - - - -Controller: +### Our new View Class (classes/view/pagination/basic.php) thing_id = $id; - echo $view; + 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++) + { + $item = array(); + + $item['num'] = TRUE; + $item['name'] = $i; + $item['url'] = ($i != $this->pagination->current_page) ? $this->pagination->url($i) : FALSE; + + $items[] = $item; + } + + // 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; } - } // End Welcome + } -Partials ---- +Yum, logic in a class, where it belongs :) + +### Our mustache template (templates/pagination/basic.mustache) + +

+ {{#items}} + {{#url}}{{/url}}{{#num}}{{/num}}{{name}}{{#num}}{{/num}}{{#url}}{{/url}} + {{/items}} +

+ +Holy cow, that's more maintainable :) + +## Partials To use a partial in your template you use the greater than sign (>) and the name, e.g. {{>header}}. @@ -198,8 +125,7 @@ You must define partials within the $_partials array in your view class. The ke 'footer' => 'footer/default', // Loads templates/footer/default.mustache ); -Using the View_Layout class ---- +## Using the View_Layout class 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.