¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

PHP/Smarty/¥µ¥ó¥×¥ë

Last-modified: 2018-11-14 (¿å) 15:23:56 (1983d)
Top / PHP / Smarty / ¥µ¥ó¥×¥ë

[PHP/Smarty]

Ä̾ïÇÛÎó¤Îɽ¼¨Îã

foo.php

<?php
// Smarty¥é¥¤¥Ö¥é¥êÆɤ߹þ¤ß
define('SMARTY_DIR', '/usr/local/lib/smarty/');
require_once(SMARTY_DIR . 'Smarty.class.php');

// Smarty¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òºîÀ®
$smarty = new Smarty();

// ³Æ¥Ç¥£¥ì¥¯¥È¥ê¤Î»ØÄê
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty->config_dir = './configs/';
$smarty->cache_dir = './cache/';

// ¥­¥ã¥Ã¥·¥åµ¡Ç½¤ÎÍ­¸ú²½
$smarty->caching = true;

// ¥Æ¥ó¥×¥ì¡¼¥È¤ÎÊÑ¿ô¤ËÃͤò³ä¤êÅö¤Æ¤ë
$smarty->assign('title', '̾Êí');

$address_book = array('»³ÅÄÂÀϺ', 'ÎëÌÚÆóϺ');
$smarty->assign('address', $address_book);

// ¥Æ¥ó¥×¥ì¡¼¥È¤ò»ØÄꤷɽ¼¨
$smarty->display('foo.tpl');
?>

foo.tpl

<html lang='ja'>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8'>
<title>{$title|escape}</title>
</head>
<body text='black' bgcolor='white'>
{* ¥Æ¥ó¥×¥ì¡¼¥ÈÃæ¤Î¥³¥á¥ó¥È *}

<!-- ¥¿¥¤¥È¥ë -->
<h1>{$title|escape}</h1>

<!-- ¥Æ¥ó¥×¥ì¡¼¥È¤Ø¤ÎËä¤á¹þ¤ß -->
<div align='center'>
<ul>
	{foreach from=$address_book item=value name=loop01}
		<li>{$value|escape}</li>
	{foreachelse}
		<li>ɽ¼¨¤µ¤»¤ë¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó¡£</li>
	{/foreach}
</ul>
</div>

</body>
</html>

Ï¢ÁÛÇÛÎó¤ÎÇÛÎóɽ¼¨Îã

foo.php

<?php
// Smarty¥é¥¤¥Ö¥é¥êÆɤ߹þ¤ß
define('SMARTY_DIR', '/usr/local/lib/smarty/');
require_once(SMARTY_DIR . 'Smarty.class.php');

// Smarty¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òºîÀ®
$smarty = new Smarty();

// ³Æ¥Ç¥£¥ì¥¯¥È¥ê¤Î»ØÄê
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty->config_dir = './configs/';
$smarty->cache_dir = './cache/';

// ¥­¥ã¥Ã¥·¥åµ¡Ç½¤ÎÍ­¸ú²½
$smarty->caching = true;

// ¥Æ¥ó¥×¥ì¡¼¥È¤ÎÊÑ¿ô¤ËÃͤò³ä¤êÅö¤Æ¤ë
$smarty->assign('title', '̾Êí');

$address_book = array(
	array('name' => '»³ÅÄÂÀϺ',
		'fax' => '03-1111-1112',
		'phone' => array('03-1111-1111', '080-2222-1111'));
	array('name' => 'ÎëÌÚÆóϺ',
		'fax' => '03-2222-1112',
		'phone' => array('03-2222-1111', '080-2222-1111'));
$smarty->assign('address', $address_book);

// ¥Æ¥ó¥×¥ì¡¼¥È¤ò»ØÄꤷɽ¼¨
$smarty->display('foo.tpl');
?>

foo.tpl

<html lang='ja'>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8'>
<title>{$title|escape}</title>
</head>
<body text='black' bgcolor='white'>
{* ¥Æ¥ó¥×¥ì¡¼¥ÈÃæ¤Î¥³¥á¥ó¥È *}

<!-- ¥¿¥¤¥È¥ë -->
<h1>{$title|escape}</h1>

<!-- ¥Æ¥ó¥×¥ì¡¼¥È¤Ø¤ÎËä¤á¹þ¤ß -->
<div align='center'>
<table border="1">
	<tr>
	{foreach from=$address_book item=row name=loop01}
		{foreach from=$row key=key item=value name=loop02}
			<th>$key|escape</th><td>{$value|escape}</td>
		{/foreach}
	{foreachelse}
		<th></th><td>ɽ¼¨¤µ¤»¤ë¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó¡£</td>
	{/foreach}
	</tr>
</table>
</div>

</body>
</html>

¥Ç¡¼¥¿¤¬Ìµ¤¤¤È¤­¤Ëtable¥¿¥°¤ò»È¤¤¤¿¤¯¤Ê¤¤¾ì¹ç¤Ï¡¢foreachelse¤ò»È¤ï¤º¼¡¤Î¤è¤¦¤Ëifʬ¤ÇȽÄꤷ¤Þ¤¹¡£

{if is_array($address_book) && count($address_book) > 0)
	<table border="1">
		<tr>
		{foreach from=$address_book item=row name=loop01}
			{foreach from=$row key=key item=value name=loop02}
				<th>$key|escape</th><td>{$value|escape}</td>
			{/foreach}
		{foreachelse}
			<th></th><td></td>
		{/foreach}
		</tr>
	</table>
{else}
	ɽ¼¨¤µ¤»¤ë¥Ç¡¼¥¿¤¬¤¢¤ê¤Þ¤»¤ó¡£
{/if}