-
Notifications
You must be signed in to change notification settings - Fork 11
Views
Devin Smith edited this page Nov 29, 2015
·
19 revisions
Tipsy does not come with a complicated tempting engine. All templates are phtml. It supports layouts as well as cascading include directories.
By default the path is the current directory. But you can set it to anything you want.
// Set the path of the view
$tipsy->config(['view' => [
'path' => 'views'
]]);
// Set up the router
$tipsy->router()
->when('/', function($View, $Scope) {
$Scope->user = 'Devin';
$View->display('home');
});
<?/* view located in views/home.phtml */?>
Welcome <b><?=$user?></b>!
By default Tipsy will look for layout.phtml in your view path, but you can set it to anything you want.
// Set the path of the view and layout
$tipsy->config(['view' => [
'path' => 'views',
'layout' => 'awesome-layout'
]]);
<?/* layout located in views/awesome-layout.phtml */?>
<title>Awesome</title>
<h1>Awesome.sauce</h1>
<div class="content">
<?=$this->content?>
</div>
The view service supports both display, which outputs, and render, which returns the output to a variable.
$tipsy->router()
->when('/chat/reply', function($View) {
$output = $View->render('reply');
echo json_encode([
'time' => date('Y-m-d H:i:s'),
'html' => $output
]);
});
All variables are passed by reference so you can change anything in, and out of the templates.
// Router PHP
$tipsy->router()
->when('chat', function($Scope, $View) {
$Scope->message = 'Hello!';
$View->display('chat'); // will output "<b>Hello!</b>"
echo $Scope->message; // will output "Goodbye!"
});
<? /* chat.phtml */ ?>
<b><?=$message?></b>
<? $message = 'Goodbye!'?>
By accessing the $this variable you can access the current $View service.
<b>Some stuff</b>
<? $this->display('stuff'); ?>
You can set config either using the methods above, or using an ini file. For more information on config, see Config.
// Tell tipsy to read the config file
$tipsy->config('config.ini');
; File located at config.ini
[view]
path=views
layout=layout
stack[]=main/english
stack[]=main/spanish
stack[]=cobrand/english
stack[]=cobrand/spanish
- Home
- Getting Started
- Server Config
- Installation
- Installing Composer
- App
- Route Shorthand
- Config
- Routes
- Methods
- Controller Types
- Params & Regex
- Aliases
- Dependency Injection
- Advanced Routing
- Services
- User Defined Services
- Built in Services
- Middleware
- Views
- Templates
- Scope
- Resource
- Factory
- Looper
- Examples
- Plugins
- About