Chapter 6. Actions and templates

Table of Contents

Introduction
Actions
Templates
Calling actions or templates programmatically
Executing actions
Rendering templates

Introduction

Without using Atomik, one way of doing things would have been to create a file per page. The page logic (i.e. connecting to a database, handling form data...) would have been at the top of the file followed by the HTML.

Example 6.1. A php script, the old way

				
<?php
	if (count($_POST)) {
		echo 'Form data received!';
	}
?>
<form>
	<input type="text" name="data" />
	<input type="submit" value="send" />
</form>
			

This is BAD!! The application logic and the presentation layer should always be separated.

Now let's imagine that rather than directly doing both part in the same file we split it. We would have three file: one with the logic, one with the HTML and on that include both.

Example 6.2. Splitting into multiple files

page_logic.php

				
<?php
if (count($_POST)) {
	echo 'Form data received!';
}
			

page_html.php

				
<form>
	<input type="text" name="data" />
	<input type="submit" value="send" />
</form>
			

page.php

				
<?php
include 'page_logic.php';
include 'page_html.php';
			

Now replace the third file (the one with the includes) with Atomik and you'll have the concept behind Atomik. The logic script is named an action and the html a template.

Actions

Actions are stored in the app/actions directory. Both the action and the template filename must have the same name.

The content of the action file is free. It can be anything you want. So you just do your logic as you used to.

Note

Be aware that action run in their own scope and not in the global scope as you might think.

Variables declare in the action are forwarded to the template. If you want to keep some variables private (i.e. which will only be available in your action) prefixed them with an underscore.

Example 6.3. Private variables in actions

				
<?php
$myPublicVariable = 'value';
$_myPrivateVariable = 'secret';
			

Note

You shouldn't use echo or write any HTML code inside an action. As said before, the goal of an action is to separate the logic from the presentation. Thus mix-in both in the action file would be a non-sense!

Templates

Templates are stored in the app/templates directory.

The content of the template file is, as the action file, free. It should mostly be text or HTML (or any presentation content, such as XML).

PHP can be used to print variables from the action or to provide presentation logic like loops.

Example 6.4. Example template

				
<html>
	<head>
		<title>Example</title>
	</head>
	<body>
		<?php echo $myPublicVariable; ?>
	</body>
</html>
			

Figure 6.1. A little schema to sum up!

A little schema to sum up!

Calling actions or templates programmatically

When executing a request, the action and/or the template associated to it are automatically called. You can however call other actions or render other templates using Atomik's API.

The most useful use of this it to render partial templates, small part of presentation code that is reusable.

Executing actions

To execute an action use the Atomik::execute() method. It takes as first argument the action name.

By default, if a template with the same name is found, it is rendered and the return value of the execute() method is the template output.

If no template is found or false is used as second argument, the return value is an array containing all “public” variables from the action.

Example 6.5. Calling an action programmatically

					
$templateOutput = Atomik::execute('myAction');
$variables = Atomik::execute('myAction', false);
				

Rendering templates

To render a template use the Atomik::render() method.

It takes as first argument the template name and optionally as second argument an array of key/value pairs representing variables.

The method returns the template output.

Example 6.6. Rendering a template programmatically

					
$templateOutput = Atomik::render('myTemplate');
$templateOutput = Atomik::render('myTemplate', array('var' => 'value'));
				

For more information on both methods, see the API reference.