• 追加された行はこの色です。
  • 削除された行はこの色です。
* ビュー・テンプレートの作成 [#u9315b98]
 
** ページのレイアウト [#vffd1fbc]
sampleモジュールのindexアクションのページを考えると、デフォルトの構成では、以下のように2つのファイルを使い、レイアウトに<html>タグや<head>タグを記述し、テンプレートには<body>内を記述する。
:レイアウトファイル|apps/frontend/templates/layout.php
:テンプレートファイル|apps/frontend/modules/sample/templates/indexSuccess.php

*** 任意のレイアウトを指定する [#je43e40e]
- apps/frontend/templates/my_layout.php
- /apps/frontend/modules/sample/config/view.yml:
 all:
   has_layout: on
 indexSuccess:
   layout:     my_layout

** テンプレートからテンプレートを呼び出す [#hfdd63e8]
*** 部分テンプレート [#e8e09f07]
以下のようにテンプレートに記述する。
 <?php include_partial('mypartial1') ?>        # frontend/modules/mymodule/templates/_mypartial1.phpが呼び出される
 <?php include_partial('foobar/mypartial2') ?> # frontend/modules/foobar/templates/_mypartial2.phpが呼び出される
部分テンプレート内で変数を参照するには、呼び出す際に以下のようにすると、mytotal変数に参照出来る。
 <?php include_partial('mypartial', array('mytotal' => $total)) ?>
*** コンポーネント [#y27476cb]
TODO

** URL・ルーティング [#z36da454]
 // 内部のURI構文
 <module>/<action>[?param1=value1][&param2=value2][&param3=value3]...
 // 内部URIの例で、エンドユーザーに決して表示されない
 article/permalink?year=2006&subject=finance&title=activity-breakdown
 // 外部URLの例で、エンドユーザーに表示される
 http://www.example.com/articles/finance/2006/activity-breakdown.html

- routing.yml
 article_by_title:
   url:    articles/:subject/:year/:title.html
   param:  { module: article, action: permalink }

- url_for()ヘルパーは内部URIを外部URLに変換する
 <a href="<?php echo url_for('article/permalink?subject=finance&year=2006&title=activity-breakdown') ?>">
   ここをクリック</a>
 // ヘルパーはURIがarticle_by_titleルールにマッチすることを見る
 // ルーティングシステムはそれから外部URLを作成する
 => <a href="http://www.example.com/articles/finance/2006/activity-breakdown.html">ここをクリック</a>
- link_to()ヘルパーは直接ハイパーリンクを出力し、PHPとHTMLを混在させることを回避する
 <?php echo link_to(
   'ここをクリック',
   'article/permalink?subject=finance&year=2006&title=activity-breakdown') ?>
 ) ?>
- 内部では、link_to()はurl_for()を呼び出すので結果はつぎのものと同じ
 => <a href="http://www.example.com/articles/finance/2006/activity-breakdown.html">ここをクリック</a>

*** FORMタグ例 [#gad40b34]
 <html><body>
 <form action="<?php echo url_for('author/edit'.(!$author->isNew() ? '?id='.$author->getId() : '')) ?>" 
   method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
 <input type="submit" value="Save" />
 </form>
 </body></html>

*** Aタグ例 [#o1a6695c]
 <html><body>
 <?php echo link_to('Delete', 'author/delete?id='.$author->getId(), 
   array('post' => true, 'confirm' => 'Are you sure?')) ?>
 </body></html>

** フォームヘルパー [#pfd3d088]
*** フォームタグ [#e3141512]
 <?php echo form_tag('test/save') ?>
   => <form method="post" action="/path/to/save">
 <?php echo form_tag('test/save', 'method=get multipart=true class=simpleForm') ?>
   => <form method="get" enctype="multipart/form-data" class="simpleForm"action="/path/to/save">
閉じタグは</form>自分で。

** テンプレート内でリクエストオブジェクトなどコアオブジェクトにアクセスする [#ibc8a654]
 <html>
 <?php if (sfContext::getInstance()->getRequest()->getAttribute('user_name') == 'taro') { ?>
   こんにちは太郎さん
 <?php } ?> 
 </html>
sfContextオブジェクトはリクエスト、レスポンス、ユーザーなどのsymfonyのコアオブジェクトへの参照を持つ。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS