Epesi's template system is built on Smarty. If you already know Smarty, there's just one more thing you need: how to start the Smarty engine from within Epesi.
A template is a file containing your module's HTML, with placeholders you fill in with data from PHP. Here's a simple example:
<html>
<head>
<title>{$title}</title>
</head>
<body>
<h1>{$title}</h1>
<p>{$content}</p>
</body>
</html>
It looks like a regular HTML file, except for the {$title} and {$content} placeholders — Smarty replaces those with values you set in your PHP file when the template is processed. Keeping HTML in a separate file like this has real advantages over printing HTML directly from PHP.
First, it separates the design (HTML/CSS) from the code. That means you can change how a module looks just by editing its template file — which reads like ordinary HTML — without touching PHP. It also means someone who knows HTML but not PHP can customize a module's appearance.
Second, it keeps the PHP code itself cleaner: its only job is to supply data to the template.
This section covers how to start the template module and display it; the next section covers filling it with data.
To initialize the template module, create an instance of it:
$theme = & $this->init_module('Base/Theme');
To display it:
$theme->display();
This loads the module's default template — theme/default.tpl and theme/default.css.
Advanced
$theme->display($user_template, $fullname);
$user_template is the path to the template you want to load, given without its extension — Base/Theme tries to load both the .tpl and .css files for it.
$fullname controls how that path is resolved: when it's false, $user_template is expected to live inside the module's own theme directory; when it's true, you must give the full path to the template (still without an extension).
The examples below assume you have a Base/Theme instance in $theme, as set up in the previous section. Remember you still need to call display() to actually render the template.
Disclaimer
$theme is technically a reference to the main Smarty class, so most of what you already know about Smarty applies here too. See Smarty's own documentation for the full picture.
Examples
The examples below show a few common ways to use the templating system.
Simple replace
Template file:
<h1>{$title}</h1>
<p>{$content}</p>
PHP file:
$theme->assign('title', 'Testing title for tutorial');
$theme->assign('content', 'And some content...');
assign() tells the template engine to replace the variable named by its first argument with the value given as the second. Result:
<h1>Testing title for tutorial</h1>
<p>And some content...</p>
More complex replace
Template file:
<h1>{$article.title}</h1>
<p>{$article.content}</p>
PHP file:
$article = array(
'title' => 'Testing title for tutorial',
'content' => 'And some content...'
);
$theme->assign('article', $article);
Here we assign a whole array to a variable, and use a dot to access its elements. Result (same as above):
<h1>Testing title for tutorial</h1>
<p>And some content...</p>
Even more complex replace (foreach loop)
Template file:
<table>
{foreach from=$numbers_list item=row}
<tr>
<td>{$row.number}</td>
<td>{$row.square}</td>
</tr>
{/foreach}
</table>
PHP file:
$numbers = array();
for($i = 1; $i <= 3; $i++) {
array_push(
$numbers,
array(
'number' => $i,
'square' => $i*$i
)
);
}
$theme->assign('numbers_list', $numbers);
The foreach loop walks through $numbers. Each element is itself an array, so we use a dot to access its fields. Result:
<table>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>9</td>
</tr>
</table>