Catyã®ãã³ãã¬ã¼ãå¦çã«ã¤ãã¦å度èãã¦ã¿ãã®ã§ãããããç¨åº¦ä¸è¬è«ã£ã½ãæ¸ãæ¹ã§è¿°ã¹ã¾ããé¢æ°å¼ã³åºãã®è¨æ³ãå ¨é¢çã«ä½¿ãã¾ãã
å 容ï¼
- ãããã¬ãã«ã®ãã³ãã¬ã¼ãå¦ç
- ãµããã³ãã¬ã¼ãã®å¦ç
- ãµããã³ãã¬ã¼ãã«æ¸¡ãæ å ±
- ãµããã³ãã¬ã¼ãã«å ¥åã¨ãã©ã¡ã¼ã¿ã渡ã
- ãã³ãã¬ã¼ãå¦çãä¸å åãããã£ãçç±
- ããã§ä½ãã§ããã
ãããã¬ãã«ã®ãã³ãã¬ã¼ãå¦ç
ãã³ãã¬ã¼ãããã¹ãtemplãããã¨ããã³ã³ããã¹ãï¼åå-å¤ãã¢ã®éã¾ãï¼contextã使ã£ã¦ãã³ãã¬ã¼ãããã¹ããå±éãããã¨ã expand(context, templ) ã¨æ¸ãã¾ãããããã®é¢æ°å¼ã³åºãã®å¤ããã³ãã¬ã¼ãå±éçµæã§ããcontextãåãªããã¼ã¿ã§ã¯ã¤ã¾ããªãã®ã§ãããã°ã©ã ã®å®è¡çµæã ã¨ãã¾ããããã§ã¯ãå¼exprãè©ä¾¡ããå¤ eval(expr) ãã³ã³ããã¹ãã«ãªãã¨ãã¾ããããããã¨ã次ããã³ãã¬ã¼ãå±éçµæã§ãã
- expand(eval(expr), templ)
evalãããå°ãç²¾å¯åãã¦ãå ¥åinputã¨ãã©ã¡ã¼ã¿pramsã«ãä¾åãã¦è©ä¾¡ããã¨ãã¾ããããinputãparamsãè¦ããã«å¤é¨ãã渡ããããã¼ã¿ã£ã¦ãã¨ã§ãã¦ãã²ã¨ã¤ã«ã¾ã¨ãã¦ãããã®ã§ããã便å®ä¸2ã¤ã«ãã¦ããã¾ããããããã¨ããã³ãã¬ã¼ãå±éã¯æ¬¡ã®å½¢ã«ãªãã¾ãã
- expand(eval(input, expr, params), templ)
ä¸ã®å¼ãresolveã¨ããé¢æ°ã«ã¾ã¨ãã¾ãã
- resolve(input, expr, params, templ) := expand(eval(input, expr, params), templ)
ããã§ãtemplã¨exprã¯ããã¼ã¿ã¨ãã¦ã¯åãªãããã¹ãã§ãããããã®ããã¹ãã¯ãã¡ã¤ã«ã·ã¹ãã ã«ä¿åããã¦ããã¨ãã¾ããexpr_path, templ_path ã¯ãã¡ã¤ã«ã·ã¹ãã ã®ãã¹ï¼æååï¼ã¨ãã¦ãããã¹ãèªä½ã®ä»£ããã«ããã¡ã¤ã«ã®ãã¹ãå¼æ°ã«ãã¾ããããããã¨*1ï¼
- resolve_path(input, expr_path, params, templ_path) := resolve(input, read(expr_path), params, read(templ_path))
ããã«ãtempl_pathã¨expar_pathã«æ³åçãªé¢ä¿ããã£ã¦ãtempl_pathããããã°expr_pathãåããã¨ãã¾ããããã使ã£ã¦æ¸ãæããã¨ï¼
- resolve_path_2(input, params, templ_path) := resolve_path(input, read_expr(templ_path), params, read(templ_path))
ãã®resolve_path_2ãWebå¦çã ã¨ã¿ãªãã¾ããããã¤ã¾ãã次ã®ããã«è§£éãã¾ãã
- input ï¼ POSTãã¼ã¿
- params ï¼ ã¯ã¨ãªã¼æåå
- templ_path ï¼ ãã³ãã¬ã¼ããã¡ã¤ã«ãåç §ããURL
ãããããååã«æ¸ãæãã¦ã¿ãã¨ï¼
- process_request(post_data, url_path, query_params)
ããä¸åº¦å®ç¾©ãæ¯ãè¿ãã°ï¼
process_request(post_data, url_path, query_params)
:= expand(eval(post_data, read_expr(url_path), query_params),
read(url_path))
ãµããã³ãã¬ã¼ãã®å¦ç
ãã¬ã³ã¹ã»ãã¼ãè¨ã£ã¦ããããã³ãã¬ã¼ãã®æå°æ©è½4ã¤ãã®ãªãã«ããµããã³ãã¬ã¼ãæ©è½ãããã¾ã*2 -- ããã¯ããã³ãã¬ã¼ãä¸ã«å¥ãªãã³ãã¬ã¼ãã®å±éçµæãæ¿å ¥ã§ããã¨ããæ©è½ã§ãã
ãµããã³ãã¬ã¼ãã§ãã£ã¦ããexpand(eval(input, expr, params), templ) ã§å±éãããã¨ã«å¤ããã¯ããã¾ããããã ããç¾ç¶ã®Catyã§ã¯ãinputãparamsã使ããªãã®ã§ãã¨ãããã expand(eval(expr), templ) ããèãã¾ãããµããã³ãã¬ã¼ãããã¡ã¤ã«ã«æ ¼ç´ããã¦ããã®ã§ãã¹å templ_path ãæã¡ã¾ããè©ä¾¡ãã¹ãå¼ãtempl_pathããå¾ãããã¨ããä»®å®ãåããªã®ã§ï¼
- expand(eval(read_expr(templ_path)), read(templ))
ã¨ãªãã¾ããããã ãã§ã¯ãããã¬ãã«ã®å±éã¨å¤ããã¾ããããå®ã¯ããµããã³ãã¬ã¼ãã®å ´åã¯è¦ªã®åå¨ãç¡è¦ã§ããªãã®ã§ããã©ããããã¨ãã¨è¨ãã¨ã親ã®ã³ã³ãã¹ããåï¼ãµããã³ãã¬ã¼ãï¼ã«ãé©ç¨ãããã®ã§ããã³ã³ããã¹ãã®ãã¼ã¸ã context1 +< context2 ã®ããã«ã足ãç®è¨å·ã¨ä¸çå·ãçµã¿åããã¦æ¸ããã¨ãã¾ããä¸çå·ã¯ã大ããæ¹ãåªå ãããã¨ããæå³ã§ä½¿ãã¾ããä»ã®ä¾ã§ã¯ãcontext2 ãåªå ããã®ã§ãcontext1 ã«ããåãååã®å¤æ°ã¯é ããã¾ãã
親ã®ã³ã³ããã¹ããå½±é¿ãããã¨ãæ¸ãä¸ãã¨ï¼
- expand(parent_context +< eval(read_expr(templ_path)), read(templ))
ããã ãã§ãã»ã¨ãã©ã®ãµããã³ãã¬ã¼ãå¦çã¯ååã§ãããããµããã³ãã¬ã¼ãå¦çã®ã»ããå°ãæ©è½ã貧弱ã«ãªã£ã¦ãã¾ãããã®ç¹ã以ä¸ã§ã¯åé¡ã«ãã¾ãã
ãµããã³ãã¬ã¼ãã«æ¸¡ãæ å ±
ä»ã¾ã§ã«è©±ããæ¹æ³ã§ã¯ã親ããåï¼ãµããã³ãã¬ã¼ãï¼ã«æ¸¡ãæ å ±ã¯ã³ã³ããã¹ãã ãã§ãããã®ã³ã³ããã¹ãããåä¾ã®å´ã§çæããã³ã³ããã¹ãããå¼±ãå½¢ã§ããå½±é¿ãã¾ããã親ãåã«å½±é¿ãä¸ããæ段ãéããã¦ãã¾ãã
ã¾ããã³ã³ãããã§è¦ªãåã«å½±é¿ãä¸ããæ¹æ³ãå°ãæ¡å¼µãã¾ãããµããã³ãã¬ã¼ããå¼ã³åºãï¼ã¤ã³ã¯ã«ã¼ãããï¼æç¹ã§ãã³ã³ããã¹ãã®ä¸é¨ãã»ããã§ããã¨ä¾¿å©ã§ããããSmartyã§ã¯æ¬¡ã®ããã«æ¸ãã¾ãã
{include file="header.tpl" greeting="ããã£ãããã¾ãã"}
ããã§ãheader.tplã¯ãµããã³ãã¬ã¼ãã®ãã¹åã§ããgreetingã®æå®ã¯ã³ã³ããã¹ãã¨ãã¦åä¾ã«æ¸¡ã£ã¦ãããã¨ã«ãªãã¾ããincludeæ令ï¼ãã³ãã¬ã¼ãã¿ã°ï¼ã®ãã©ã¡ã¼ã¿ï¼å±æ§ï¼ã§ããfileã¨ãåä¾ã«æ¸¡ããã©ã¡ã¼ã¿ã®æ§æãåããªã®ãã©ãã«ãã¤ã¤ã§ãã次ã®ãããªæ§æã®ã»ããåã¯å®å¿ã§ãã
{include file="header.tpl"}
{context name="greeting" value="ããã£ãããã¾"}
{/include}
ã¾ã¼ãããããæ¸ãã®ãé¢åãã¨ããäºææ§ããªããã¨ãã®çç±ã§ãæ°æã¡æªãã¦ãSmartyæ§æã«ããã®ãç¾å®çãªã®ããããã¾ããã
æ§æã®åé¡ããã¦ããã°ãã³ã³ããã¹ããåä¾ã«æ¸¡ãæ¹æ³ã¯ããã§ããã¨ãã¾ãããã¨ã¯ãå¼è©ä¾¡ï¼evalï¼ã®ã¨ãã®inputã¨paramsã§ãã
ãµããã³ãã¬ã¼ãã«å ¥åã¨ãã©ã¡ã¼ã¿ã渡ã
å ¥åï¼inputï¼ã¨ãã©ã¡ã¼ã¿ï¼paramsï¼ã®ãªãã§ããã©ã¡ã¼ã¿ã¯ãã¹åã«ä»å ãã¦ãã¾ã£ã¦ããã¨æãã¾ããã¤ã¾ããã¯ã¨ãªæååæ¹å¼ã§ãã
{include file="header.tpl?name=hiyama"}
ããã«ãã£ã¦ãevalãå©ç¨ãããã©ã¡ã¼ã¿ {"name": "hiyama"} ãå¾ããã¾ããWebããã®ãªã¯ã¨ã¹ãã®ã¨ãã¨ã¾ã£ããåãæ¹å¼ãªã®ã§çµ±ä¸æ§ããããã¨ã
æå¾ã«æ®ã£ãinputã«é¢ãã¦ã¯ãincludeãã³ãã¬ã¼ãã¿ã°ãæã¤å±æ§ã¨ã㦠"input" ã¨ã "data" ã¨ãã追å ãããããªãããã§ãã
{include file="listCart.tpl" input=$cartInfo}
ãã³ãã¬ã¼ãå¦çãä¸å åãããã£ãçç±
以ä¸ã®ããã«ããã¨ããããã¬ãã«ã®ãã³ãã¬ã¼ãå¦çã¨ãµããã³ãã¬ã¼ãã®å¦çã®åºå¥ããªããªãã¾ããæè¿ã¾ã§åã¯ããµããã³ãã¬ã¼ãã¯includeçµç±ã§ããã¢ã¯ã»ã¹ãããªãã®ã ããããããã¬ãã«ã«æ¯ã¹ã¦ç°¡ç¥ãªå¦çã§ãããã¨æã£ã¦ãã¾ãããããããã©ããªãã³ãã¬ã¼ããã¡ã¤ã«ã§ãã次ã®3ã¤ã®æ¹æ³ã§ã¢ã¯ã»ã¹ãããå¯è½æ§ãããã¾ãã
- Webããã®é常ã®ãªã¯ã¨ã¹ãï¼GETã¾ãã¯POSTï¼
- 親ãã³ãã¬ã¼ãããã®include
- Ajaxï¼XMLHttpRequestï¼éä¿¡ã®ãªã¯ã¨ã¹ãï¼GETã¾ãã¯POSTï¼
Ajaxã«ãããªã¯ã¨ã¹ãã¯ãincludeå¦çããªã¢ã¼ãåãããããªãã®ã§ããããã¬ãã«ã»ãã³ãã¬ã¼ãå¦çã¨ãµããã³ãã¬ã¼ãå¦çã®ä¸éã®ï¼ãããã¯æ··ãã£ãï¼ãããªæ§æ ¼ãæã¡ã¾ãããªã¯ã¨ã¹ããããç¶æ³ãå¢ããã«å¾ãå¦çæ¹å¼ãå¢ããã¨è¤éåããã®ã§ããªã¯ã¨ã¹ãç¶æ³ã®ãã¨ã¯èããªãã§ãã©ããªã¨ãã§ãåãæ¹æ³ã§å¦çããã»ããã·ã³ãã«ã«ãªãã§ãããã
ã¨ããããã§ããã³ãã¬ã¼ãå¦çã¯ä¸å¾ã«æ¬¡ã®é¢æ°ã§è¡¨ç¾ã§ãã¾ãã
expand(parent_context +<
eval(input, read_expr(templ_path), params),
read(templ_path))
ãããã¬ãã«ã»ãã³ãã¬ã¼ãå¦çã®ã¨ãã¯ãparent_contextã空ï¼{}ï¼ã«ãªãã ãã§ãã
ããã§ä½ãã§ããã
ãã³ãã¬ã¼ãå¦çã®includeã¯ããµã¼ãå´ã§å¦çãå®äºããåºæ¥ä¸ãã£ãçµæããã©ã¦ã¶ã«éããã¾ããAjaxã使ãã¨ãincludeã®ã¿ã¤ãã³ã°ãå¤ããäºãã§ãã¦ããã©ã¦ã¶ã«å±ããå¾ã§ãªã¢ã¼ãããincludeå¦çãä¾é ¼ãããã¨ãã§ãã¾ãããããã»ãã«ã½ã³ã®è¨èãåããã°ãã©ã³ã¹ã¯ã«ã¼ãï¼transcludeï¼ã¨è¨ããã§ãããã
ã¡ã«ããºã çã«ã¤ã³ã¯ã«ã¼ãã¨ãã©ã³ã¹ã¯ã«ã¼ããå¥ã ã«æ±ã£ã¦ããã®ã¯ã«ãã³æªãããå¼å®³ããããããªã®ã§ããããã¬ãã«ã®ã¢ã¯ã»ã¹ï¼ã¤ã³ã¯ã«ã¼ãã»ã¢ã¯ã»ã¹ï¼ãã©ã³ã¹ã¯ã«ã¼ãã»ã¢ã¯ã»ã¹ã®ãµã¼ãã¼å´å¦çãå ¨é¨åãã«ãã¦ãããããã¨ãã話ã§ãã
*1:ããã£ãä»æ°ãã¤ãããã©ãresolve path ã£ã¦ãã¼ã ãµã¼ãã¼ã¿ãããªå°è±¡ãããã¾ããã確ãã«ããã³ãã¬ã¼ãã®ãã¹ããã¹ã¯ãªããã®ãã¹ãå¼ãå½ã¦ãç¹ã§ã¯ãã¼ã ãµã¼ãã¹ã£ã½ãã§ãããresolveã®æå³ã¨ãã¦ã¯ï¼è±è¾éããï¼ï¼ããããå¥ã®ãã®ã«ãå¤åãããã転æããããã¨ããããããåã®æå³ã§ãã
*2:ä»ã®3ã¤ã¯ããã³ãã¬ã¼ãå¤æ°ãifæãforeachæã§ãã