¥È¥Ã¥×   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS

PHP/Smarty/¥µ¥ó¥×¥ë ¤Î¥Ð¥Ã¥¯¥¢¥Ã¥×º¹Ê¬(No.1)


  • Äɲ䵤줿¹Ô¤Ï¤³¤Î¿§¤Ç¤¹¡£
  • ºï½ü¤µ¤ì¤¿¹Ô¤Ï¤³¤Î¿§¤Ç¤¹¡£
[[[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>