ãã¶ã¤ãã¼ã¨ã®åæ¥ã§ã®å·¥å¤«ãSmartyããªãã£ã«ã¿ã®æ´»ç¨æ³
ãã¾è¡ãªã£ã¦ããæ¡ä»¶ã§ã社å¤ã®ãã¶ã¤ãã¼ãããä½ã£ããã¶ã¤ã³ãã·ã¹ãã ã«åãè¾¼ãã¨ãã件ãããããäºãã«å´åã®å°ãªãåºæ¥ãæ¹æ³ãèãã¦ã¿ãã®ã§ããã«æ®ãã¦ãããåæã¨ãã¦ãã·ã¹ãã ã°ãã°ããªãã®ã§ã¯ãªãããã¶ã¤ã³ãã¡ã¤ã³ã ãããã©ã¼ã ããããã¼ã¸ãæ稿系ã§ã·ã¹ãã ã§åºãã¹ãä¸è¦§ãã¼ã¸ããã£ãããããããªãµã¤ãã®å ´åã§ãã
åºæ¬çãªææ³
åºæ¬çã«ã¯ããã¶ã¤ãã¼ãããä½ã£ãhtmlãã¡ã¤ã«ã«æ¥µåããã°ã©ãå´ã§æãå
¥ããªããéã«ããã°ã©ã ä¸å¿
è¦ãªã¿ã°çãåãè¾¼ãã å ´åã«ã¯ããã®ãã¼ã¸å¾ã®ãã¡ã¤ã«ãä¿®æ£ãã¦ãããã
ä»åã¯ããã¬ã¼ã ã¯ã¼ã¯ã«Ethnaããã³ãã¬ã¼ãã¨ã³ã¸ã³ã«ã¯Smartyã使ã£ã¦ããã¾ãã
æè¿ãSmartyãããªãã¨ãã風潮ã§ãããããªãã£ã«ã¿ãªã©ã®ãã©ã°ã¤ã³æ©è½ã¯æç¨ã ã¨æãã¾ãã
ä»çµã¿ã¨ãã¦ä½ã£ããã®
- 1. .htmlãã¡ã¤ã«ãã¨ã³ããªãã¤ã³ãã«ãã
- 2. Smartyã®ããªãã¿ãå¤ãã
- 3. Smartyã®ããªãã¿ã®ç¨®é¡å¢ãã
- 4. http<->httpsã«çµ¶å¯¾ãã¹ã§æ¸ããªãã¦ãåãããã«ãã
- 5. é¨åã®includeãåºæ¥ãããã«ãã
1. .htmlãã¡ã¤ã«ãã¨ã³ããªãã¤ã³ãã«ãã
ãã¶ã¤ãã¼ããã¯ãå½ç¶htmlãã¡ã¤ã«ã¨ãã¦ãã¶ã¤ã³ããã¾ãããããã·ã¹ãã å´ã«åãè¾¼ããã¨ã«ãªãã®ã ãããã®æ®µéã§ãã¡ã¤ã«ã®é層ããããããURLãå¤ãã£ã¦ãã¾ãã¨ãåãã¼ã¸ããã®ãªã³ã¯ãªã©ãä¿®æ£ããå¿
è¦ãåºã¦ãã¾ãã¾ãã
éã«ãã¼ã¸ã®URLãã·ã¹ãã å´ã§æ±ºããè«ççãªãã®ï¼ç©çãã¡ã¤ã«ãç¡ããã®ï¼ã«ãã¦ãã¾ãã¨ããã¶ã¤ã³ä½ææã«ãªã³ã¯ãæ£ããããªã©ã®ç¢ºèªãç
©éã«ãªãã¾ãã
ããã§ãä»åã¯ã.htmlã®æ¡å¼µåãä»ãããã¡ã¤ã«ã phpã¨ãã¦åãããããã«ãããã¨ã³ããªãã¤ã³ãã¨ãã¾ããã¾ãåæã«ããã³ãã¬ã¼ãã«ããªãããã«ãã¾ãã
ä¾) http://example.com/page/request_form.html â request_form ã¢ã¯ã·ã§ã³ãèµ·åãã
ãã³ãã¬ã¼ãã¨ãã¦ä½¿ç¨ããä»çµã¿ã¯ãEthnaã®å ´åã«ã¯ãViewã® forward_path ãããã£ã¦ããããã¨ã§å®ç¾ããã¦ãã¾ãã
.htmlãã¡ã¤ã«ã php ã¨ãã¦åããã«ã¯ã以ä¸ã®ãã㪠.htaccess ãªã©ã§è¨å®ããã¦ãã¾ãã
#php settings AddType application/x-httpd-php .html AddType application/x-httpd-php .htm <FilesMatch "\.html?$"> php_value auto_prepend_file "/path/to/_action.php" # AcceptPathInfo Apache 2.0 only AcceptPathInfo Off </FilesMatch>
2. Smartyã®ããªãã¿ãå¤ãã
Smartyã®ããªãã¿ã¯ã次ã®ããã«ãã¦ãã¾ãã
<?php //è²ã¥ã $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
htmlå ã§ãã® ã <{ ã ã¨ããæ¸ãæ¹ã¯é常åºã¦ãããã¨ã¯ãããã¾ããããªããªããæç« ä¸ã§æ¸ãããã®ãªãã°ã ã <{ ã ã¨æ¸ãã¹ãã§ãhtmlã¿ã°ã¨ãã¦ã¯ã< ã®æ¬¡ã«ã¯ãhtmlã¿ã°ã¨ãã¦æå¹ãªã¢ã«ãã¡ãããããhtmlã³ã¡ã³ãç¨ã® ! ããåºã¦ãããã¨ã¯ãããã¾ããã
ããã¯ã以åãã¡ãã®è¨äºã§ãæ¸ãããã¨ãããã¾ããã
ãSmartyのデリミタとescape - maru.cc@はてなã
3. Smartyã®ããªãã¿ã®ç¨®é¡å¢ãã
2ã®ã <{ ããã®å½¢å¼ã®ããªãã¿ã使ç¨ããå ´åã®å¼å®³ã¨åºã¦ããã®ã¯ãhtmlã¿ã°å ã®å±æ§å¤ã«Smartyã¿ã°ãå ¥ãããå ´åã«ãhtmlã¨ãã¦ããããªç¶æ ã«ãªã£ã¦ãã¾ãã¾ãã
ä¾) <a href="sample.html?id=<{$id}>">link</a> <input type="text" name="sample" value="<{$form.sample}>" />
ãããåé¿ããããã«ãSmartyã®ããªãã¿ã®ç¨®é¡ãå¢ããã¦ã¿ã¾ãããæ£ç¢ºã«ã¯ããªãã¿ã¨ãã¦èªèãããæååãå¢ããã¦ã¿ã¾ãããã¾ããhtmlã¿ã°å
ã§ä½¿ãããã«urlencodeãããæååã使ãããã«ãã¦ã¿ã¾ãã
ä»åæ¡ç¨ããæ¹æ³ã¨ãã¦ã¯ã次ã®ãããªã«ã¼ã«ã§ãã
ããªãã¿ã¨ãã¦æ¬¡ã®æåã追å
- %3C%7B
- %7D%3E
ãããã ã <{ ã ã }> ã ã urlencodeãããã®ã§ããã¾ãããã®ã¿ã°å
㯠urldecodeãããããã«ãã¾ããã
å®éã«è¨è¿°ããå ´åã«ã¯æ¬¡ã®ããã«ãªãã¾ãã
ä¾) <a href="sample.html?id=%3C%7B%24id%7D%3E">link</a> <input type="text" name="sample" value="%3C%7B%24form.sample%7D%3E" />
ã$ãã¯ãurldecodeãã¦ããã®ã¾ã¾ãªã®ã§ã次ã®ããã«æ¸ãã¦ãOKã
ä¾) <a href="sample.html?id=%3C%7B$id%7D%3E">link</a> <input type="text" name="sample" value="%3C%7B$form.sample%7D%3E" />
ãããæå¹ã«ããã«ã¯ã以ä¸ã®ãã㪠Smartyã®ããªãã£ã«ã¿ãã©ã°ã¤ã³ã使ç¨ãã¾ãã
<?php /** * Smartyããªãã£ã«ã¿ãã©ã°ã¤ã³ */ /** * ã¿ã°å ã®å±æ§å¤çã«Smartyã¿ã°ãå ¥ããããã®æ¡å¼µ * * <pre> * Smartyã®ããªãã¿ã®URLã¨ã³ã³ã¼ãå¤ã®ç¯å²å ãurldecodeãã * ex.) * left_delimiter = '<{' * right_delimiter = '}>' * <input type="text" name="sample" value="%3C%7B%24form.sample%7D%3E" /> * â * <input type="text" name="sample" value="<{$form.sample}>" /> * </pre> */ function smarty_prefilter_delimiter_urldecode($source, &$smarty) { $pattern = sprintf('!(%s.*%s)!msueU' , preg_quote(urlencode($smarty->left_delimiter), '!') , preg_quote(urlencode($smarty->right_delimiter), '!') ); $buf = preg_replace($pattern, "urldecode('\$1')", $source); return $buf; }
ãã®ãã¡ã¤ã«ã Smartyãã©ã°ã¤ã³ãã£ã¬ã¯ããªã¨ãã¦ç»é²ãããã£ã¬ã¯ããªå
ã« ãprefilter.delimiter_urldecode.phpãã¨ãããã¡ã¤ã«åã§ä¿åããSmartyã®èªã¿è¾¼ã¿ãã©ã°ã¤ã³ã«ç»é²ãã¾ãã
<?php //è²ã¥ã $smarty->autoload_filters['pre'][] = 'delimiter_urldecode';
ã¨ãããããä»åã¯ãã®ç¨åº¦ã§æ¸ã¾ãã¾ããããif ã foreach ãhtmlã³ã¡ã³ãå½¢å¼ã«ããããªãã¿å½¢å¼ã追å ããã®ãããã ã¨æãã¾ãã
4. http<->httpsã«çµ¶å¯¾ãã¹ã§æ¸ããªãã¦ãåãããã«ãã
htmlãä½ãããã§ãç°å¢ä¾åã«ãªããã¡ãªã®ããhttpé åã¨SSLé åãè¡ãæ¥ãããªã³ã¯ã§ããURLã« http ã¾ã㯠https ããæ¸ããã°ãªããã絶対ãã¹ã«ãªã£ã¦ãã¾ãã®ã§ç°å¢ä¾åã«ãªãã¾ãããã¡ãã Smartyã®ããªãã£ã«ã¿ã使ç¨ãã¦ç½®æå¦çããã¦ãã¾ãã¾ãã
ç§ã®ä¼ç¤¾ã§ã¯ããã¥ã¡ã³ãã«ã¼ãã¯æ¬¡ã®ãããªååã使ç¨ãã¦ãã¾ãã
- htdocsããã<-httpé åã®ããã¥ã¡ã³ãã«ã¼ã
- htdocs.sslã<- httpsé åã®ããã¥ã¡ã³ãã«ã¼ã
ãã¡ããããããç¸å¯¾ãã¹ã§æ¸ãã¦ããã£ã¦ç½®æããä»çµã¿ã§ãã
ä¾) http://example.com/index.html -> https://example.com/form.html ã¸ã®ãªã³ã¯ <a href="../htdocs.ssl/form.html">ãªã³ã¯</a> https://example.com/sample1/page1.html -> http://example.com/sample2/page2.html ã¸ã®ãªã³ã¯ <a href="../../htdocs/sample2/page2.html">ãªã³ã¯</a>
phpã®ã³ã¼ãã¯å²æãã¾ãããä¸è¨ã®ããªãã¿ç½®æã¨åæ§ã®å½¢å¼ã§ããå®éã«ä½¿ç¨ãã¦ããããªãã£ã«ã¿ã§ã¯ãhtmlã®ã¿ã°ã¨å±æ§ã¾ã§èå¥ããåãªãæååç½®æã§ã¯ãªããaã¿ã°ãªã©ã®ã¿ã°ã¨hrefãªã©ã®å±æ§ã¾ã§è¦ãããã«ãã¦ãã¾ãã
5. é¨åã®includeãåºæ¥ãããã«ãã
htmlããã¶ã¤ã³ããæã«ãå®éã«ã¯ãããã¼ãããã¿ã¼ãªã©ã¯é¨ååãã¦ä½æãã¦ããã®ã§ãããããã®ã¾ã¾ä½¿ããããã«ãã¾ãã
ãã¶ã¤ã³æç¹ã§ã¯é¨ååããSSIã®ã¤ã³ã¯ã«ã¼ãã§ç¢ºèªãªã©ããã¦ããã¡ãã®ã·ã¹ãã ã¸ã®çµã¿è¾¼ã¿ã¯ãã®SSIã®ã¿ã°ã®ã¾ã¾ãã³ãã¬ã¼ãã¨ãã¦ä½¿ç¨ããSSIã®ã¤ã³ã¯ã«ã¼ãã¿ã°ã Smartyã®ã¤ã³ã¯ã«ã¼ãã¿ã°ã«ç½®æããããªãã£ã«ã¿ãä½æãã¾ããã
ä¾) htmlä¸ã®è¨è¿° <!--#include virtual="/include/header.html" --> âç½®æå¾ <{include file="/path/to/include/header.html"}>
Smartyããªãã£ã«ã¿ãã©ã°ã¤ã³
<?php /** * Smartyããªãã£ã«ã¿ãã©ã°ã¤ã³ */ /** * SSIå½¢å¼ã®includeã¿ã°ãèªèãããããã®æ¡å¼µ * * <pre> * ex.) * <!--#include virtual="/include/header.html" --> * â * <{include file="/path/to/include/header.html"}> * </pre> */ function smarty_prefilter_ssiparts_include($source, &$smarty) { $pattern = sprintf('/<!--#include +virtual *= *"\/([^"]+)" +-->/msuU' ); $path = $_SERVER['DOCUMENT_ROOT']; $buf = preg_replace($pattern, "<{include file='{$path}/\$1'}>", $source); return $buf; }
ãã®ãã¡ã¤ã«ã Smartyãã©ã°ã¤ã³ãã£ã¬ã¯ããªã¨ãã¦ç»é²ãããã£ã¬ã¯ããªå
ã« ãprefilter.ssiparts_include.phpãã¨ãããã¡ã¤ã«åã§ä¿åããSmartyã®èªã¿è¾¼ã¿ãã©ã°ã¤ã³ã«ç»é²ãã¾ãã
<?php //è²ã¥ã $smarty->autoload_filters['pre'][] = 'ssiparts_include';
æå¾ã«
ããã¯ããã¾ã§ãä»çµã¿ã§å¸åã§ããã§ãããé¨åã®ã¿ãªã®ã§ãå®éã«ã¯ãã¶ã¤ãã¼ããã SSIã§é¨åãåãã¦ããããã¨ãããã¼ã¸å¾ã®æååãå£ããªãããªã©ã®åé¡ã¯ããã¾ãããæèãããããå¿
è¦ãããã¾ãã
ä»åãSSIã¿ã°æ¹å¼ãªã©ã¯ãå
ã
ã¯ç¬èªã¿ã°ãæ¸ãã¦ãããã¤ããã§ãããããã¼ãã£ã³ã°æã«SSIã§é¨ååãããããªè©±ãåºã¦ããã®ã§ãã®å ´ã§æ±ºããä»æ§ã ã£ãããã¾ãã
ãã¾ããããªãå ´é¢ãåºã¦ããã¨ã¯æãã¾ãããéæããæ¹ãã¢ãããã¼ããã¦ããããã¨æãã¾ãã
æè¿ã®phpçéã®äººãã¡ã¨è©±ãã¨ããã³ãã¬ã¼ãã¨ã³ã¸ã³ãã©ãããã¨ãã話ãããåºã¦ããã¨æãã¾ãããã¾ã©ãSmartyãç¡ãããã¨ããã®ãããèãã¾ãã
確ãã«ãã·ã¹ãã ããªããªã®ä½ãããã 管çç»é¢ã«ãä¸è¨ã®ãããªä»çµã¿ãå°å
¥ãããã¨ã¯æãã¾ãããããã¯éçºè
ã楽ã«ãªãããã«ä½ãã°ããã¨æãã¾ããããµã¤ãã®ã¦ã¼ã¶å´ã¯ã©ããã¦ããã¶ã¤ã³ã®è¦ç´ ãã¨ã¦ã大ãããªãã¾ãããµã¤ãã«ãããã¾ããã
ãã®å ´åã«ããã¶ã¤ãã¼ããã¯ã¿ã°ãç解ãã¦ãããªãã¨ããéçºå´ã¯ãã¶ã¤ã³å¤æ´æã«æããããããããããªãã¨ããã
ã©ã¡ããä¸æ¹ã«è² æ
ããããããæ¹ã§ã¯ãªãããäºããå°ãã®åªåã§ããè¯ã楽ã«ãªããããªä»çµã¿ã«ãã¦ãããã°ã¨å¸¸ã
èããããã¦ãã¾ãã
ã§ãSmartyã®æ¯éã§ãããå人çã«ã¯ä½¿ãåæãããããEthnaã®ããã©ã«ããã³ãã¬ã¼ãã¨ã³ã¸ã³ã¨ãããã¨ã§å¤ç¨ãã¦ãã¾ãã
ä¸è¨ã®ãããªç½®æããããã¨ãã¦ãããªãã£ã«ã¿ã§ããã°ãSmartyã®ã³ã³ãã¤ã«æã«1度ã ãåããã¨ã«ãªãã¾ããã
Smartyã®è©±ãã§ã¦ããæã«æ°ã«ãªãã®ã¯ããã®ãã㪠Smartyãã©ã°ã¤ã³ããããããããã¦ããã® Smartyã ã¨æãã¾ãã®ã§ãã¿ã°å½¢å¼ãªã©ã ãã§è©±ãã®ã¯å°ãä¸æ¯ãªæ°ããã¾ãã