Skip to content
Devin Smith edited this page Feb 3, 2016 · 19 revisions

Tipsy comes with a phtml template system. It supports layouts as well as cascading include directories.


Examples

Single view with no layout

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>!

Adding a layout

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>

Returning output

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
		]);
	});

Two way data binding

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!'?>

Displaying a partial template

By accessing the $this variable you can access the current $View service.

render

Renders a file and returns as string

<?=$this->render('file', ['user' => 'devin'])?>
display

Calls the render function and prints the output

<? $this->display('file', ['user' => 'devin']); ?>
$include

Renders a file with the current scope and returns the output

<?=$include('file', ['user' => 'devin'])?>

Adding paths in config

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