基本構文

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

基本構文

web2pyのテンプレート言語は、すべてのPythonの制御構造をサポートしています。ここでは、それらのそれぞれの例を示します。それらは通常のプログラミングのプラクティスに準じてネストさせることができます。

The web2py template language supports all Python control structures. Here we provide some examples of each of them. They can be nested according to usual programming practice.

for...in

テンプレートでは、任意の反復可能オブジェクトに対してループを回すことができます:

In templates you can loop over any iterable object:

1.

2.

3.

4.

{{items = ['a', 'b', 'c']}}

<ul>

{{for item in items:}}<li>{{=item}}</li>{{pass}}

</ul>

これは次のようになります:

which produces:

1.

2.

3.

4.

5.

<ul>

<li>a</li>

<li>b</li>

<li>c</li>

</ul>

ここで、itemは任意のiterableオブジェクトです。これは、Pythonのリスト、Pythonのタプル、Rowsオブジェクト、イテレータとして実装された任意のオブジェクトなどです。表示されるオブジェクトは初めにシリアライズされ、エスケープされます。

Here item is any iterable object such as a Python list, Python tuple, or Rows object, or any object that is implemented as an iterator. The elements displayed are first serialized and escaped.

while

whiteキーワードを用いてループを作成できます:

You can create a loop using the while keyword:

1.

2.

3.

4.

{{k = 3}}

<ul>

{{while k > 0:}}<li>{{=k}}{{k = k - 1}}</li>{{pass}}

</ul>

これは次のようになります:

which produces:

1.

2.

3.

4.

5.

<ul>

<li>3</li>

<li>2</li>

<li>1</li>

</ul>

if...elif...else

条件句を使用できます:

You can use conditional clauses:

1.

2.

3.

4.

5.

6.

7.

8.

{{

import random

k = random.randint(0, 100)

}}

<h2>

{{=k}}

{{if k % 2:}}is odd{{else:}}is even{{pass}}

</h2>

これは次のようになります:

which produces:

1.

2.

3.

<h2>

45 is odd

</h2>

elseが最初のifブロックを閉じることは明らかなため、ここではpass文は必要なく、その使用は不適切です。しかし、elseブロックはpass文とともに明示的に閉じなければなりません。

Since it is obvious that else closes the first if block, there is no need for a pass statement, and using one would be incorrect. However, you must explicitly close the else block with a pass.

Pythonでは"else if"をelifと書くことに留意して、次の例を見てください:

Recall that in Python "else if" is written elif as in the following example:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

{{

import random

k = random.randint(0, 100)

}}

<h2>

{{=k}}

{{if k % 4 == 0:}}is divisible by 4

{{elif k % 2 == 0:}}is even

{{else:}}is odd

{{pass}}

</h2>

これは次のようになります:

It produces:

1.

2.

3.

<h2>

64 is divisible by 4

</h2>

try...except...else...finally

try...except文をビューの中で使用することもできます。ただし、ある注意があります。次の例を考えてください。

It is also possible to use try...except statements in views with one caveat. Consider the following example:

1.

2.

3.

4.

5.

6.

7.

8.

9.

{{try:}}

Hello {{= 1 / 0}}

{{except:}}

division by zero

{{else:}}

no division by zero

{{finally}}

<br />

{{pass}}

これは次のような出力を生成します:

It will produce the following output:

1.

2.

3.

Hello

division by zero

<br />

この例は、例外が発生する前のすべての出力が、tryブロック内でレンダリングされること示しています(例外の前の出力もレンダリングされます)。"Hello"は例外の前にあるので書かれます。

This example illustrates that all output generated before an exception occurs is rendered (including output that preceded the exception) inside the try block. "Hello" is written because it precedes the exception.

def...return

web2pyのテンプレート言語では、任意のPythonオブジェクトやtext/html文字列を返す関数を定義し実装することができます。ここでは、2つの例を考えます:

The web2py template language allows the developer to define and implement functions that can return any Python object or a text/html string. Here we consider two examples:

1.

2.

3.

4.

{{def itemize1(link): return LI(A(link, _href="http://" + link))}}

<ul>

{{=itemize1('www.google.com')}}

</ul>

これは次のような出力を生成します:

produces the following output:

1.

2.

3.

<ul>

<li><a href="http:/www.google.com">www.google.com</a></li>

</ul>

関数itemize1は関数が呼ばれた場所に挿入されるヘルパーオブジェクトを返します。

The function itemize1 returns a helper object that is inserted at the location where the function is called.

次のコードを検討してください:

Consider now the following code:

1.

2.

3.

4.

5.

6.

{{def itemize2(link):}}

<li><a href="http://{{=link}}">{{=link}}</a></li>

{{return}}

<ul>

{{itemize2('www.google.com')}}

</ul>

これは上記と全く同じ出力を生成します。この場合、関数itemize2は、関数が呼ばれた場所でweb2pyのタグが置換されることになるHTMLの一部を表現します。itemize2の呼び出しの手前に'='がないことに注意してください。これは、関数が文字列を返すのではなく、直接レスポンスに書き込んでいるからです。

It produces exactly the same output as above. In this case, the function itemize2 represents a piece of HTML that is going to replace the web2py tag where the function is called. Notice that there is no '=' in front of the call to itemize2, since the function does not return the text, but it writes it directly into the response.

1つの注意点は、ビュー内で定義された関数はreturn文で終了しなければならないことです。そうしないと自動インデントが失敗します。

There is one caveat: functions defined inside a view must terminate with a return statement, or the automatic indentation will fail.