smartyで同じテンプレートを使いまわす

このQ&Aのポイント
  • smartyを使用して同じテンプレートを複数回使いまわす方法について教えてください。
  • index.phpからindex.tplを使いまわすためにリストの表示を効率化したいです。
  • boylist.tplとgirllist.tplを使いまわす方法を教えてください。
回答を見る
  • ベストアンサー

smartyで同じテンプレートを使いまわす

list.tpl {foreach from=$data key=key item=row loop=test} <tr><td>{$row.name}</td>:<td>{$row.kokugo}</td><td>{$row.sansu}</td></tr> {/foreach} ----- index.tpl(一部) <h2>男の子</h2> {include file=list.tpl} <h2>女の子</h2> {include file=list.tpl} ----- index.php(一部) $Smarty -> assign( "data", $boy ); $Smarty -> assign( "data", $girl ); $Smarty -> display( 'index.tpl' ); (実際にこの書き方は間違っていますが…) index.php から index.tpl を display したいのですが、 この中で使うリストを、使いまわしたいと考えています。 こういうやり方はそもそも可能なのでしょうか? たしかに boylist.tpl 、 girllist.tpl とし中の割当変数を $boy 、 $girl に変えればいいのですが、 将来変更が加わった際に両方変えなければなりません。 今回は例なので男女ですが、もっと多くの要素が出て来た時に 厄介なことになるように思います。 何か解決方法はありませんでしょうか?

  • PHP
  • 回答数1
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • ackie00h
  • ベストアンサー率100% (2/2)
回答No.1

fetch を使ってみてはいかがでしょうか? 多少書き方は変わりますが… index.tpl 内を <h2>男の子</h2> {$boyposition} <h2>女の子</h2> {$girlposition} とします。 index.php 内で $Boy = new Smarty(); $Girl = new Smarty(); として $Boy -> assign( "data", $boy ); $boyhtml = $Boy -> fetch( "test.tpl" ); $Girl -> assign( "data", $girl ); $girlhtml = $Girl -> fetch( "test.tpl" ); $Smarty -> assign( "boyposition", $boyhtml ); $Smarty -> assign( "girlposition", $girlhtml ); これでいけそうな気がしますがいかがでしょうか?

koyuki_0808
質問者

お礼

ありがとうございます !! この方法で大丈夫でした !!

関連するQ&A

  • Smarty includeでテンプレートを読み込めない

    よろしくお願いします。 まずphpファイルから $smarty->display('111/body.tpl');(*111はディレクトリ名) を呼び出して、さらにbody.tplと同ディレクトリにあるcontents.tplを読み込もうとしています。 はじめのbody.tplは読み込むことができブラウザで表示されるのですが、contents.tplは読み込めず、 Warning: Smarty error: unable to read resource: "contents.tpl" in <絶対パス>/Smarty/Smarty.class.php on line 1095 のエラーが出てしまいます。 ちなみに/111/というディレクトリは、$template_dir以下、$template_dir/111/です。 {include file="contents.tpl"} {include file="file:contents.tpl"} {include file="絶対パス/contents.tpl"} {include file="file:絶対パス/contents.tpl"} など試しましたが、ダメでした。 知恵をお貸しください。よろしくお願いします。

    • ベストアンサー
    • PHP
  • smartyのforeachの使い方?

    smartyについて教えてください。 mysqlのデータを繰り返し表示させたいのですが Warning: htmlspecialchars() expects parameter 1 to be string, array given in パス modifier.escape.php on line 26 とエラーが出てしまい動作しません。 foreachの使い方がおかしいのかと思いマニュアルをみてみたのですが わかりませんでした。 どうすれば繰り返し表示できるようになるでしょうか? ---php--- $i = 0; $list = array(); while ($col = mysql_fetch_array($res, MYSQL_ASSOC)) { $list[$i]['id'] = $col[id]; $list[$i]['title'] = $col[title]; $list[$i]['main'] = $col[main]; $i++; } $smarty->assign('list', $list); ---tpl--- {foreach item=list from=$list} <tr> <td>{$list.id}</td> <td>{$list.title}</td> <td>{$list.main}</td> <td><a href="hoge.php?id={$list.id}">a</a></td> <td><a href="hoges.php?id={$list.id}">a</a></td> </tr> {/foreach}

    • ベストアンサー
    • PHP
  • smartyについて

    smartyにでサイトを作る際のテンプレート構造について質問です。 現在はindex.phpで $smarty->display('hoge.tpl'); としhoge.tplは {include file="./common/header.tpl"} <div id="contents"> {include file="./common/left_side.tpl"} <div id="center"> ここに表示する内容 </div> {include file="./common/right_side.tpl"} </div> {include file="./common/footer.tpl"} のようにしています。 表示する内容はそれぞれ違うものの全てではありませんがincludeする tplは他のページもほぼ同様ですのでもっと効率的にできるのでは?と 思うのですがどんな方法があるでしょうか?

    • 締切済み
    • PHP
  • PHPとSmartyの関連について

    PHPとSmartyの関連について PHPからSmartyに出力しているのですが中身がNULLになります。 データベースアクセスにはPDOを使用しています。 PHPソース DBアクセス $SQL = "select ITEM_ID from ITEM_TBL limit 0, 3"; $STMT = $GLOBALS["PDO"] -> query($SQL); $ROW = $STMT -> fetchAll(PDO::FETCH_ASSOC); Smartyでアサイン $smarty -> assign("ITEM_LIST", $ROW); Smartyソース <table> {{foreach from=$ITEM_LIST item=LIST}} <tr> <td>{{$LIST.ITEM_ID}}</td> </tr> {{/foreach}} </table> のような感じでページ出力したいのですが、表示をすると中身がNULLでした。 PHP側で var_dump($ROW); を行うと array(3) { [0]=> array(1) { ["ITEM_ID"]=> string(1) "1" } [1]=> array(1) { ["ITEM_ID"]=> string(1) "2" } [2]=> array(1) { ["ITEM_ID"]=> string(1) "3" } } 中身が配列で格納されています。 smarty側で {$ITEM_LIST|@var_dump} を行うと array(3) { [0]=> NULL [1]=> NULL [2]=> NULL } でした。 いろいろとやってみたのですが、わからなくなってしまったので どなたかご教授いただけませんでしょうか。よろしくお願いします。

    • 締切済み
    • PHP
  • smartyについて

    smartyを勉強中です。 市販本のサンプルアプリは、うまく実行できました。 環境は、windows /php-5 / smarty-2.6.17 サンプルを真似して、少し修正して、実行すると、下記エラーが出ます。 Fatal error: Smarty error: [in fra_disp.tpl line 13]: syntax error: unrecognized tag: frag.rec_ctl (Smarty_Compiler.class.php, line 446) ソースをじっくり見てますが、サンプルと何が違うか分かりません。こんな質問で申し訳ありませんが、何かヒントいただけませんか。 ちなみに、呼び元pgでは、 $o_smarty->assign("frags",$data); $o_smarty->display("fra_disp.tpl"); また、テンプレートでは、 {foreach from=$frags item="frag"} <tr> <td>{frag.rec_ctl}</td> とコーディングしてます。

    • ベストアンサー
    • PHP
  • Smartyに関しまして

    require_once("Smarty/Smarty.class.php"); $smarty = new Smarty(); $smarty->template_dir = "./templates"; $smarty->compile_dir = "./templates_c"; $smarty->assign("message", "こんばんは"); $a = array('hello','php','html'); $smarty->assign('a',$a); $smarty->display("index.tpl"); これでindex.tplで出力すると日本語部分が「?????」になってしまいます。 何か設定がいるんでしょうか??

    • ベストアンサー
    • PHP
  • 【PHP】smartyとPHPの記述違いとメリット

    smartyというのが、デザインとプログラムを分けれるというので 使ってみたのですが、 //--------------------smarty--------------------- <table border=1> <tr> <th>{$table.title}</th> </tr> {foreach item=result from=$data_list} <tr> <td>{$result[0]}</td> </tr> {/foreach} </table> と、smarty用の記述をHTML内にいろいろ書かなければならず //------------------php---------------------- <table border=1> <tr> <th><?=$table[title] ?></th> </tr> <?php foreach($data_list as $key => $value){ ?> <tr> <td><?= $value ?></td> </tr> <?php } ?> </table> とHTML内にPHPをいろいろ書くのと  あまり大差無い気がして どういう点がよいのがイマイチ分かりません。 smartyは 使う メリットは どういうものがあるのでしょうか。

    • ベストアンサー
    • PHP
  • smartyについて

    smartyについて 環境は、windows /php-5 / smarty-2.6.17です。 以下の2つエラーが出てどう対処すればいいか困っています。 【1つめ】 Fatal error: Smarty error: [in **.tpl line 10]: syntax error: unrecognized tag: 文--> 【2つめ】 {$errmsg (Smarty_Compiler.class.php, line 446) in C:\xampp\php\PEAR\Smarty\libs\Smarty.class.php on line 1095 【ソース】 呼び先PGは、 $this->smarty->assign("errmsg","ニックネーム"); $this->smarty->display("**.tpl"); テンプレートでは、 <tr id="" class=""> <td id="" class=""><font color="#ff0000">{$errmsg}を入力してください。</font></td> </tr> よろしくお願いいたします。

    • 締切済み
    • PHP
  • PHP PEARのPagerをSmartyで使う

    初めまして、PEARのPagerを勉強していたのですが、Smartyで実装したいと思い試行錯誤していたらうまく表示されなくて困っています。 【PHP】 //PEARの読み込み、Smartyクラスの読み込み、MySQLクラスの読み込みは省略 //上記は正常に動作しています。 // テーブルの行数を数えます $sql = "SELECT COUNT(*) AS cnt FROM table_name;"; //レコード開始位置指定 $start = 0; //最大レコード数を指定 $limit = 10; // データを取り出すSQLを実行します。 $sql = "select * from table_name ORDER BY no DESC LIMIT $start, $limit"; $result = mysql_query($sql); while($rows = mysql_fetch_array($result)){ $res_data[]=array("title"=>$row['title'],"name"=>$row["name"],"no"=>$row["no"]); } //Pagerのパラメータを指定 $params = array( "itemData" => $res_data, "totalItems" => $limit, "perPage" => 6, "delta" =>5, "mode" =>"Jumping" ); //パラメータの連想配列を受け取る $pager =& Pager::factory($params); // ページデータの割り当て処理 foreach($pager->getPageData() as $item){ $res_data_page[] = $item; } //戻る/次/最初/最後のリンクとページリンクを返す $link = $pager -> getLinks(); //現在のページ番号を返す $currentPageID = $pager -> getCurrentPageID(); // テンプレート用に変数を設定 $smarty->assign('data', $res_data_page); $smarty->assign('pageNavi', $link['all']); $smarty->assign('currentPage', $currentPageID); ■tpl <table border="1"> <tr> <th>名前</th> <th>NO</th> <th>タイトル</th> </tr> {foreach from=$data item=i} <tr> <td>{$i.name}</td> <td>{$i.no}</td> <td>{$i.title}</td> </tr> {/foreach} </table> <p>{$pageNavi}</p> <p>{$currentPage}ページを表示</p> 参考URL http://meerweb.blog7.fc2.com/blog-entry-101.html データベースへの接続は出来ていて、ページャー機能は動いています。 取得しているデータ数もあっているのですが、全部1行目のデータだけ表示されます。 どなたか原因が分かる方いらっしゃいましたら、是非ともご教授ください。 よろしくお願い致します。

    • ベストアンサー
    • PHP
  • Smartyでインクリメント

    Smartyで以下のような処理がしたいのですがうまくいきません・・・(横にセルを5コ並べた後に改行したい) どのように記述したらよいか教えて頂けますでしょうか。 <table> <tr> {$i = 0} {foreach item=item_list from=$item_list} <td>{$item_list.hoge|escape}</td> {$i = $i + 1} {if $i mod 4 eq 0} </tr><tr> {/if} {/foreach} </table>

    • ベストアンサー
    • PHP

専門家に質問してみよう