Velocity

Velocityについて

コミュニティ

ドキュメント

ツール

比較

日本語訳について

About this Guide − このガイドについて

This guide is the reference for the Velocity Template Language (VTL). For more information, please also refer to the Velocity User Guide.

このガイドは、Velocity テンプレート言語 (VTL) のためのリファレンスです。 詳細は、Velocity ユーザガイドを参照してください。


References − リファレンス
Variables − 変数

Notation:

$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]

表記:

$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]

Examples:

  • Normal notation: $mud-Slinger_9
  • Silent notation: $!mud-Slinger_9
  • Formal notation: ${mud-Slinger_9}

例:

  • 通常表記: $mud-Slinger_9
  • 沈黙表記: $!mud-Slinger_9
  • 正式表記: ${mud-Slinger_9}

Properties − プロパティ

Notation:

$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]

表記:

$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]

Examples:

  • Regular Notation: $customer.Address
  • Formal Notation: ${purchase.Total}

例:

  • 通常表記: $customer.Address
  • 正式表記: ${purchase.Total}

Methods − メソッド

Notation:

$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ opional parameter list... ] ) [ } ]

表記:

$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ optional parameter list... ] ) [ } ]

Examples:

  • Regular Notation: $customer.getAddress()
  • Formal Notation: ${purchase.getTotal()}
  • Regular Notation with Parameter List: $page.setTitle( "My Home Page" )

例:

  • 通常表記: $customer.getAddress()
  • 正式表記: ${purchase.getTotal()}
  • 通常表記(パラメータリスト付き): $page.setTitle( "My Home Page" )

VTL Properties can be used as a shorthand notation for VTL Methods that take get and set. Either $object.getMethod() or $object.setMethod() can be abbreviated as $object.Method. It is generally preferable to use a Property when available. The main difference between Properties and Methods is that you can specify a parameter list to a Method.

VTL プロパティは、getset をとる VTL メソッドの簡略表記として使うことができます。 $object.getMethod()$object.setMethod() は、$object.Method と略記できます。 一般的に、利用できる場合はプロパティを使う方が好ましいです。 プロパティとメソッドの主な違いは、メソッドにはパラメータリストを指定できるということです。


Directives − 指示子
#set - Establishes the value of a reference − #set - リファレンスの値を設定

Format:

#set( $ref = [ ", ' ]arg[ ", ' ] )

書式:

#set( $ref = [ ", ' ]arg[ ", ' ] )

Usage:

  • $ref - The LHS of the assignment must be a variable reference or a property reference.
  • arg - The RHS of the assignment, arg is parsed if enclosed in double quotes, and not parsed if enclosed in single quotes. If the RHS evaluates to null, it is not assigned to the LHS.

使い方:

  • $ref - 左辺は、変数リファレンスまたはプロパティリファレンスでなければなりません。
  • arg - 右辺である arg は、 ダブルクォーテーションで囲まれた場合解析されますが、 シングルクォーテーションで囲まれた場合解析されません。 右辺が null と評価された場合、左辺には代入されません

Examples:

  • Variable reference: #set( $monkey = $bill )
  • String literal: #set( $monkey.Friend = 'monica' )
  • Property reference: #set( $monkey.Blame = $whitehouse.Leak )
  • Method reference: #set( $monkey.Plan = $spindoctor.weave($web) )
  • Number literal: #set( $monkey.Number = 123 )
  • Range operator: #set( $monkey.Numbers = [1..3] )
  • Object array: #set( $monkey.Say = ["Not", $my, "fault"] )

例:

  • 変数リファレンス: #set( $monkey = $bill )
  • 文字列リテラル: #set( $monkey.Friend = 'monica' )
  • プロパティリファレンス: #set( $monkey.Blame = $whitehouse.Leak )
  • メソッドリファレンス: #set( $monkey.Plan = $spindoctor.weave($web) )
  • 数値リテラル: #set( $monkey.Number = 123 )
  • 範囲演算子: #set( $monkey.Numbers = [1..3] )
  • オブジェクト配列: #set( $monkey.Say = ["Not", $my, "fault"] )

The RHS can also be a simple arithmetic expression, such as:

  • Addition: #set( $value = $foo + 1 )
  • Subtraction: #set( $value = $bar - 1 )
  • Multiplication: #set( $value = $foo * $bar )
  • Division: #set( $value = $foo / $bar )
  • Remainder: #set( $value = $foo % $bar )

右辺には、単純な算術式も記述できます:

  • 加算: #set( $value = $foo + 1 )
  • 減算: #set( $value = $bar - 1 )
  • 乗算: #set( $value = $foo * $bar )
  • 除算: #set( $value = $foo / $bar )
  • 剰余: #set( $value = $foo % $bar )

#if / #elseif / #else - output conditional on truth of statements − #if / #elseif / #else - 条件分岐

Format:

#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end

書式:

#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end

Usage:

  • condition - If a boolean, considered true if it has a true false; if not a boolean, considered true if not null.
  • output - May contain VTL.

使い方:

  • condition - boolean 型の場合、値そのもので true もしくは false と解釈されます。boolean 型以外の場合、null 以外が true と解釈されます。
  • output - VTL を含みます。

Examples:

  • Equivalent Operator: #if( $foo == $bar )
  • Greater Than: #if( $foo > 42 )
  • Less Than: #if( $foo < 42 )
  • Greater Than or Equal To: #if( $foo >= 42 )
  • Less Than or Equal To: #if( $foo <= 42 )
  • Equals Number: #if( $foo == 42 )
  • Equals String: #if( $foo == "bar" )
  • Boolean NOT: #if( !$foo )

例:

  • 等値演算子: #if( $foo == $bar )
  • より大きい: #if( $foo > 42 )
  • より小さい: #if( $foo < 42 )
  • 以上   : #if( $foo >= 42 )
  • 以下   : #if( $foo <= 42 )
  • 数値の等号: #if( $foo == 42 )
  • 文字列等号: #if( $foo == "bar" )
  • 論理否定 : #if( !$foo )

#foreach - Loops through a list of objects − #foreach - オブジェクトのリストでループ

Format:

#foreach( $ref in arg ) statement #end

書式:

#foreach( $ref in arg ) statement #end

Usage:

  • $ref - The first variable reference is the item.
  • arg - May be one of the following: a reference to a list (i.e. object array, collection, or map), an array list, or the range operator.
  • statement - What is output each time Velocity finds a valid item in the list denoted above as arg. This output is any valid VTL and is rendered each iteration of the loop.

使い方:

  • $ref - 最初の変数リファレンスは、リストの項目です。
  • arg - リストのリファレンス(つまりオブジェクト配列/コレクション/マップ)、 配列リスト、範囲演算子のいずれかです。
  • statement - 上記で arg と示されたリスト内にある、 有効なアイテムの分だけ出力する内容です。 この出力は有効なVTLなら何でもよく、ループの繰り返し毎に処理されます。

Examples of the #foreach(), omitting the statement block :

  • Reference: #foreach ( $item in $items )
  • Array list: #foreach ( $item in ["Not", $my, "fault"] )
  • Range operator: #foreach ( $item in [1..3] )

#foreach() の例を示します(statement ブロックは省略しています):

  • リファレンス: #foreach ( $item in $items )
  • 配列リスト: #foreach ( $item in ["Not", $my, "fault"] )
  • 範囲演算子: #foreach ( $item in [1..3] )

Velocity provides an easy way to get the loop counter so that you can do something like the following:

Velocity は、以下のようにループ回数を取得する簡単な方法を提供します:

<table>
#foreach( $customer in $customerList )
    <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>

The default name for the loop counter variable reference, which is specified in the velocity.properties file, is $velocityCount. By default the counter starts at 1, but this can be set to either 0 or 1 in the velocity.properties file. Here's what the loop counter properties section of the velocity.properties file appears:

ループカウンタの変数リファレンスのデフォルトの名前は、$velocityCount です。 名前は velocity.properties ファイルで指定することが出来ます。 デフォルトで、カウンタは 1 から開始しますが、 velocity.properties ファイルで 0 や 1 にセットできます。 ここで velocity.properties ファイルに現れるループカウンタの プロパティセクションを示します。

# Default name of the loop counter
# variable refernce.
counter.name = velocityCount

# Default starting value of the loop
# counter variable reference.
counter.initial.value = 1
# ループカウンタ変数リファレンスのデフォルト名.
counter.name = velocityCount

# ループカウンタ変数リファレンスのデフォルト開始値.
counter.initial.value = 1

#include - Renders local file(s) that are not parsed by Velocity − #include - ローカルファイルを処理するが、Velocity に解析させない

Format:

#include( arg[, arg2, ... argn] )

  • arg - Refers to a valid file under TEMPLATE_ROOT.

書式:

#include( arg[, arg2, ... argn] )

  • arg - TEMPLATE_ROOT 下で有効なファイルを参照します。

Examples:

  • String: #include( "disclaimer.txt", "opinion.txt" )
  • Variable: #include( $foo, $bar )

例:

  • 文字列: #include( "disclaimer.txt", "opinion.txt" )
  • 変数: #include( $foo, $bar )

#parse - Renders a local template that is parsed by Velocity − #parse - ローカルテンプレートを処理して Velocity に解析させる

Format:

#parse( arg )

  • arg - Refers to a template under TEMPLATE_ROOT.

書式:

#parse( arg )

  • arg - TEMPLATE_ROOT 下で有効なファイルを参照します。

Examples:

  • String: #parse( "lecorbusier.vm" )
  • Variable: #parse( $foo )

例:

  • 文字列: #parse( "lecorbusier.vm" )
  • 変数: #parse( $foo )

Recursion permitted. See parse_directive.maxdepth in velocity.properties to change from parse depth. (The default parse depth is 10.)

再帰解析も可能です。 解析レベルを変更するためには、velocity.properties内の parse_directive.maxdepth を確認してください。 (デフォルトの解析レベルは 10 です。)


#stop - Stops the template engine − #stop - テンプレートエンジンを停止する

Format:

#stop

書式:

#stop

Usage:

This will stop execution of the current template. This is good for debugging a template.

使い方:

現在のテンプレートで実行を止めます。 テンプレートをデバッグするのに使えます。


#macro - Allows users to define a Velocimacro (VM), a repeated segment of a VTL template, as required − #macro - Velocimacro (VM)を定義する。 必要に応じて VTL テンプレートのセグメントを容易に繰り返せる。

Format:

#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] #end

  • vmname - Name used to call the VM (#vmname)
  • $arg1 $arg2 [ ... ] - Arguments to the VM. There can be any number of arguments, but the number used at invocation must match the number specified in the definition.
  • [ VM VTL code... ] - Any valid VTL code, anything you can put into a template, can be put into a VM.

書式:

#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] #end

  • vmname - VM を呼び出すのに使う名前 (#vmname)
  • $arg1 $arg2 [ ... ] - VM に対する引数です。引数の数はいくつでもかまいませんが、呼び出し側の引数の数と定義側の引数の数が一致しなければなりません。
  • [ VM VTL code... ] - テンプレートに置くことができる有効な VTL コードなら何でも、VM に入れることができます。

Once defined, the VM is used like any other VTL directive in a template.

定義した VM は、テンプレート内で他の VTL 指示子と同様に扱われます。

#vmname( $arg1 $arg2 )

VMs can be defined in one of two places:

  1. Template library: can be either VMs pre-packaged with Velocity or custom-made, user-defined, site-specific VMs; available from any template
  2. Inline: found in regular templates, only usable when velocimacro.permissions.allowInline=true in velocity.properties.

VM は、2つの場所のいずれかで定義できます。

  1. テンプレート ライブラリ: Velocity に同梱のVMである場合と、 カスタムメイドで、ユーザが定義し、サイト固有の VM である場合があります。 どのテンプレートからでも使えます。
  2. インライン: 通常のテンプレート内で定義されたもので、 velocity.properties内でvelocimacro.permissions.allowInline=trueとなっている場合のみ使用可能です。


Comments − コメント

Comments are not rendered at runtime.

コメントは実行時には解析されません。

Single Line − 単一行コメント

Example:

## This is a comment.

例:

## コメントです。


Multi Line − 複数行コメント

Example:

#*
This is a multiline comment.
This is the second line
*#

例:

#*
複数行コメントです。
2行目です。
*#




このドキュメントは、 山野内 義一 、 新穂 洋史 、 高橋 達男 、 小山 博史 が訳しました。
コメントがある場合は、 report@jajakarta.org までお願いします。
オリジナル英文 Copyright © 1999-2005, The Apache Software Foundation