| |
|
|
II. EFFICIENT (1)
5. Ç¥Çö½Ä
++ -- ?: , >>> ÇҴ翬»êÀÚ ¿¹¾à¾î¿¬»êÀÚ µîÀ» Á¦¿ÜÇÑ ¿¬»êÀÚ¸¦ »ç¿ëÇÏ¿© ÀÚ¹Ù½ºÅ©¸³Æ® ¹®¹ýÀ» µû¸£´Â Ç¥Çö½ÄÀ» ÀÚÀ¯·Ó°Ô ÀÛ¼ºÇÒ ¼ö ÀÖ½À´Ï´Ù.
{ forum.listnum % 2 }
{ (some.var + 3) * test }
7. À̽ºÄÉÀÌÇÁ
| ¸í·É¾î |
¼³¸í |
| \ |
ÅÛÇø´Äڵ带 ±×´ë·Î Ãâ·Â. |
ÅÛÇø´ÄÚµåÀÇ Çü½ÄÀÌ ¿Ã¹Ù¸£Áö ¾ÊÀ» °æ¿ì À̽ºÄÉÀÌÇÁ¸í·É¾î°¡ ¾ø¾îµµ ±×´ë·Î Ãâ·ÂÇÕ´Ï´Ù.
| ¿¹Á¦ |
| index.tpl |
>>output |
|
| {\@ abc} |
{@ abc} |
|
| { 2 + 2 } |
4 |
|
| {\ 2 + 2 } |
{ 2 + 2 } |
|
| {\\ 2 + 2 } |
{\ 2 + 2 } |
|
| { 2 + (2 } |
{ 2 + (2 } |
Çü½ÄÀÌ ¹Ù¸£Áö ¾ÊÀº °æ¿ì |
| {\ 2 + (2 } |
{\ 2 + (2 } |
|
{ orange + 2 } ¿Í °°Àº ¹«ÀǹÌÇÑ ÀÚ¹Ù½ºÅ©¸³Æ®ºí·°À» Á¦¿ÜÇϸé, ÅÛÇø´ÄÚµå´Â À̽ºÄÉÀÌÇÁÇÏÁö ¾Ê¾Æµµ ½ºÅ¸ÀÏ½ÃÆ®³ª ÀÚ¹Ù½ºÅ©¸³Æ®¿Í Ãæµ¹ÇÏÁö ¾Ê½À´Ï´Ù.
8. ·çÇÁ(2) - ¿¹¾àº¯¼ö
¡Ø index_ size_
| ¿¹¾àº¯¼ö |
¼³¸í |
| index_ |
0 ºÎÅÍ ½ÃÀÛ, ·çÇÁ°¡ ¹Ýº¹ÇÒ ¶§ 1 ¾¿ Áõ°¡ |
| size_ |
·çÇÁÀÇ Àüü ¹Ýº¹ ȸ¼ö |
| index.php |
<?php
...
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$result = mysql_query('select title from song');
$loop = array();
while ($data = mysql_fetch_assoc($result)) $loop[] = $data;
$tpl->assign('list', $loop);
$tpl->assign('start', 10);
$tpl->print_('index');
?>
|
| index.tpl |
<div>num of list: { list.size_ }</div>
<!--{@ list}-->
<div>{ list.index_ + start }th. { list.title }</div>
<!--{/}-->
|
| >>output |
<div>num of list: 3</div>
<div>10th. I Wish..</div>
<div>11th. Love Revolution 21</div>
<div>12th. The Peace</div>
|
size_ ´Â ÇØ´ç·çÇÁ ¹Û¿¡¼µµ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. ÇØ´ç·çÇÁ ¹Û¿¡¼ »ç¿ëÇÒ ¶§´Â {.size_} ó·³ ·çÇÁ¾ÆÀ̵𸦠»ý·«ÇÒ ¼ö´Â ¾ø½À´Ï´Ù.
¡Ø key_ value_
| ¿¹¾àº¯¼ö |
¼³¸í |
| key_ |
·çÇÁ·Î ÇÒ´çµÈ ¹è¿ÀÇ Å° |
| value_ |
·çÇÁ·Î ÇÒ´çµÈ ¹è¿ÀÇ °ª |
| index.php |
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$loop = array('apple'=>'red', 'banana'=>'yellow', 30 =>'unknown');
$tpl->assign('fruit', $loop);
$tpl->print_('index');
?>
|
| index.tpl |
<!--{@ fruit}-->
<div>{fruit.index_} - {fruit.key_} - {fruit.value_}</div>
<!--{/}-->
<div>num of fruit: {fruit.size_}</div>
|
| >>output |
<div>0 - apple - red</div>
<div>1 - banana - yellow</div>
<div>2 - 30 - unknown</div>
<div>num of list: 3</div>
|
9. ºÐ±â(1) - if
| ¸í·É¾î |
ÀÇ¹Ì |
»ç¿ë¹ý |
| ? |
if |
{? expression } |
| : |
elseif, else (¿É¼Ç) |
{: expression }, {:} |
| / |
endif |
{/} |
¹®¸Æ¿¡ ÀÇÇØ, ':' Àº ºÐ±âÀÇ else if, else ¶Ç´Â ·çÇÁÀÇ else ·Î, '/' Àº ·çÇÁ³ª ºÐ±âÀÇ Á¾°á¹®À¸·Î ÇØ¼®µË´Ï´Ù.
| ºÐ±â ¿¹Á¦ |
| index.php |
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$tpl->assign('fruit', 'banana');
$tpl->print_('index');
?>
|
| index.tpl |
<!--{? fruit=='apple' || person=='santa' }-->
<div> red </div>
<!--{: fruit=='banana' }-->
<div> yellow </div>
<!--{: fruit=='orange' }-->
<div> orange </div>
<!--{:}-->
<div> unknown </div>
<!--{/}-->
|
| >> output |
|
<div> yellow </div>
|
| ·çÇÁ-ºÐ±â Áßø ¿¹Á¦ |
| index.php |
<?php
include 'Template_.class.php'
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$page_loop = array(
1=>'page1.php',
2=>'page2.php',
3=>'page3.php',
);
$tpl->assign(array(
'current'=>2,
'page' =>$page_loop,
));
$tpl->print_('index');
?>
|
| index.tpl |
<!--{@ page}-->
<!--{? page.key_ == current }-->
[ { page.key_ } ]
<!--{:}-->
<a href='{ page.value_ }'>[ { page.key_ } ]</a>
<!--{/}-->
<!--{:}-->
-- no page --
<!--{/}-->
|
| >>output |
<a href='page1.php'>[ 1 ]</a>
[ 2 ]
<a href='page3.php'>[ 3 ]</a>
|
ºÐ±â¿Í ºÐ±â, ºÐ±â¿Í ·çÇÁ ¿ª½Ã ÀÚÀ¯·Ó°Ô ÁßøÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
10. ¹è¿
¹è¿¿ä¼Ò¸¦ ÇϳªÇϳª ÇÒ´çÇÏÁö ¾Ê°í ¹è¿ÀÚü¸¦ ÇϳªÀÇ ÅÛÇø´ º¯¼ö·Î ÇÒ´çÇÒ ¼ö ÀÖ½À´Ï´Ù. À妽º¹è¿, ¿¬°ü¹è¿, ´ÙÁß¹è¿ ¸ðµÎ °¡´ÉÇÕ´Ï´Ù.
| ¹è¿ ÇÒ´ç ¿¹Á¦ |
| index.php |
<?php
include 'Template_.class.php'
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$user = array(
'name'=>'julia',
'hobby'=>array('swimming', 'singing'),
);
$tpl->assign('user', $user);
$tpl->print_('index');
?>
|
| index.tpl |
|
<div>{user.name} {user.hobby[0]} {user.hobby[1]}</div>
|
| >>output |
|
<div>julia swimming singing</div>
|
ÀÚ¹Ù½ºÅ©¸³Æ®¿Í ¸¶Âù°¡Áö·Î {user.name} °ú {user["name"]}, {user['name']} Àº ¸ðµÎ °°À¸¸ç, {user[name]} ó·³ µû¿ÈÇ¥°¡ ¾øÀ» ¶§ÀÇ name Àº ÅÛÇø´ º¯¼öÀ̰í, À妽º ¹è¿ÀÇ °æ¿ì, [] ¸¦ »ç¿ëÇÏ´Â {style[23]} ÀÇ Çü½Ä¸¸ °¡´ÉÇÕ´Ï´Ù.
¹è¿¿ä¼Ò¸¦ ÅÛÇø´ Ç¥Çö½ÄÀÇ ÀϺηΠ»ç¿ëÇÒ ¼ö ÀÖÀ¸¸ç, ¹è¿À» ·çÇÁº¯¼ö·Î ÇÒ´çÇÒ ¼öµµ ÀÖ½À´Ï´Ù.
| ·çÇÁ-¹è¿ ÇÒ´ç |
| index.php |
<?php
...
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$result = mysql_query('select num, name, date from table');
$loop = array();
while ($data = mysql_fetch_array($result)) $loop[]['data'] = $data;
$tpl->assign(array(
'start'=>5,
'list' =>$loop,
));
$tpl->print_('index');
?>
|
| index.tpl |
<!--{@ list}-->
<div>{list.data.num + start}. {list.data.name} ({.data.date})</div>
<!--{/}-->
|
| >>output |
<div>6. Julia (1993-05-06)</div>
<div>7. Maria (2002-02-22)</div>
<div>8. Mickey (2003-03-01)</div>
|
·çÇÁº¯¼ö°¡ ¹è¿ÀÏ ¶§µµ {.data.num} °ú °°ÀÌ ·çÇÁ¾ÆÀ̵𸦠»ý·«ÇÒ ¼ö ÀÖ½À´Ï´Ù.
10. ÅÛÇø´º¯¼ö ÇÒ´ç»ý·«
ÅÛÇø´ º¯¼ö¸íÀÌ ¾ð´õ¹Ù '_' ·Î ½ÃÀÛÇÏ¸é ½ÇÇàÆÄÀϳ» Àü¿ªº¯¼öÀÇ °ªÀ» °¡Áö¸ç ÀÌ ¶§´Â assign() ÇÏÁö ¾Ê½À´Ï´Ù.
| Àü¿ªº¯¼ö ÇÒ´ç»ý·« |
| index.php |
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$apple = 'global apple';
$melon = array('red'=>5, 'green'=>10);
$tpl->assign(array(
'_apple' =>'this assignment has not effect.',
'apple' =>'apple by assignment',
'_orange'=>'no effect',
'orange' =>'orange by assignment',
));
$tpl->print_('index');
?>
|
| index.tpl |
<div>{_apple}</div>
<div>{apple}</div>
<div>{_melon.red + _melon.green + 100}</div>
<div>{_orange}</div>
|
| >>output |
<div>global apple</div>
<div>apple by assignment</div>
<div>115</div>
<div></div>
|
¾ð´õ¹Ù '_' ·Î ½ÃÀÛÇÏ´Â ÅÛÇø´ º¯¼ö´Â ÀÌ ¿ëµµ·Î¸¸ »ç¿ëµÇ¸ç, ¹è¿ÀÇ Å° ¶Ç´Â ·çÇÁ¿¡ Á¾¼ÓµÈ º¯¼ö¿¡´Â ÀÌ ±ÔÄ¢ÀÌ Àû¿ëµÇÁö ¾Ê½À´Ï´Ù.
¶ÇÇÑ ·çÇÁ¾ÆÀ̵ðÀÇ Ã¹±ÛÀÚ·Î ¾ð´õ¹Ù¸¦ »ç¿ëÇÒ °æ¿ì, Àü¿ªº¯¼ö¶ó¸é ÇÒ´çÀ» »ý·«ÇÒ ¼ö ÀÖ½À´Ï´Ù.
| index.php |
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$num = array(1, 2, 3);
$tpl->print_('index');
?>
|
| index.tpl |
<!--{@ _num}-->
<div> { _num.index_ } - { _num.key_ } - { .value_ } </div>
<!--{/}-->
|
| >>output |
<div> 0 - 0 - 1 </div>
<div> 1 - 1 - 2 </div>
<div> 2 - 2 - 3 </div>
|
Áßø·çÇÁÀÇ °æ¿ì, ¾ÈÂÊ ·çÇÁ´Â ¹Ù±ùÂÊ ·çÇÁÀÇ ¹è¿¿ä¼ÒÀ̹ǷÎ, °¡Àå ¹Ù±ùÂÊ ·çÇÁ ¾ÆÀ̵𿡸¸ ÀÌ ±ÔÄ¢ÀÌ Àû¿ëµË´Ï´Ù.
PHPÀÇ ÀÚµ¿Àü¿ª(auto global)º¯¼öÀÎ $_SERVER , $_ENV , $_COOKIE , $_GET , $_POST , $_FILES , $_REQUEST , $_SESSION µîµµ assign() ¿¡ ÀÇÇÑ ÇÒ´ç¾øÀÌ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
| ÀÚµ¿Àü¿ªº¯¼ö ÇÒ´ç»ý·« |
| index.php?abc=2 |
<?php
include 'Template_.class.php';
$tpl = new Template_;
$tpl->define('index', 'index.tpl');
$tpl->assign('number', 2);
$tpl->print_('index');
?>
|
| index.tpl |
<div>{ _SERVER.PHP_SELF }</div>
<div>{? !_COOKIE}Greetings{/}</div>
<div>{ _GET.abc + number }</div>
|
| >>output |
<div>/tutorial2/index.php</div>
<div>Greetings</div>
<div>4</div>
|
ÀÌ ±â´ÉÀ» »ç¿ëÇϸé assign() ÇÏÁö ¾ÊÀº ¸¸Å ÄÚµùÀ» Àý¾àÇÒ ¼ö ÀÖ°í ¼º´É»óÀÇ À̵æÀÌ ´Ù¼Ò ÀÖÀ¸³ª, ¸ðµâȸ¦ °í·ÁÇÏ¿© ÀûÀýÈ÷ »ç¿ëÇϽñ⠹ٶø´Ï´Ù.
|
|