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

Code

Run a PHP statement

Description​

{{ code statement }}

Runs PHP statements in the template. Nothing is output unless the statement itself prints something.

  • Template variables are written as usual ($page.data, $categories). A variable assigned in {{ code }} can be used by the following tags.
  • Several statements can be separated with ;. The code cannot contain { or }, so blocks such as if (...) { } are not possible: use {{ if }} and {{ foreach }} instead.
  • Unlike {{ if }} and {{ assign }}, the code is not null-safe: on PHP 8, reading a variable that is not set raises a warning, which Fegg's error handler turns into an exception (HTTP 500).

Keep logic in the controller. {{ code }} is for small things such as calling a helper function; for a value, prefer {{ assign }}.

Example​

Template
{{ code $categories = feggCategories('news') }}
<select name="category">
{{ foreach $categories as $key => $label }}
<option value="{{ $key }}"{{ selected key=$key value=$in.category }}>{{ $label }}</option>
{{ end foreach }}
</select>

feggCategories() returns the list news of site/config/category_info.php.

Template
<a href="{{ $site.prefix }}?p={{ $page.pager.next_page }}&{{ code echoSearchQuery() }}">Next</a>

echoSearchQuery() prints the current request parameters (without p) as a query string.

警告

Templates are code: {{ code }} runs any PHP. Only developers may edit templates. See Security.