Added blurb on using partials. Formatting documentation for userguide module. Includes kodoc markdown handler extension to allow mustach tags in userguide.

This commit is contained in:
Dan Robertson 2010-09-04 23:09:46 +08:00 committed by Jeremy Bush
parent 22ee92b081
commit 4a3277020b
6 changed files with 242 additions and 0 deletions

View file

@ -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
---

View file

@ -0,0 +1,30 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Kodoc_Markdown extends Kohana_Kodoc_Markdown {
public function doIncludeViews($text)
{
if (preg_match_all('/{{([^\s{}]++)}}/', $text, $matches, PREG_SET_ORDER))
{
$replace = array();
foreach ($matches as $set)
{
list($search, $view) = $set;
try
{
$replace[$search] = View::factory($view)->render();
}
catch (Exception $e)
{
// View file not found. Do not replace {{text}}.
}
}
$text = strtr($text, $replace);
}
return $text;
}
}

7
guide/kostache.about.md Normal file
View file

@ -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.

139
guide/kostache.examples.md Normal file
View file

@ -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:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
<p>Here are all my {{things}}:</p>
<ul>
{{#tests}}
<li><strong>{{id}}:</strong> ({{name}}:{{value}})</li>
{{/tests}}
</ul>
</body>
</html>
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:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
<p>This is just one thing:</p>
<h2>{{thing.id}}</h2>
<ul>
<li>Name: {{thing.name}}</li>
<li>Value: {{thing.value}}</li>
</ul>
</body>
</html>
Controller:
class Controller_Welcome extends Controller {
public function action_singular($id)
{
$view = new View_Singular;
$view->thing_id = $id;
echo $view;
}
} // End Welcome

50
guide/kostache.usage.md Normal file
View file

@ -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 &#123;&#123;>body&#125;&#125; 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/)

4
guide/menu.kostache.md Normal file
View file

@ -0,0 +1,4 @@
1. **KOstache**
- [About](kostache.about)
- [Usage](kostache.usage)
- [Examples](kostache.examples)