facebook twitter hatena line google mixi email

Php/Smarty/講座

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

実用的な使い方

----index.php---------------------------------------------------
<?php
require 'Myapprty.class.php';

$app['title'] = "実用的な使い方";

$app['arr'] = array(1, 2, 3);

$list = array();
$row['id'] = 1;
$row['name'] = "yahoo";
$row['url'] = "http://www.yahoo.co.jp";
array_push($list,$row);
$row['id'] = 2;
$row['name'] = "google";
$row['url'] = "http://www.google.com";
array_push($list,$row);
$app['listsite'] = $list;

unset($row);
$list = array();
$row['id'] = 1;
$row['name'] = "red";
$row['hex'] = "ff0000";
array_push($list,$row);
$row['id'] = 2;
$row['name'] = "blue";
$row['hex'] = "0000ff";
array_push($list,$row);
$row['id'] = 3;
$row['name'] = "white";
$row['hex'] = "ffffff";
array_push($list,$row);
$app['color'] = $list;

$apprty = new Myapprty;
$apprty->assign("app", $app);
$apprty->display('index.tpl');
?>
----------------------------------------------------------------


----templates-index.tpl------------------------------------------
{* タイトル *}
title={$app.title}<br><br>

{* 配列 *}
{section name=i loop=$app.arr}
  arr[{$apprty.section.i.iteration}]={$app.arr[i]}<br>
{/section}
<br>

{* データベースからのデータの表示 *}
listsite<br>
<table border=1>
<tr><td>ID</td><td>site</td></tr>
{foreach item=item from=$app.listsite}
  <tr>
  <td>{$item.id}</td>
  <td><a href="{$item.url}">{$item.name}</a></td>  
  </tr>
{/foreach}
</table>
<br>

{* データベースからのデータの表示2 *}
listcolor<br>
<table border=1>
<tr><td>ID</td><td>name</td><td>hex</td></tr>
{foreach key=key item=item from=$app.listcolor}
  <tr>
  <td>{$app.listcolor.$key.id}</td>
  <td>{$app.listcolor.$key.name}</td>
  <td>{$app.listcolor.$key.hex}</td>
  </tr>
{/foreach}
</table>
----------------------------------------------------------------