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. You can specify named parameters in routes which will be passed along to your callback function.
Flight::route('/@name/@id', function($name, $id){ 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: 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 // Map your method
Flight::map('hello', function($name){ Flight::map('hello', function($name){
echo "hello {$name}!"; echo "hello $name!";
}); });
// Call your custom method // Call your custom method
@ -260,7 +260,7 @@ Here's an example the filtering process:
// Map a custom method // Map a custom method
Flight::map('hello', function($name){ Flight::map('hello', function($name){
return "Hello, {$name}!"; return "Hello, $name!";
}); });
// Add a before filter // Add a before filter
@ -332,7 +332,7 @@ Flight also uses variables for configuration purposes.
## Views ## 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: name of the template file and optional template data:
Flight::render('hello.php', array('name', 'Bob')); Flight::render('hello.php', array('name', 'Bob'));
@ -346,11 +346,42 @@ The output would be:
Hello, Bob! 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 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: by setting the following config:
Flight::set('flight.views.path', '/path/to/views'); 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 ### Custom Views
Flight allows you to swap out the default view engine simply by registering your own view class. 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); static::error($e);
} }
catch (Exception $ex) { catch (Exception $ex) {
error_log($ex->getMessage());
exit( exit(
'<h1>500 Internal Server Error</h1>'. '<h1>500 Internal Server Error</h1>'.
'<h3>'.$ex->getMessage().'</h3>'. '<h3>'.$ex->getMessage().'</h3>'.
@ -486,6 +487,7 @@ class Flight {
* @param object $ex Exception * @param object $ex Exception
*/ */
public static function _error(Exception $e) { public static function _error(Exception $e) {
error_log($e->getMessage());
self::response(false) self::response(false)
->status(500) ->status(500)
->write( ->write(
@ -528,14 +530,11 @@ class Flight {
* *
* @param string $file Template file * @param string $file Template file
* @param array $data Template data * @param array $data Template data
* @param string $layout Layout file * @param string $key View variable name
* @param string $key Content variable name
*/ */
public static function _render($file, $data = null, $layout = null, $key = 'content') { public static function _render($file, $data = null, $key = null) {
if ($layout !== null) { if ($key !== null) {
$content = self::view()->fetch($file, $data); self::view()->set($key, self::view()->fetch($file, $data));
self::view()->render($layout, array($key => $content));
} }
else { else {
self::view()->render($file, $data); 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. * Unsets a template variable. If no key is passed in, clear all variables.
* *

Loading…
Cancel
Save