2014年2月26日水曜日

drupal bartik フィールド値の抜き出し

フィールド値の抜き出し

bartikのテンプレート、node.tpl.phpの中に、こう書いてある。
$content: An array of node items. Use render($content) to print them all,
 *   or print a subset such as render($content['field_example']).

以下、その説明。
http://customfield.jp/tutorial/demo1/content/117
からの切取り。
(ここで言う
node--forrent.tpl.php
は、node.tpl.phpと思っていい。書き換える前のものを指しているので。)

----------------------------------------------------------------------------

node--forrent.tpl.php の中の、以下の部分を探してください。
<?php
  // We hide the comments and links now so that we can render them later.
  hide($content['comments']);
  hide($content['links']);
  print render($content);
?>

実は print render($content); の部分で、コンテンツの各フィールドを含む内容の出力をすべて担っています。変数 $content の中には表示すべき各フィールドの内容が含まれ、レンダー関数は子要素を再帰的にレンダリングするためです。特定のフィールドのみをレンダリングしたい場合は、
<?php print render($content['フィールド名']); ?>
のようにします。たとえば「敷金(field_deposit)」フィールドを表示したい場合は、
<?php print render($content['field_deposit']); ?>
となります。これを、いわゆるブログでいうところのテンプレートタグのように使い、あとはデザイン担当者が好きなようにHTML を使ってマークアップし、 <?php /* ~ */ print render($content); ?> の部分と置き換えてください。
----------------------------------------------------------------------------

0 件のコメント:

コメントを投稿