From d32e36ab4b151ba90a7674e9eb1b3b5732cf8a5e Mon Sep 17 00:00:00 2001 From: shoully Date: Sat, 20 Dec 2014 06:01:13 -0800 Subject: [PATCH] Updated Views (markdown) --- Views.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/Views.md b/Views.md index 25a2620..5a3840d 100644 --- a/Views.md +++ b/Views.md @@ -22,4 +22,79 @@ 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'); \ No newline at end of file + 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: + +

+ +body.php: + +
+ +layout.php: + + + + <?php echo $title; ?> + + + + + + + +The output would be: + + + + Home Page + + +

Hello

+
World
+ + + +## 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); + }); \ No newline at end of file