ȥå   Խ ʬ Хåå ź ʣ ̾ѹ   ñ측 ǽ   إ   ǽRSS

PHP/Smarty

Last-modified: 2014-04-01 () 21:37:08 (1330d)
Top / PHP / Smarty

[PHP]


SmartyϢ

ȤϼΤ褦˵Ҥޤ
foo.tpl

{* SmartyΥȤǤ *}

ҤˤϤǤޤ

ƥץ졼Ȥɽ

եˤѿ
foo.conf

fax = '111-222-3333'

foo.tpl

{config_load file='foo.conf'}
{#fax#}

PHPɽ

foo.php

define('FAX','111-222-3333');

foo.tpl

{$smarty.const.FAX}

ͽѿ{$smarty}Ѥޤ

ѿ

ƥץ졼Ȥѿɽ

foo.tpl

{assign var=fax value='111-222-3333'}
{$fax}

PHPѿɽ

foo.php

$smarty = new Smarty();
$smarty->assign('fax', '111-222-3333');

foo.tpl

{$fax}

Ϣѿ

PHPϢѿɽ

foo.php

$smarty = new Smarty();
$smarty->assign('foo',
	array('fax' => '111-222-3333',
		'email' => 'zaphod@slartibartfast.example.com',
		'phone' => array('home' => '111-222-3334',
						'cell' => '111-222-3335')
					)
	);

foo.tpl

{$foo.fax}

Ϣѿ$foo['fax']ɽ ϢϥԥꥪɤѤޤ ̾{$foo[4]}ȤˤΤޤޥƥץ졼Ȥ˵Ҥޤ

ͽѿ{$smarty}

ڡɤ߹߻Ѳǽͽѿ{$smarty}

PHPǤɽ
SmartyΥС{$smarty.version}ǤSmartyΥС-
ߤΥƥץ졼ȥե̾{$smarty.template}Ǥƥץ졼ȥե̾-
<a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>HTMLե̾-
SmartyǤΥǥߥ {$smarty.ldelim} {$smarty.rdelim} ǤSmartyΥǥߥ-
{$smarty.get.foo}getѥ᡼foo͡$_GET['foo']
{$smarty.post.foo}postѥ᡼foo͡$_POST['foo']
{$smarty.cookies.foo}åѿfoo͡$_COOKIE['foo']
{$smarty.server.HTTP_USER_AGENT}ѿHTTP_USER_AGENT͡$_SERVER['HTTP_USER_AGENT']
{$smarty.env.PATH}ĶѿPATH͡$_ENV['PATH']
{$smarty.session.foo}åѿfoo͡$_SESSION['foo']
{$smarty.request.foo}get, post, cookies, server, envɤ줫foo-
{$smarty.const.MY_CONST}PHPǼΤ褦define('MY_CONST', 'abc');MY_CONST
1970ǯ11鸽ߤޤǤηвÿ{$smarty.now}äǤ1970ǯ11ηвÿ-
ߤ{$smarty.now|date_format:'%Y/%m/%d %H:%M:%S'}Ǥɽ-

{foreach}롼פѲǽͽѿ{$smarty}

{$smarty.foreach.loopname.iteration}롼ײ1, 2, 3, ...
{$smarty.foreach.loopname.index}ǥå0, 1, 2, ...
{if $smarty.foreach.loopname.first}ǽιܤɤtrue, false
{elseif $smarty.foreach.loopname.last}Ǹιܤɤtrue, false
{$smarty.foreach.loopname.total}롼פΥȡ롼פȴƤѲǽ

{capture}ѲǽȤʤͽѿ{$smarty}

{capture}ǰϤޤ줿Ƥname°ǻꤷѿ˼ޤ
{capture}

{* ƥĤɽʤ¤ꡢơ֥ιԤɽޤ *}
{capture name=banner}
	{include file='get_banner.tpl'}
{/capture}

{if $smarty.capture.banner != ''}
	<div id="banner">{$smarty.capture.banner}</div>
{/if}

ƥץ졼Ƿ׻ˡ

ǥҥåȤܿӡҥåȤʤܿ򥫥Ȥ
foreachǥ󥿤

 {assign var = "hit_count" value = 0}

foreachǥޥåԤ򥫥

 {math equation = "x + 1" x = $hit_count assign = hit_count}

foreach˥롼ײޥåԤơĤԿ

 {math equation = "x - y" x = $smarty.foreach.list_loop.total y = $hit_count assign = unhit_count}

ɽ

 ҥåȤΤ{$hit_count}Ǥ<br />
 ҥåȤʤäΤ{$unhit_count}Ǥ

ƥץ졼ʸˡ

{assign var = "now_date" value = "`$v.year`/`$v.month`/`$v.date`"}