Updated Views (markdown)

master
shoully 10 years ago
parent 5da13626e7
commit d32e36ab4b

@ -23,3 +23,78 @@ Note that when specifying the name of the template in the render method, you can
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 for websites to have a single layout template file with interchanging content. To render content to be used in a layout, you can pass in an optional parameter to the `render` method.
Flight::render('header', array('heading' => 'Hello'), 'header_content');
Flight::render('body', array('message' => 'World'), 'body_content');
Your view will then have saved variables called `header_content` and `body_content`. You can then render your layout by doing:
Flight::render('layout', array('title' => 'Home Page'));
If the template files looks like this:
header.php:
<h1><?php echo $heading; ?></h1>
body.php:
<div><?php echo $body; ?></div>
layout.php:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $header_content; ?>
<?php echo $body_content; ?>
</body>
</html>
The output would be:
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Hello</h1>
<div>World</div>
</body>
</html>
## Custom Views
Flight allows you to swap out the default view engine simply by registering your own view class.
Here's how you would use the [Smarty](http://www.smarty.net/) template engine for your views:
// Load Smarty library
require './Smarty/libs/Smarty.class.php';
// Register Smarty as the view class
// Also pass a callback function to configure Smarty on load
Flight::register('view', 'Smarty', array(), function($smarty){
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty->config_dir = './config/';
$smarty->cache_dir = './cache/';
});
// Assign template data
Flight::view()->assign('name', 'Bob');
// Display the template
Flight::view()->display('hello.tpl');
For completeness, you should also override Flight's default render method:
Flight::map('render', function($template, $data){
Flight::view()->assign($data);
Flight::view()->display($template);
});
Loading…
Cancel
Save