メインコンテンツまでスキップ
Version: Fegg 5.x(開発版)

Application::displayPage

Display a page with the standard template variables and end the response

Description​

public void Application::displayPage( string $template )

Renders the template with Application::fetchPage (so $page, $site, $in … are available), outputs the page and ends the response by throwing Fegg\Http\Halt (not exit; see Ending a response). Code after displayPage() does not run. A catch (\Throwable $e) around it must rethrow Fegg\Http\Halt.

The security headers (X-Frame-Options: SAMEORIGIN, X-Content-Type-Options: nosniff, Referrer-Policy ...) are sent for every response before the controller runs, not by displayPage() (see Security). To let other sites show one page in a frame, call $this->allowFraming() before displayPage().

This is the method to use for every page. The site-wide Basic authentication (basic_auth_scope = 1) is checked earlier, before the controller runs.

Arguments​

NameData typeDefaultRemarks
$templatestringTemplate name relative to the template root, without .tpl (news/detail)

Example​

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

defined('FEGG_CORE_DIR') || exit;

class News extends Controller
{
public function detail($id = '')
{
$this->page['title'] = 'Hello';
$this->displayPage('news/detail');
}
}
htdocs/_fegg/site/template/news/detail.tpl
<h1>{{ $page.title }}</h1>
Output
<h1>Hello</h1>

Errors​

When the template does not exist, Fegg\Template\TemplateException is thrown. Uncaught, it becomes an HTTP 500 that is written to the PHP error log; nothing of the half-rendered page is sent. Catch it when a missing template is expected:

try {
$this->displayPage('campaign/current');
} catch (\Fegg\Template\TemplateException $e) {
$this->displayPage('not_found');
}
警告

Never build the template name from user input without checking it. A controller that chooses a template from its URL arguments should only be reachable through site/config/route.php (implement Fegg\Http\RouteOnly, like the skeleton's Site\Controller\Page).