Documentation

You are viewing the documentation for Play 1. The documentation for Play 2 is here.

Java エクステンション

Java エクステンションは、ビューテンプレートで使う便利なメソッドをオブジェクトに追加し、式をより表現力豊かにします。

コードサンプルは、テンプレートにおける式の例と、その結果を次の行に示しています。

コレクションの拡張

join(separator)

コレクションの要素それぞれの間を与えられたセパレータで連結します。返却型: String

${['red', 'green', 'blue'].join('/')}
red/green/blue

last()

リストの最後のアイテムを返します。返却型: Object

${['red', 'green', 'blue'].last()}
blue

pluralize()

コレクションのサイズが 1 でない場合に ‘s’ を返します。返却型: String

colour${['red', 'green', 'blue'].pluralize()} 
colours

pluralize(plural)

コレクションのサイズが 1 でない場合に、与えられた複数形を返します。返却型: String

box${['red', 'green', 'blue'].pluralize('es')}
boxes

pluralize(singular, plural)

コレクションのサイズが 1 でない場合に与えられた複数形を返します; コレクションのサイズが 1 の場合は与えられた単数形を返します。返却型: String

journ${['red'].pluralize('al', 'aux')}
journal
 
journ${['red', 'green', 'blue'].pluralize('al', 'aux')}
journaux

日付の拡張

format(format)

与えられたフォーマットパターンで日付をフォーマットします。返却型: String

${new Date(1275910970000).format('dd MMMM yyyy hh:mm:ss')}
07 June 2010 01:42:50

format(format, language)

与えられたフォーマットパターンと言語で日付をフォーマットします。返却型: String

${new Date(1275910970000).format('dd MMMM yyyy hh:mm:ss', 'fr')}
07 juin 2010 01:42:50

since()

日付を現在と比較した相対値、例えば 3 minutes ago としてフォーマットします。返却型: String

${new Date(new Date().getTime() - 1000000).since()}
16 minutes ago

次のメッセージを使って出力をカスタマイズすることができます: since.seconds, since.minutes, since.hours, since.days, since.months and since.years.

since(condition)

日付を現在と比較した相対値としてフォーマットします。条件が true の場合、一ヶ月より前の日付は日付としてフォーマットされます。返却型: String

since() と同じメッセージを使って出力をカスタマイズすることができるのと同様に、一ヶ月より前の日付について since.format を使うことができます。

${new Date(1262350000000).since(false)}
5 months ago
 
${new Date(1262350000000).since(true)}
Jan 1, 2010

Long の拡張

asdate(format)

タイムスタンプを日付としてフォーマットします。返却型: String - フォーマットされた日付

${1275910970000.asdate('dd MMMM yyyy hh:mm:ss')}
07 June 2010 01:42:50

asdate(format, language)</a

タイムスタンプを与えられた言語における日付としてフォーマットします。返却型: String - フォーマットされた日付

${1275910970000.asdate('dd MMMM yyyy hh:mm:ss', 'fr')}
07 juin 2010 01:42:50

formatSize()

バイト数を単位の付いたファイルサイズとしてフォーマットします。

${726016L.formatSize()}
709KB

Map の拡張

asAttr()

マップのキーと値を HTML の属性としてフォーマットします。返却型: play.templates.Template.ExecutableTemplate.RawData.

${[id:'42', color:'red'].asAttr()}
id="42" color="red" 

asAttr(condition)

条件が ture の場合、マップのキーと値を HTML の属性としてフォーマットします。返却型: play.templates.Template.ExecutableTemplate.RawData.

${[id:'42', color:'red'].asAttr(true)}
id="42" color="red" 

Number の拡張

divisibleBy(divisor)

与えられた数字で割り切れる場合に true を返します。返却型: boolean

${42.divisibleBy(7)}
true

format(format)

与えられたフォーマットパターンで数字をフォーマットします。返却型: String

${42.format('000.00')}
042.00

formatCurrency(currencyCode)</a

与えられた通貨コード、例えば EUR で数字を通貨としてフォーマットします。返却型: String

${42.formatCurrency('EUR').raw()}
&euro; 42.00

page(pageSize)

インデックスとして解釈した数字の、与えられたページ数に対するページ数を返します。返却型: String

${42.page(10)}
5

pluralize()

数字が 1 でない場合に ‘s’ を返します。返却型: String

colour${['red', 'green', 'blue'].pluralize()} - colour${3.pluralize()} 
colours - colours

pluralize(plural)

数字が 1 でない場合に与えられた複数形を返します。返却型: String

box${3.pluralize('es')}
boxes

pluralize(singular, plural)

数字が 1 でない場合に与えられた複数形を返します; 数字が 1 の場合は与えられた単数形を返します。返却型: String

journ${1.pluralize('al', 'aux')}
journal
 
journ${3.pluralize('al', 'aux')}
journaux

オブジェクトの拡張

addSlashes()

Java においてエスケープされたオブジェクトの 文字列 表現に含まれるシングルクォーテーションとダブルクォーテーションを、バックスラッシュでエスケープします。返却型: String

${"single quote (')".addSlashes().raw()} ${'double quote (")'.addSlashes().raw()}
single quote (\') double quote (\")

capAll()

オブジェクトの 文字列 表現に含まれるすべての単語の先頭を大文字にします。返却型: String

${"lorum ipsum dolor".capAll()}
Lorum Ipsum Dolor

capFirst()

オブジェクトの 文字列 表現に含まれる最初の単語の先頭を大文字にします。返却型: String

${"lorum ipsum dolor".capFirst()}
Lorum ipsum dolor

cut(substring)

与えられた部分文字列を取り除きます。返却型: String

${"lorum ipsum dolor".cut('um')}
lor ips dolor

escape()

オブジェクトの 文字列 表現に含まれる HTML 文字をエスケープします。返却型: String

${"The <blink>tag</blink> is evil".escape().raw()}
The &lt;blink&gt;tag&lt;/blink&gt; is evil

nl2br()

改行文字を HTML の br タグで置き換えます。返却型: String

${"one\ntwo".nl2br()}
one<br/>two

この出力は HTML-エスケープされないので、 出力中の br タグ がエスケープされていないことに注意してください。これは、おそらくユーザの入力に対して escape() を使用したくなることを意味します:

${userInput.escape().nl2br()}

raw()

オブジェクトをテンプレートエスケープせずに返します。返却型: play.templates.Template.ExecutableTemplate.RawData

${'<'}
&lt;
 
${'<'.raw()}
<

raw(condition)

条件が true の場合、オブジェクトをテンプレートエスケープせずに返します。返却型: play.templates.Template.ExecutableTemplate.RawData

${'<'.raw(true)}
<

yesNo(‘yes’, ‘no’)

オブジェクトが true と評価される場合は最初のパラメータ (‘yes’) を、そうでない場合は二番目のパラメータ (‘no’) を返します。返却型: String

${"".yesno('yes', 'no')}
no
 
${"not empty".yesno('yes', 'no')}
yes

String の拡張

asXml()

与えられた XML 文字列を解析します。返却型: groovy.util.slurpersupport.GPathResult

camelCase()

文字列を Java クラス名のようにキャメルケースとしてフォーマットします。返却型: String

${"lorum ipsum dolor".camelCase()}
LorumIpsumDolor

capitalizeWords()

文字列に含まれるすべての単語の先頭を大文字にします。返却型: String

${"lorum ipsum dolor".capitalizeWords()}
Lorum Ipsum Dolor

escapeHtml()

与えられた HTML 文字をエスケープします。返却型: String

${"The <blink>tag</blink> is evil".escapeHtml().raw()}
The &lt;blink&gt;tag&lt;/blink&gt; is evil

escapeJavaScript()

与えられた JavaScript 文字をエスケープします。返却型: String

${"single quote (') double quote (\")".escapeJavaScript().raw()}
single quote (\') double quote (\")

escapeXml()

与えられた XML 文字をエスケープします。返却型: String

${"<>\"&".escapeXml().raw()}
&lt;&gt;&quot;&amp;

noAccents()

文字列に含まれる文字からアクセントを取り除きます。返却型: String

${"Stéphane Épardaud".noAccents()}
Stephane Epardaud

pad(length)

与えられた長さだけ文字列に nbsp; を付け足します。返却型: String

${"x".pad(4).raw()}
x&nbsp;&nbsp;&nbsp;

slugify()

URL パスとして予約されている文字に沿うように、文字列を URL に使用できる ‘俗語’ としてフォーマットします。返却型: String

${"The Play! framework’s manual".slugify()}
the-play-framework-s-manual

urlEncode()

与えられた URL クエリ文字列をエスケープします。返却型: String

${"!@#\$%^&()".urlEncode()}
%21%40%23%24%25%5E%26%28%29

String 配列の拡張

add(value)

配列の終端に値を追加します。返却型: String[]

${(["red", "green", "blue"] as String[]).add('pink').join(' ')}
red green blue pink

contains(string)

配列が与えられた文字列を含む場合は true を返します。返却型: boolean

${(['red', 'green', 'blue'] as String[]).contains('green')}
true

remove(string)

与えられた文字列を取り除いた配列を返します。返却型: String[]

${(['red', 'green', 'blue'] as String[]).remove('green').join(' ')}
red blue