フィルター

Twigテンプレートで利用できるフィルター(モディファイア)について説明します。
※ ドキュメントの準備ができていない項目については、Twigの公式サイトへのリンクを用意しています。

フィルター一覧

abs

https://twig.symfony.com/doc/3.x/filters/abs.html

basename ME拡張

パス(URL)の最後にあるファイル名(拡張子を含む)部分を返します。 内部ではPHPの basename 関数を利用しています。

batch

https://twig.symfony.com/doc/3.x/filters/batch.html

capitalize

https://twig.symfony.com/doc/3.x/filters/capitalize.html

column

https://twig.symfony.com/doc/3.x/filters/column.html

convert_encoding

https://twig.symfony.com/doc/3.x/filters/convert_encoding.html

date

https://twig.symfony.com/doc/3.x/filters/date.html

date_admin ME拡張

管理画面用の日付フォーマットに変換します。
※この関数は主に管理画面での利用を想定しています。

date_admin

パラメータ:
ありません。

例:

{{ page.updated_at|date_admin }}

管理画面用日付フォーマットは settings.php の date_admin 項目で指定します。 初期値は 'Y/m/d H:i:s' です。 カレンダーなどでは個別のフォーマットを指定している箇所があります。

date_local ME拡張

日時をロケール設定にもとづいてフォーマットします。 内部では PHP の strftime 関数を利用しています。 このフィルターを活用するためには、settings.php の locale 項目にシステムで有効なロケール情報が設定されている必要があります。

date_local(format)

パラメータ:
format フォーマット文字列。指定できるフォーマットは PHP の strftime 関数と同じです。次のページを参考にしてください。 https://secure.php.net/manual/ja/function.strftime.php

例:

{{ calendar.date|date_local('%Y年%-m月%-d日 (%a)') }}

出力例:

2018年2月20日 (火)

date_local_long ME拡張

ローカル日付フォーマット(ロング)設定に日付を変換します。

date_local_long

パラメータ:
ありません。

例:

{{ page.updated_at|date_local_long }}

ローカル日付フォーマット(ロング)設定は settings.php の date_local_long 項目で指定します。 初期値は '%Y年%m月%d日(%a) %H時%M分' です。

date_local_short ME拡張

ローカル日付フォーマット(ショート)設定に日付を変換します。

date_local_short

パラメータ:
ありません。

例:

{{ page.updated_at|date_local_short }}

ローカル日付フォーマット(ショート)設定は settings.php の date_local_short 項目で指定します。 初期値は '%Y年%m月%d日(%a)' です。

date_long ME拡張

日付フォーマット(ロング)設定に日付を変換します。

date_long

パラメータ:
ありません。

例:

{{ page.updated_at|date_long }}

日付フォーマット(ロング)設定は settings.php の date_long 項目で指定します。 初期値は 'Y/m/d H:i' です。

date_modify

https://twig.symfony.com/doc/3.x/filters/date_modify.html

date_short ME拡張

日付フォーマット(ショート)設定に日付を変換します。

date_short

パラメータ:
ありません。

例:

{{ page.updated_at|date_short }}

日付フォーマット(ショート)設定は settings.php の date_short 項目で指定します。 初期値は 'Y/m/d' です。

default

https://twig.symfony.com/doc/3.x/filters/default.html

escape

https://twig.symfony.com/doc/3.x/filters/escape.html

filesize_format ME拡張

ファイルサイズの単位を変換します。

filesize_format

パラメータ:
ありません。

例:

{{ file.size|filesize_format }}

たとえば、file.size が 1363896 という数値を持つ場合は、「1 MB」と表示されます。 対応している単位は、'バイト' と 'KB', 'MB', 'GB', 'TB', 'PB' です。
※数値は2進接頭辞として計算しています。

first

https://twig.symfony.com/doc/3.x/filters/first.html

format

https://twig.symfony.com/doc/3.x/filters/format.html

join

https://twig.symfony.com/doc/3.x/filters/join.html

json_encode

https://twig.symfony.com/doc/3.x/filters/json_encode.html

keys

https://twig.symfony.com/doc/3.x/filters/keys.html

last

https://twig.symfony.com/doc/3.x/filters/last.html

length

https://twig.symfony.com/doc/3.x/filters/length.html

lower

https://twig.symfony.com/doc/3.x/filters/lower.html

map

https://twig.symfony.com/doc/3.x/filters/map.html

merge

https://twig.symfony.com/doc/3.x/filters/merge.html

nl2br

https://twig.symfony.com/doc/3.x/filters/nl2br.html

number_format

https://twig.symfony.com/doc/3.x/filters/number_format.html

raw

https://twig.symfony.com/doc/3.x/filters/raw.html

reduce

https://twig.symfony.com/doc/3.x/filters/reduce.html

recent_updates ME拡張

日時が5分以内か当日の場合にそれぞれに対応したクラス文字列を出力します。 このフィルターは主に管理画面で利用するために用意されました。

recent_updates(today, flesh)

パラメータ:
today 当日を表すクラス文字列を指定します。デフォルト値は "today" です。
flesh 5分以内に更新されたことを表すクラス文字列を指定します。デフォルト値は "flesh" です。

例:

<tr class="{{ page.updated_at|recent_updates }}">

日時データが現在の5分以内の場合の出力例:

<tr class="flesh">

日時データが当日の場合の出力例:

<tr class="today">

replace

https://twig.symfony.com/doc/3.x/filters/replace.html

resize ME拡張

画像をリサイズ用のファイル名に変換します。 ファイルディレクトリ(/files/)に配置した画像ファイルは、ルールに従いファイル名を書き換えることでリサイズすることができます。 resizeフィルターを使うと、リサイズに対応したファイル名に書き換えることができます。
※ リサイズの仕組みについてはファイルコンテンツの「画像のリサイズ」をご覧ください。

resize(path, option)

パラメータ:
option リサイズ名

例:

<img src="{{ item.fields.image.src|resize('thumb') }}" alt="">

出力例:

<img src="/files/img/flower_thumb.jpg" alt="">

item.fields.image.src の値が "/files/img/flower.jpg" の場合は ファイル名と拡張子の間に _thumb が追加されます。

reverse

https://twig.symfony.com/doc/3.x/filters/reverse.html

round

https://twig.symfony.com/doc/3.x/filters/round.html

slice

https://twig.symfony.com/doc/3.x/filters/slice.html

sort

https://twig.symfony.com/doc/3.x/filters/sort.html

spaceless

https://twig.symfony.com/doc/3.x/filters/spaceless.html

split

https://twig.symfony.com/doc/3.x/filters/split.html

striptags

https://twig.symfony.com/doc/3.x/filters/striptags.html

then ME拡張

値が空ではない場合に渡された文字列を返します。

then(value)

パラメータ:
value 判定する値

例:

<td class="{{ day.today|then('today') }}">

day.today が空ではない場合は 「today」という文字列を返します。

出力例

<td class="today">

thenフィルターを使わずに if と endif タグで同様の結果を得たい場合の記述は次のようになります。

<td class="{% if day.today %}today{% endif %}">

title

https://twig.symfony.com/doc/3.x/filters/title.html

trim

https://twig.symfony.com/doc/3.x/filters/trim.html

upper

https://twig.symfony.com/doc/3.x/filters/upper.html

url_encode

https://twig.symfony.com/doc/3.x/filters/url_encode.html

このHTMLファイルについて

このHTMLファイルは Twig Team の著作物を複製・改変したドキュメントが含まれています。

著作物:https://twig.symfony.com/
著作権:Copyright (c) 2009-2020 by the Twig Team
ライセンス:https://twig.symfony.com/license