From 4a3277020bbea712765fee300a0abd28bdab22fc Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Sat, 4 Sep 2010 23:09:46 +0800 Subject: [PATCH 1/8] Added blurb on using partials. Formatting documentation for userguide module. Includes kodoc markdown handler extension to allow mustach tags in userguide. --- README.markdown | 12 ++++ classes/kodoc/markdown.php | 30 ++++++++ guide/kostache.about.md | 7 ++ guide/kostache.examples.md | 139 +++++++++++++++++++++++++++++++++++++ guide/kostache.usage.md | 50 +++++++++++++ guide/menu.kostache.md | 4 ++ 6 files changed, 242 insertions(+) create mode 100644 classes/kodoc/markdown.php create mode 100644 guide/kostache.about.md create mode 100644 guide/kostache.examples.md create mode 100644 guide/kostache.usage.md create mode 100644 guide/menu.kostache.md diff --git a/README.markdown b/README.markdown index 6c6b807..5e1d6ae 100644 --- a/README.markdown +++ b/README.markdown @@ -186,6 +186,18 @@ Controller: } } // End Welcome +Partials +--- + +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 + ); + Using the View_Layout class --- diff --git a/classes/kodoc/markdown.php b/classes/kodoc/markdown.php new file mode 100644 index 0000000..04a2ddd --- /dev/null +++ b/classes/kodoc/markdown.php @@ -0,0 +1,30 @@ +render(); + } + catch (Exception $e) + { + // View file not found. Do not replace {{text}}. + } + } + + $text = strtr($text, $replace); + } + + return $text; + } +} diff --git a/guide/kostache.about.md b/guide/kostache.about.md new file mode 100644 index 0000000..f007f5b --- /dev/null +++ b/guide/kostache.about.md @@ -0,0 +1,7 @@ +# About KOstache + +KOstache is a 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. + +This module requires mustache.php and includes it as a git submodule. See [Working With Git](tutorials.git) for more information about managing submodules. \ No newline at end of file diff --git a/guide/kostache.examples.md b/guide/kostache.examples.md new file mode 100644 index 0000000..cb259ca --- /dev/null +++ b/guide/kostache.examples.md @@ -0,0 +1,139 @@ +# KOstache Examples + +## Complex Example + +Model (This example uses [AutoModeler](http://github.com/zombor/Auto-Modeler)): + + class Model_Test extends AutoModeler + { + protected $_table_name = 'tests'; + + protected $_data = array( + 'id' => '', + 'name' => '', + 'value' => '', + ); + + protected $_rules = array( + 'name' => array('not_empty'), + 'value' => array('not_empty'), + ); + } + +View: + + class View_Example extends Kostache + { + public $title = 'Testing'; + + public function things() + { + return Inflector::plural(get_class(new Model_Test)); + } + + public function tests() + { + $tests = array(); + foreach (AutoModeler::factory('test')->fetch_all() as $test) + { + $tests[] = $test->as_array(); + } + return $tests; + } + } + +Template: + + + + + + {{title}} + + +

{{title}}

+

Here are all my {{things}}:

+ + + + +Controller: + + class Controller_Welcome extends Controller { + + public function action_index() + { + echo new View_Example; + } + + } // End Welcome + +## Grabbing a single model value + +Model (This example uses [AutoModeler](http://github.com/zombor/Auto-Modeler)): + + class Model_Test extends AutoModeler + { + protected $_table_name = 'tests'; + + protected $_data = array( + 'id' => '', + 'name' => '', + 'value' => '', + ); + + protected $_rules = array( + 'name' => array('not_empty'), + 'value' => array('not_empty'), + ); + } + +View: + + class View_Singular extends Kostache + { + protected $_pragmas = array(Kostache::PRAGMA_DOT_NOTATION => 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: + + class Controller_Welcome extends Controller { + + public function action_singular($id) + { + $view = new View_Singular; + $view->thing_id = $id; + echo $view; + } + } // End Welcome diff --git a/guide/kostache.usage.md b/guide/kostache.usage.md new file mode 100644 index 0000000..3ebb6ba --- /dev/null +++ b/guide/kostache.usage.md @@ -0,0 +1,50 @@ +# KOstache Usage + +View classes go in classes/view/ + +classes/view/example.php + + 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" + +## Partials + +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 + ); + +## 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. + +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. + +## Mustache Documentation + +For specific usage and documentation, see: + +[PHP Mustache](http://github.com/bobthecow/mustache.php) + +[Original Mustache](http://defunkt.github.com/mustache/) \ No newline at end of file diff --git a/guide/menu.kostache.md b/guide/menu.kostache.md new file mode 100644 index 0000000..12d4c06 --- /dev/null +++ b/guide/menu.kostache.md @@ -0,0 +1,4 @@ +1. **KOstache** + - [About](kostache.about) + - [Usage](kostache.usage) + - [Examples](kostache.examples) \ No newline at end of file From 56ab985be7f3809dd4fdf0e4f477f20cd3fdebee Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Mon, 6 Sep 2010 10:48:19 +0800 Subject: [PATCH 2/8] Add render_layout property and update documentation. --- README.markdown | 8 ++++++++ classes/view/kohana/layout.php | 15 ++++++++++----- guide/kostache.usage.md | 10 +++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index 5e1d6ae..d365533 100644 --- a/README.markdown +++ b/README.markdown @@ -205,6 +205,14 @@ KOstache comes with a View_Layout class instead of a template controller. This a 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. +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; + } + For specific usage and documentation, see: [PHP Mustache](http://github.com/bobthecow/mustache.php) diff --git a/classes/view/kohana/layout.php b/classes/view/kohana/layout.php index 18c073f..47df7fb 100644 --- a/classes/view/kohana/layout.php +++ b/classes/view/kohana/layout.php @@ -4,6 +4,8 @@ class View_Kohana_Layout extends Kostache { protected $_layout = 'layout'; + public $render_layout = TRUE; + /** * @var string template title */ @@ -22,12 +24,15 @@ class View_Kohana_Layout extends Kostache $this->_template = strtolower(implode(DIRECTORY_SEPARATOR, $foo)); } - $this->_partials+=array( - 'body' => $this->_template - ); + if ($this->render_layout) + { + $this->_partials+=array( + 'body' => $this->_template + ); - // Make the layout view the child class's template - $this->_template = $this->_layout; + // Make the layout view the child class's template + $this->_template = $this->_layout; + } return parent::render($template, $view, $partials); } diff --git a/guide/kostache.usage.md b/guide/kostache.usage.md index 3ebb6ba..6d2f1da 100644 --- a/guide/kostache.usage.md +++ b/guide/kostache.usage.md @@ -39,7 +39,15 @@ You must define partials within the $_partials array in your view class. The ke 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. -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. +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. + +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; + } ## Mustache Documentation From 48f32bfd8d37287075e5526ac7524759a8ea653c Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Mon, 6 Sep 2010 10:56:55 +0800 Subject: [PATCH 3/8] Removed markdown extension to be put in userguide module. --- classes/kodoc/markdown.php | 30 ------------------------------ 1 file changed, 30 deletions(-) delete mode 100644 classes/kodoc/markdown.php diff --git a/classes/kodoc/markdown.php b/classes/kodoc/markdown.php deleted file mode 100644 index 04a2ddd..0000000 --- a/classes/kodoc/markdown.php +++ /dev/null @@ -1,30 +0,0 @@ -render(); - } - catch (Exception $e) - { - // View file not found. Do not replace {{text}}. - } - } - - $text = strtr($text, $replace); - } - - return $text; - } -} From 92ea393e6e199914ec1d71c0ce6271ac22f2f4ce Mon Sep 17 00:00:00 2001 From: Jeremy Bush Date: Tue, 19 Oct 2010 07:08:07 -0500 Subject: [PATCH 4/8] adding date form dropdown templates --- templates/dates/days.mustache | 33 +++++++++++++++++++++++++++++++++ templates/dates/months.mustache | 4 ++++ templates/dates/years.mustache | 4 ++++ 3 files changed, 41 insertions(+) create mode 100644 templates/dates/days.mustache create mode 100644 templates/dates/months.mustache create mode 100644 templates/dates/years.mustache diff --git a/templates/dates/days.mustache b/templates/dates/days.mustache new file mode 100644 index 0000000..7c07b70 --- /dev/null +++ b/templates/dates/days.mustache @@ -0,0 +1,33 @@ + \ No newline at end of file diff --git a/templates/dates/months.mustache b/templates/dates/months.mustache new file mode 100644 index 0000000..fec51fa --- /dev/null +++ b/templates/dates/months.mustache @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/templates/dates/years.mustache b/templates/dates/years.mustache new file mode 100644 index 0000000..7e55a4e --- /dev/null +++ b/templates/dates/years.mustache @@ -0,0 +1,4 @@ + \ No newline at end of file From 8149e6d3a99e89f322e63172e7e7ef3a698e0b07 Mon Sep 17 00:00:00 2001 From: Matt Button Date: Tue, 16 Nov 2010 01:51:13 +0800 Subject: [PATCH 5/8] Updating mustache submodule --- vendor/mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/mustache b/vendor/mustache index 8b1c9c1..a39c6f6 160000 --- a/vendor/mustache +++ b/vendor/mustache @@ -1 +1 @@ -Subproject commit 8b1c9c113c4e189923e4874be6fca5be715239ac +Subproject commit a39c6f689ecf3f6b5b309a32e09a1d12a3866682 From 9b492500c81369d9c0a494c78a3879e1076003f4 Mon Sep 17 00:00:00 2001 From: Jeremy Bush Date: Thu, 9 Dec 2010 13:04:05 -0600 Subject: [PATCH 6/8] Upgrading to mustache 0.5.0 --- vendor/mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/mustache b/vendor/mustache index a39c6f6..a448453 160000 --- a/vendor/mustache +++ b/vendor/mustache @@ -1 +1 @@ -Subproject commit a39c6f689ecf3f6b5b309a32e09a1d12a3866682 +Subproject commit a448453da30c6862cad912cd3db4ac4b8751deb2 From cae332760a335ebbd426253d56e06a83883a881d Mon Sep 17 00:00:00 2001 From: Jeremy Bush Date: Thu, 9 Dec 2010 13:04:27 -0600 Subject: [PATCH 7/8] Adding better readme example --- README.markdown | 264 +++++++++++++++++------------------------------- 1 file changed, 95 insertions(+), 169 deletions(-) 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}}:

-
    - {{#tests}} -
  • {{id}}: ({{name}}:{{value}})
  • - {{/tests}} -
- - - -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}}

-
    -
  • Name: {{thing.name}}
  • -
  • Value: {{thing.value}}
  • -
- - - -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. From ceffe2942f8fb62fd2670732c39e6900146ef36f Mon Sep 17 00:00:00 2001 From: Jeremy Bush Date: Thu, 9 Dec 2010 13:05:25 -0600 Subject: [PATCH 8/8] adding version constant --- classes/kohana/kostache.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/kohana/kostache.php b/classes/kohana/kostache.php index c005023..9dea279 100644 --- a/classes/kohana/kostache.php +++ b/classes/kohana/kostache.php @@ -2,6 +2,8 @@ class Kohana_Kostache extends Mustache { + const VERSION = 1.3; + protected $_partials_processed = FALSE; /**