Changed render method for views and layouts.

The previous render method allowed you to render a template
directly into a layout. This however only allowed one template
to be used per layout. The new method instead stores the template
results as a view variable, which the layout can reference.

Also expanded documentation on views.

Added a has() method to the view to check if a variable is set.
pull/11/head
Mike Cao 13 years ago
parent 516873fe13
commit 8af17f2215

@ -117,7 +117,7 @@ You can also use the wildcard character `*` for matching:
You can specify named parameters in routes which will be passed along to your callback function.
Flight::route('/@name/@id', function($name, $id){
echo "hello, {$name} - {$id}!";
echo "hello, $name ($id)!";
});
You can also include regular expressions with your named parameters by using the `:` delimiter:
@ -141,7 +141,7 @@ To map your own custom method, you use the `map` function:
// Map your method
Flight::map('hello', function($name){
echo "hello {$name}!";
echo "hello $name!";
});
// Call your custom method
@ -260,7 +260,7 @@ Here's an example the filtering process:
// Map a custom method
Flight::map('hello', function($name){
return "Hello, {$name}!";
return "Hello, $name!";
});
// Add a before filter
@ -332,7 +332,7 @@ Flight also uses variables for configuration purposes.
## Views
Flight provides you with some basic templating functionality. To display a view call the `render` method with the
Flight provides some basic templating functionality by default. To display a view call the `render` method with the
name of the template file and optional template data:
Flight::render('hello.php', array('name', 'Bob'));
@ -346,11 +346,42 @@ The output would be:
Hello, Bob!
You can manually set view variables at any time by using the `set` method:
Flight::view()->set('name', 'Bob');
Set variables will be automatically be included when you render a view. So you can simply do:
Flight::render('hello');
Note that when specifying the name of the template in the `render` method, you can leave out the `.php` extension.
By default Flight will look for a `views` directory for template files. You can set an alternate path for your templates
by setting the following config:
Flight::set('flight.views.path', '/path/to/views');
### Layouts
It is common to have a single layout template file with interchanging content. To render content for a layout you need
to pass in a variable name to the render method.
Flight::render('hello', array('name' => 'Bob'), 'body_content');
Your view will have a saved variable called `body_content`. You can then render your layout by doing:
Flight::render('layout');
Your layout template file could look like this:
<html>
<head>
</head>
<body>
<?php echo $body_content; ?>
</body>
</html>
### Custom Views
Flight allows you to swap out the default view engine simply by registering your own view class.

@ -414,6 +414,7 @@ class Flight {
static::error($e);
}
catch (Exception $ex) {
error_log($ex->getMessage());
exit(
'<h1>500 Internal Server Error</h1>'.
'<h3>'.$ex->getMessage().'</h3>'.
@ -486,6 +487,7 @@ class Flight {
* @param object $ex Exception
*/
public static function _error(Exception $e) {
error_log($e->getMessage());
self::response(false)
->status(500)
->write(
@ -528,14 +530,11 @@ class Flight {
*
* @param string $file Template file
* @param array $data Template data
* @param string $layout Layout file
* @param string $key Content variable name
* @param string $key View variable name
*/
public static function _render($file, $data = null, $layout = null, $key = 'content') {
if ($layout !== null) {
$content = self::view()->fetch($file, $data);
self::view()->render($layout, array($key => $content));
public static function _render($file, $data = null, $key = null) {
if ($key !== null) {
self::view()->set($key, self::view()->fetch($file, $data));
}
else {
self::view()->render($file, $data);

@ -47,6 +47,15 @@ class View {
}
}
/**
* Checks if a template variable is set.
*
* @param string $key Key
*/
public function has($key) {
return !empty($this->data[$key]);
}
/**
* Unsets a template variable. If no key is passed in, clear all variables.
*

Loading…
Cancel
Save