05 ビュー

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(Omi Chiba)

ビュー

web2pyはモデル、コントローラ、ビューのためにPythonを使用しています。ただし、ビューにおいては若干修正したPython構文を用いています。これにより、適切なpythonの利用に制約をかけることなく、より可読性のあるコードが書けるようになっています。

web2py uses Python for its models, controllers, and views, although it uses a slightly modified Python syntax in the views to allow more readable code without imposing any restrictions on proper Python usage.

ビューの目的は、HTMLドキュメント内にPythonコードを埋め込むことです。一般的に、これはいくつかの問題を引き起こします:

The purpose of a view is to embed code (Python) in an HTML document. In general, this poses some problems:

  • 埋め込まれたコードは、エスケープすべきか?

  • How should embedded code be escaped?

  • インデントはPythonかHTMLのどちらのルールに基づいてされるべきか?

  • Should indenting be based on Python or HTML rules?

web2py は{{...}}をHTMLに埋め込んだPythonコードをエスケープするために使用しています。中括弧を山括弧の代わりに用いる利点は、すべての一般 的なHTMLエディタにおいて分かりやすいからです。これによりそれらのエディタでweb2pyのビューを作成することが可能になります。

web2py uses {{ ... }} to escape Python code embedded in HTML. The advantage of using curly brackets instead of angle brackets is that it's transparent to all common HTML editors. This allows the developer to use those editors to create web2py views.

開発者はHTMLにPythonコードを埋め込むので、ドキュメントはPythonのルールでなく、HTMLのルールに従ってインデントされる必要があります。そのため、web2pyでは、{{ ... }}のタグの中でインデント不要のPythonを書けるようにしています。Pythonは通常、コードのブロックを区切るためインデントを使用していますが、ここでは別の方法が要求されます。このような理由で、web2pyのテンプレート言語はPythonキーワードのpassを活用しています。

Since the developer is embedding Python code into HTML, the document should be indented according to HTML rules, and not Python rules. Therefore, we allow unindented Python inside the {{ ... }} tags. Since Python normally uses indentation to delimit blocks of code, we need a different way to delimit them; this is why the web2py template language makes use of the Python keyword pass.

コードブロックは、コロンで終わる行から始まり、passで始まる行で終わります。キーワードpassはブロックの終わりがコンテキストから明白な場合は必要ではありません。

A code block starts with a line ending with a colon and ends with a line beginning with pass. The keyword pass is not necessary when the end of the block is obvious from the context.

これはその例です:

Here is an example:

1.

2.

3.

4.

5.

6.

7.

{{

if i == 0:

response.write('i is 0')

else:

response.write('i is not 0')

pass

}}

passはPythonのキーワードで、web2pyのキーワードではないことに注意してください。EmacsなどのいくつかのPythonエディタは、passキーワードを用いてブロックの区切りを示し、自動的にコードを再インデントします。

Note that pass is a Python keyword, not a web2py keyword. Some Python editors, such as Emacs, use the keyword pass to signify the division of blocks and use it to re-indent code automatically.

web2pyのテンプレート言語は、Pythonと全く同じことをします。次のような記述は:

The web2py template language does exactly the same. When it finds something like:

1.

2.

3.

<html><body>

{{for x in range(10):}}{{=x}}hello<br />{{pass}}

</body></html>

テンプレート言語により、次のようなプログラムに変換されます:

it translates it into a program:

1.

2.

3.

4.

5.

response.write("""<html><body>""", escape=False)

for x in range(10):

response.write(x)

response.write("""hello<br />""", escape=False)

response.write("""</body></html>""", escape=False)

response.writeは、response.bodyに書き込みます。

response.write writes to the response.body.

web2pyのビューにエラーがある場合、エラーレポートには、開発者が書いた実際のビューではなく、生成されたコードが表示されます。これにより、実行された実際のコードをハイライトしてコードをデバッグすることが容易になります(そのコードはHTMLエディタやブラウザのDOMインスペクタを使用してデバッグすることができるものです)。

When there is an error in a web2py view, the error report shows the generated view code, not the actual view as written by the developer. This helps the developer debug the code by highlighting the actual code that is executed (which is something that can be debugged with an HTML editor or the DOM inspector of the browser).

また注意する点として:

Also note that:

1.

{{=x}}

これは以下ものを生成します:

generates

1.

response.write(x)

このようにHTMLに注入された変数は、デフォルトでエスケープされます。もしxがXMLオブジェクトなら、escapeをTrueにしても、エスケープは無視されます。

Variables injected into the HTML in this way are escaped by default. The escaping is ignored ifx is an XML object, even if escape is set to True.

ここで示すのは、H1ヘルパーの利用例です:

Here is an example that introduces the H1 helper:

1.

{{=H1(i)}}

これは、以下のものに変換されます:

which is translated to:

1.

response.write(H1(i))

上記の評価では、H1オブジェクトとその要素は再帰的にシリアライズされ、エスケープされ、response.bodyに書かれます。H1と内部HTMLによって生成されたタグはエスケープされません。この仕組みによって、Webページ上に表示されるすべてのテキスト--そしてテキストのみ--が、常にエスケープされ、それによりXSS脆弱性を防ぐことを保証します。同時に、コードはシンプルでデバッグし易いものとなります。

upon evaluation, the H1 object and its components are recursively serialized, escaped and written to the response body. The tags generated by H1 and inner HTML are not escaped. This mechanism guarantees that all text --- and only text --- displayed on the web page is always escaped, thus preventing XSS vulnerabilities. At the same time, the code is simple and easy to debug.

response.write(obj, escape=True)のメソッドは、書かれるオブジェクトとエスケープされるかどうか(デフォルトはTrue)という2つの引数を取ります。objが.xml()メソッドを持つ場合、それが呼ばれ、その結果がresponseのbodyに書かれます(エスケープ引数は無視されます)。それ以外の場合は、オブジェクトの__str__メソッドがシリアライズするのに用いられ、エスケープ引数がTrueならばエスケープされます。すべての組み込みのヘルパーオブジェクト(H1など)は、.xml()メソッドを介して自分自身をシリアライズする方法を知っています。

The method response.write(obj, escape=True) takes two arguments, the object to be written and whether it has to be escaped (set to True by default). If obj has an .xml() method, it is called and the result written to the response body (the escape argument is ignored). Otherwise it uses the object's __str__ method to serialize it and, if the escape argument isTrue, escapes it. All built-in helper objects (H1 in the example) are objects that know how to serialize themselves via the .xml() method.

これは、すべて透過的に行われます。response.writeメソッドを明示的に呼び出す必要は決してありません(呼び出すべきではないです)。

This is all done transparently. You never need to (and never should) call the response.writemethod explicitly.