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>site {foreach item=item from=$app.listsite} <tr> <td>{$item.id} <td><a href="{$item.url}">{$item.name}</a> {/foreach}

<br>

{* データベースからのデータの表示2 *}
listcolor<br>
<table border=1>

<tr><td>ID<td>name<td>hex {foreach key=key item=item from=$app.listcolor} <tr> <td>{$app.listcolor.$key.id} <td>{$app.listcolor.$key.name} <td>{$app.listcolor.$key.hex} {/foreach}

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