Development

Documentation/ja_JP/book/1.0/07-Inside-the-View-Layer (diff)

You must first sign up to be able to contribute.

Changes between Version 3 and Version 4 of Documentation/ja_JP/book/1.0/07-Inside-the-View-Layer

Show
Ignore:
Author:
takuya.morioka (IP: 58.188.174.146)
Timestamp:
03/02/07 10:28:33 (11 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/ja_JP/book/1.0/07-Inside-the-View-Layer

    v3 v4  
    1 Chapter 7 - Inside The View Layer 
    2 ================================= 
     1= Chapter 7 - Inside The View Layer = 
    32 
    43The view is responsible for rendering the output correlated to a particular action. In symfony, the view consists of several parts, with each part designed to be easily modified by the person who usually works with it.viewはアクションに紐付いた出力作成を任されています。symfonyではviewが幾つかのパーツに分かれており、それぞれは、人の手でメンテナンスをしやすいようになっています。 
    1817 
    1918Listing 7-1 - A Sample indexSuccess.php Template 
    20  
    21     [php] 
     19{{{ 
     20#!php 
    2221    <h1>Welcome</h1> 
    2322    <p>Welcome back, <?php echo $name ?>!</p> 
    2625      <li><?php echo link_to('Start writing a new one', 'article/write') ?></li> 
    2726    </ul> 
    28  
     27}}} 
    2928As explained in Chapter 4, the alternative PHP syntax is preferable for templates to make them readable for non-PHP developers. You should keep PHP code to a minimum in templates, since these files are the ones used to design the GUI of the application, and are sometimes created and maintained by another team, specialized in presentation but not in application logic. Keeping the logic inside the action also makes it easier to have several templates for a single action, without any code duplication.第4章を思い出してください。決められたPHPの文法を使うことで、php初心者でも読みやすいテンプレートに仕立てることができます。テンプレート中のPHPコードをできる限り少なくしましょう。そうすることで、デザイナはお気に入りのHTMLエディタで安心してデザインに集中できます。また別チームにテンプレートを丸投げしてしまって、安心してAPPの開発に専念できるでしょう。ロジックをAction内に完結させていれば、コードの重複無くtemplateとActionの一対一対応を維持したままに、簡単に管理できることでしょう。 
    3029 
    31 ### Helpers 
     30== Helpers 
    3231 
    3332Helpers are PHP functions that return HTML code and can be used in templates. In Listing 7-1, the `link_to()` function is a helper. Sometimes, helpers are just time-savers, packaging code snippets frequently used in templates. For instance, you can easily imagine the function definition for this helper:ヘルパー関数はオブジェクトでなく、関数のみで定義されていて、HTMLコードを返す関数として定義されています。次の7-1の例はlink_to()関数の利用例を示しています。ヘルパー関数で時間の節約を図れます。ヘルパー関数はこのようにSnippetsとしてテンプレート中のあちこちで利用されます。そして、関数名を見れば出力HTMLが何か直ぐに分かるように単純化されています。 
    3433 
    35     [php] 
     34{{{ 
     35#!php 
    3636    <?php echo input_tag('nickname') ?> 
    3737     => <input type="text" name="nickname" id="nickname" value="" /> 
    38  
     38}}} 
    3939It should look like Listing 7-2. 7-2を見てください 
    4040 
    4141Listing 7-2 - Sample Helper Definition ヘルパー関数のソースコード(だいたいこんな感じです) 
    42  
    43     [php] 
     42{{{ 
     43#!php 
    4444    function input_tag($name, $value = null) 
    4545    { 
    4646      return '<input type="text" name="'.$name.'" id="'.$name.'"value="'.$value.'" />'; 
    4747    } 
    48  
     48}}} 
    4949As a matter of fact, the input_tag() function built into symfony is a little more complicated than that, as it accepts a third parameter to add other attributes to the <input> tag. You can check its complete syntax and options in the online API documentation (http://www.symfony-project.com/api/symfony.html).この例よりもSymfonyではHelper関数input_tag()、本当はもう少し複雑な内部を処理を行っています。本当は、三番目の引数が存在し、Inputタグの属性値(訳注:typeとかidやsize等)を設定することが来ます。細かい仕様はAPIドキュメントを参照してください。(http://www.symfony-project.com/api/symfony.html) 
    5050 
    5151Most of the time, helpers carry intelligence and save you long and complex coding:殆どの場合Helper関数はコード作成の省力化に役立つように設計されています。 
    5252 
    53     [php] 
     53{{{ 
     54#!php 
    5455    <?php echo auto_link_text('Please visit our website www.example.com') ?> 
    5556     => Please visit our website <a href="http://www.example.com">www.example.com</a> 
    56  
     57}}} 
    5758Helpers facilitate the process of writing templates and produce the best possible HTML code in terms of performance and accessibility. You can always use plain HTML, but helpers are usually faster to write.Helper関数を利用することでテンプレート作成の機械化が図れ、HTMLのパフォーマンスとアクセシビリティーの向上に役立ちます。生HTMLソースと向き合うだけでなく、Helper関数のおかけで手早く書くこともできるでしょう。 
    5859 
    6061>You may wonder why the helpers are named according to the underscore syntax rather than the camelCase convention, used everywhere else in symfony. This is because helpers are functions, and all the core PHP functions use the underscore syntax convention.Helper関数名がCamelizeではなく、アンダースコアを用いていることに疑問を感じたことでしょう。これはHelper関数がオブジェクトではなく単なるPHPの関数であること、PHP関数がアンダースコアで表記されていることに由来します(訳注:str_replace等の命名規則を真似ている) 
    6162 
    62 #### Declaring Helpers Helper関数を作る 
    63  
    64 The symfony files containing helper definitions are not autoloaded (since they contain functions, not classes). Helpers are grouped by purpose. For instance, all the helper functions dealing with text are defined in a file called `TextHelper.php`, called the `Text` helper group. So if you need to use a helper in a template, you must load the related helper group earlier in the template by declaring it with the use_helper() function. Listing 7-3 shows a template using the auto_ link_text() helper, which is part of the `Text` helper group. 
     63== Declaring Helpers Helper  Helper関数を読み込むHelper関数 
     64 
     65The symfony files containing helper definitions are not autoloaded (since they contain functions, not classes). Helpers are grouped by purpose. For instance, all the helper functions dealing with text are defined in a file called `TextHelper.php`, called the `Text` helper group. So if you need to use a helper in a template, you must load the related helper group earlier in the template by declaring it with the use_helper() function. Listing 7-3 shows a template using the auto_link_text() helper, which is part of the `Text` helper group. Helper関数のソースファイルは自動駅にロードされるわけではありません(Helper関数のソースはClassを含まず関数だけが記述されているため)。Helper関数は目的別にまとめられ記述してあります。たとえば、テキスト関連のHelper関数を集めたファイルは`TextHelper.php`になっています。またこれを、`テキストヘルパーグループ`と呼んでいます。ココまで分かるように、テンプレート中でHelper関数を使うときは、必要としている、ヘルパー関数定義をソースの最初の方で読み込んで関数を使えるようします。このために用意された専用Helper関数`use_helper()`を使います。7-3の例はauto_link_text()関数をテンプレート中で使うための処理を記述した例です。auto_link_text()はTextグループ中に定義されています。 
    6566 
    6667Listing 7-3 - Declaring the Use of a Helper 
    67  
    68     [php] 
     68{{{ 
     69#!php 
    6970    // Use a specific helper group in this template 
    7071    <?php echo use_helper('Text') ?> 
    7273    <h1>Description</h1> 
    7374    <p><?php echo auto_link_text($description) ?></p> 
    74  
     75}}} 
    7576>**TIP** 
    76 >If you need to declare more than one helper group, add more arguments to the `use_helper()` call. For instance, to load both the `Text` and the `Javascript` helper groups in a template, call `<?php echo ` use_helper('Text', 'Javascript') ?>`. 
    77  
    78 A few helpers are available by default in every template, without need for declaration. These are helpers of the following helper groups: 
    79  
    80   * `Helper`: Required for helper inclusion (the `use_helper()` function is, in fact, a helper itself) 
    81   * `Tag`: Basic tag helper, used by almost every helper 
    82   * `Url`: Links and URL management helpers 
    83   * `Asset`: Helpers populating the HTML `<head>` section, and providing easy links to external assets (images, JavaScript, and style sheet files) 
    84   * `Partial`: Helpers allowing for inclusion of template fragments 
    85   * `Cache`: Manipulation of cached code fragments 
    86   * `Form`: Form input helpers 
    87  
    88 The list of the standard helpers, loaded by default for every template, is configurable in the `settings.yml` file. So if you know that you will not use the helpers of the `Cache` group, or that you will always use the ones of the Text group, modify the standard_helpers setting accordingly. This will speed up your application a bit. You cannot remove the first four helper groups in the preceding list (`Helper`, `Tag`, `Url`, and `Asset`), because they are compulsory for the templating engine to work properly. Consequently, they don't even appear in the list of standard helpers. 
     77>If you need to declare more than one helper group, add more arguments to the `use_helper()` call. For instance, to load both the `Text` and the `Javascript` helper groups in a template, call `<?php echo ` use_helper('Text', 'Javascript') ?>`.もし、一つ以上複数のHelper関数を利用するときは、use_helper()の引数に追加していけばOK。たとえば、`Text`と`JavaScript`を使うときは、`<?php echo ` use_helper('Text', 'Javascript') ?>`と書きます 
     78 
     79A few helpers are available by default in every template, without need for declaration. These are helpers of the following helper groups:幾つかのHelper関数はテンプレート中のどこでも使うことができるデフォルトとして準備されています。利用可能なHelper関数グループは以下の通りです 
     80 
     81  * `Helper`: Required for helper inclusion (the `use_helper()` function is, in fact, a helper itself) `use_helper()`その物。Helper関数をInlucdeするHelper関数群 
     82  * `Tag`: Basic tag helper, used by almost every helper HTMLタグを扱うHelper関数群。デフォルトで読み込み済み 
     83  * `Url`: Links and URL management helpers LinkとURLを扱うHelper関数群。 
     84  * `Asset`: Helpers populating the HTML `<head>` section, and providing easy links to external assets (images, JavaScript, and style sheet files) HTMLの`<head>`部分を作るHelper関数群、CSS,JavaScript,Imageなどのリンクを作成する関数が含まれている。 
     85  * `Partial`: Helpers allowing for inclusion of template fragments. 細かく分割したSymfonyテンプレートをIncludeする関数 
     86  * `Cache`: Manipulation of cached code fragments キャッシュされたコードの制御をする 
     87  * `Form`: Form input helpers フォーム関連のInputタグを扱う。 
     88 
     89The list of the standard helpers, loaded by default for every template, is configurable in the `settings.yml` file. So if you know that you will not use the helpers of the `Cache` group, or that you will always use the ones of the Text group, modify the standard_helpers setting accordingly. This will speed up your application a bit. You cannot remove the first four helper groups in the preceding list (`Helper`, `Tag`, `Url`, and `Asset`), because they are compulsory for the templating engine to work properly. Consequently, they don't even appear in the list of standard helpers.テンプレート中で最初から読み込み済みのHelper関数群ですが、これは`settings.yml`で定義することができます。`Cache`Helper関数を使わないと分かっていて、`Text`はいつも使うのならば、standard_helpersをそれに従って変更すればよいです。余計な読み込みをを省略することでほんのちょっと動作を速くすることができます。一覧の前半4つのHelper関数は絶対に読み込む必要があります(`Helper`, `Tag`, `Url`, and `Asset`)。これら4つがないとSymfonyテンプレート自体が動作できなくなるおそれがあります。 
    8990 
    9091>**TIP** 
    91 >If you ever need to use a helper outside a template, you can still load a helper group from anywhere by calling `sfLoader::loadHelpers($helpers)`, where `$helpers` is a helper group name or an array of helper group names. For instance, if you want to use `auto_link_text()` in an action, you need to call `sfLoader::loadHelpers('Text')` first. 
    92  
    93 #### Frequently Used Helpers 
    94  
    95 You will learn about some helpers in detail in later chapters, in relation with the feature they are helping. Listing 7-4 gives a brief list of the default helpers that are used a lot, together with the HTML code they return. 
     92>If you ever need to use a helper outside a template, you can still load a helper group from anywhere by calling `sfLoader::loadHelpers($helpers)`, where `$helpers` is a helper group name or an array of helper group names. For instance, if you want to use `auto_link_text()` in an action, you need to call `sfLoader::loadHelpers('Text')` first.Helper関数はもちろんテンプレート外でも使えます。`sfLoader::loadHelpers($helpers)`と書くことで任意のHelper関数をロードし利用できます($helpersはHelperグループ名)。`auto_link_text()`をActrionで使いたいときは`sfLoader::loadHelpers('Text')`と記述すれば利用可能になります。 
     93 
     94== Frequently Used Helpers よく使うHelper関数 
     95 
     96You will learn about some helpers in detail in later chapters, in relation with the feature they are helping. Listing 7-4 gives a brief list of the default helpers that are used a lot, together with the HTML code they return.ここまででHelper関数の詳細とその機能について学んできました。7-4の例では、たくさんのHelper関数を使った例で、コメントは実際に作成されるHTMLの例です。 
    9697 
    9798Listing 7-4 - Common Default Helpers 
    98  
    99     [php] 
     99{{{ 
     100#!php 
    100101    // Helper group 
    101102    <?php echo use_helper('HelperName') ?> 
    104105    // Tag group 
    105106    <?php echo tag('input', array('name' => 'foo', 'type' => 'text')) ?> 
    106     <?php echo tag('input', 'name=foo type=text') ?>  // Alternative options syntax 
     107    <?php echo tag('input', 'name=foo type=text') ?>  // Alternative options syntax Inputの属性値を書き出す方法です。 
    107108     => <input name="foo" type="text" /> 
    108109    <?php echo content_tag('textarea', 'dummy content', 'name=foo') ?> 
    111112    // Url group 
    112113    <?php echo link_to('click me', 'mymodule/myaction') ?> 
    113     => <a href="/route/to/myaction">click me</a>  // Depends on the routing settings 
     114    => <a href="/route/to/myaction">click me</a>  // Depends on the routing settings ルーティング設定に併せてパスを考えます。 
    114115 
    115116    // Asset group 
    120121    <?php echo stylesheet_tag('style') ?> 
    121122     => <link href="/stylesheets/style.css" media="screen" rel="stylesheet"type="text/css" /> 
    122  
    123 There are many other helpers in symfony, and it would take a full book to describe all of them. The best reference for helpers is the online API documentation (`http:// www.symfony-project.com/api/symfony.html`), where all the helpers are well documented, with their syntax, options, and examples. 
    124  
    125 #### Adding Your Own Helpers 
    126  
    127 Symfony ships with a lot of helpers for various purposes, but if you don't find what you need in the API documentation, you will probably want to create a new helper. This is very easy to do. 
    128  
    129 Helper functions (regular PHP functions returning HTML code) should be saved in a file called `FooBarHelper.php`, where `FooBar` is the name of the helper group. Store the file in the `apps/myapp/lib/helper/` directory (or in any `helper/` directory created under one of the `lib/` folders of your project) so it can be found automatically by the `use_helper('FooBar')` helper for inclusion. 
     123}}} 
     124There are many other helpers in symfony, and it would take a full book to describe all of them. The best reference for helpers is the online API documentation (`http:// www.symfony-project.com/api/symfony.html`), where all the helpers are well documented, with their syntax, options, and examples.このほかにもたくさんのHelper関数がありますが紙面の関係でこれ以上は紹介できません。詳しくはAPIドキュメント(`http:// www.symfony-project.com/api/symfony.html`)をご参照ください。ドキュメントでは全ての関数とその引数と例を紹介しています。 
     125 
     126== Adding Your Own Helpers 自分で作るHelper関数 
     127 
     128Symfony ships with a lot of helpers for various purposes, but if you don't find what you need in the API documentation, you will probably want to create a new helper. This is very easy to do. Symfonyは色々な目的のHelper関数を持っています。しかしAPIドキュメントを見渡しても必要とするHelper関数が無いとき、自分でHelper関数を定義することができます。とっても簡単ですよ。 
     129 
     130Helper functions (regular PHP functions returning HTML code) should be saved in a file called `FooBarHelper.php`, where `FooBar` is the name of the helper group. Store the file in the `apps/myapp/lib/helper/` directory (or in any `helper/` directory created under one of the `lib/` folders of your project) so it can be found automatically by the `use_helper('FooBar')` helper for inclusion.Helper関数群は(一般的なPHP関数でHTMLコードを作るモノ)決められた命名規則でファイルを作ります.`FooBarHeher.php`の場合、FooBarがHelper関数のグループ名になります。このファイルを`apps/myapp/lib/helper/`に保存します(もし`helper/`ディレクトリが無ければ`lib/`直下に作ってください)、そして`use_heler($helpername)`をつかえば、利用可能です。 
    130131 
    131132>**TIP** 
    132 >This system even allows you to override the existing symfony helpers. For instance, to redefine all the helpers of the `Text` helper group, just create a `TextHelper.php` file in your `apps/myapp/lib/helper/` directory. Whenever you call `use_helper('Text')`, symfony will use your helper group rather than its own. But be careful: as the original file is not even loaded, you must redefine all the functions of a helper group to override it; otherwise, some of the original helpers will not be available at all. 
    133  
    134 ### Page Layout 
     133>This system even allows you to override the existing symfony helpers. For instance, to redefine all the helpers of the `Text` helper group, just create a `TextHelper.php` file in your `apps/myapp/lib/helper/` directory. Whenever you call `use_helper('Text')`, symfony will use your helper group rather than its own. But be careful: as the original file is not even loaded, you must redefine all the functions of a helper group to override it; otherwise, some of the original helpers will not be available at all.この仕組みを使って、Symfony組み込みのHelper関数を上書きすることができます。たとえば、組み込みの`Text`グループを抹消して自分のTextグループを作るときは`TextHelper.php`を上記のディレクトリに作ればよいでしょう。use_helper()関数はSymfony組み込みよりもユーザー定義を優先します。これは逆に言うと、SymfonyのHelperグループが全くロードされませんから、自分で全ての定義を行って上書きしてください。グループを上書きするとSymfony組み込みはつけなくなります。 
     134 
     135 
     136== Page Layout レイアウト 
    135137 
    136138The template shown in Listing 7-1 is not a valid XHTML document. The `DOCTYPE` definition and the `<html>` and `<body>` tags are missing. That's because they are stored somewhere else in the application, in a file called `layout.php`, which contains the page layout. This file, also called the global template, stores the HTML code that is common to all pages of the application to avoid repeating it in every template. The content of the template is integrated into the layout, or, if you change the point of view, the layout "decorates" the template. This is an application of the decorator design pattern, illustrated in Figure 7-1.