-
Notifications
You must be signed in to change notification settings - Fork 10
Views
Tipsy comes with a phtml template system. 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) {
$View->display('home', ['user' => 'Devin']);
});
<?/* 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.
Renders a file and returns as string
<?=$this->render('file', ['user' => 'devin'])?>
Calls the render
function and prints the output
<? $this->display('file', ['user' => 'devin']); ?>
Renders a file with the current scope and returns the output
<?=$include('file', ['user' => 'devin'])?>
You can set config either using the methods above, or using an config 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