Skip to main content
Version: Fegg 5.x (next)

Application::displayTemplate

Output a template with only the variables you pass

Description​

public void Application::displayTemplate( string $template, [ array $assignedValue = array() ] )

Finds the template (site/template/ first, then core/template/), compiles it when the cache is missing or out of date, and outputs it. Only the variables in $assignedValue are available: $page, $site, $in … are not set. Use Application::displayPage for pages.

The template engine itself uses this method for {{ include }} and {{ transclude }}. Inside a template, a name not starting with / is relative to the directory of the current template; from a controller, names are relative to the template root.

Arguments​

NameData typeDefaultRemarks
$templatestringTemplate name without .tpl (foo/bar)
$assignedValuearrayarray()Variables for the template: the key title becomes $title

Example​

htdocs/_fegg/site/controller/Foo.php
<?php
namespace Site\Controller;

defined('FEGG_CORE_DIR') || exit;

class Foo extends Controller
{
public function bar()
{
$this->displayTemplate('foo/bar', array(
'title' => 'Hello',
'items' => array('a', 'b'),
));
}
}
htdocs/_fegg/site/template/foo/bar.tpl
<h1>{{ $title }}</h1>
{{ foreach $items as $item }}<p>{{ $item }}</p>{{ end foreach }}
{{ if $page.baz }}not shown: $page is not set{{ end }}
Output
<h1>Hello</h1>
<p>a</p><p>b</p>
note

Do not include the .tpl extension in the name. A missing template throws Fegg\Template\TemplateException (see Lint and errors).