{include}鐢ㄤ簬杞藉叆鍏朵粬妯℃澘鍒板綋鍓嶆ā鏉夸腑銆�
鍦ㄥ寘鍚ā鏉夸腑鍙敤鐨勫彉閲忥紝杞藉叆鍚庡湪褰撳墠妯℃澘浠嶇劧鍙敤銆�
{include}蹇呴』璁剧疆file灞炴�э紝璁剧疆杞藉叆鐨勬枃浠惰祫婧愯矾寰勩��璁剧疆浜嗗彲閫夌殑
assign灞炴�э紝灏�{include}妯℃澘鐨勫唴瀹硅祴鍊煎埌鍙橀噺锛岃�屽苟闈炶緭鍑恒�� 涓�{assign}鎿嶄綔鐩镐技銆�鍖呭惈妯℃澘鏃讹紝鍙互鍍忎娇鐢�灞炴��涓�鏍疯缃紶閫掔殑鍙橀噺銆� 杩欐牱浼犻�掔殑鍙橀噺锛屼綔鐢ㄨ寖鍥翠粎闄愪簬鍖呭惈鐨勬ā鏉垮唴銆� 灞炴�т紶閫掔殑鍙橀噺灏嗚鐩栧師鍖呭惈妯℃澘鐨勫悓鍚嶅彉閲忋��
浣犲彲浠ュ湪褰撳墠妯℃澘鍐呬娇鐢ㄥ寘鍚ā鏉跨殑鍏ㄩ儴鍙橀噺銆� 浣嗘槸濡傛灉鍖呭惈妯℃澘鍐呮湁淇敼鎴栬�呮柊寤哄彉閲忥紝閭d箞杩欎簺鍙橀噺鍙湁鍖呭惈妯℃澘鐨勪綔鐢ㄨ寖鍥达紝鑰屼笉鍙互鏄綋鍓�
{include}妯℃澘涓娇鐢ㄣ�� 杩欑榛樿鐨勮缃紝鍙互閫氳繃鍦ㄥ寘鍚ā鏉挎椂璁剧疆{include}鐨勪綔鐢ㄨ寖鍥村睘鎬э紝鎴栬�� 鍦ㄤ慨鏀规垨鏂板鍙橀噺鏃堕�氳繃{assign}鐨勪綔鐢ㄨ寖鍥村睘鎬ф潵璁惧畾銆� 鍚庤�呭湪闇�瑕佸寘鍚ā鏉胯繑鍥炲�兼椂姣旇緝鏈夌敤銆�褰撴枃浠朵笉鍦�
$template_dir鐩綍涓椂锛� 浣跨敤璧勬簮鐨勮娉曟潵{include}鍖呭惈鏂囦欢銆�
Attributes:
| 鍙傛暟鍚嶇О | 绫诲瀷 | 蹇呴�夊弬鏁� | 榛樿鍊� | 璇存槑 |
|---|---|---|---|---|
| file | string | Yes | n/a | 鍖呭惈杞藉叆鐨勬枃浠跺悕 |
| assign | string | No | n/a | 灏嗗寘鍚殑鏂囦欢鍐呭璧嬪�肩粰鍙橀噺 |
| cache_lifetime | integer | No | n/a | 鍗曠嫭寮�鍚鍖呭惈妯℃澘鐨勭紦瀛樻椂闂� |
| compile_id | string/integer | No | n/a | 鍗曠嫭璁剧疆琚寘鍚ā鏉跨殑缂栬瘧ID |
| cache_id | string/integer | No | n/a | 鍗曠嫭璁剧疆琚寘鍚ā鏉跨殑缂撳瓨ID |
| scope | string | No | n/a | 瀹氫箟琚寘鍚ā鏉跨殑璧嬪�煎彉閲忎綔鐢ㄨ寖鍥�: 'parent','root' 鎴� 'global' |
| [var ...] | [var type] | No | n/a | 浼犻�掑埌鍖呭惈妯℃澘鐨勫彉閲� |
鍙�夋爣璁�:
| 鍚嶇О | 璇存槑 |
|---|---|
| nocache | 鍏抽棴鍖呭惈妯℃澘鐨勭紦瀛� |
| caching | 鎵撳紑鍖呭惈妯℃澘鐨勭紦瀛� |
| inline | 璁剧疆鎴恡rue鏃讹紝鍦ㄧ紪璇戞椂鎶婂寘鍚ā鏉跨殑鍐呭涔熷悎骞跺埌褰撳墠妯℃澘鐨勭紪璇戞枃浠朵腑銆� |
Example聽7.46.聽绠�鍗� {include} 渚嬪瓙
<html>
<head>
<title>{$title}</title>
</head>
<body>
{include file='page_header.tpl'}
{* body of template goes here, the $tpl_name variable
is replaced with a value eg 'contact.tpl'
*}
{include file="$tpl_name.tpl"}
{* using shortform file attribute *}
{include 'page_footer.tpl'}
</body>
</html>
Example聽7.47.聽{include} 浼犻�掑彉閲�
{include 'links.tpl' title='Newest links' links=$link_array}
{* body of template goes here *}
{include 'footer.tpl' foo='bar'}
鍖呭惈浜嗕笅闈㈢殑 links.tpl 妯℃澘
<div id="box">
<h3>{$title}{/h3>
<ul>
{foreach from=$links item=l}
.. do stuff ...
</foreach}
</ul>
</div>
Example聽7.48.聽{include} 浣滅敤鑼冨洿绀轰緥
鍦ㄥ寘鍚殑妯℃澘鍐呰祴鍊肩殑鍙橀噺锛屽湪鍖呭惈妯℃澘鍐呭彲瑙併��
{include 'sub_template.tpl' scope=parent}
...
{* display variables assigned in sub_template *}
{$foo}<br>
{$bar}<br>
...
鍖呭惈浜嗕笅闈㈢殑 sub_template.tpl 妯℃澘
...
{assign var=foo value='something'}
{assign var=bar value='value'}
...
Example聽7.50.聽{include} 鍗曠嫭鐨勭紦瀛樻椂闂�
涓嬮潰渚嬪瓙鍖呭惈妯℃澘灏嗗崟鐙缃紦瀛樻椂闂�500绉掋��
{include 'sub_template.tpl' cache_lifetime=500}
...
Example聽7.51.聽{include}寮�鍚紦瀛�
涓嬮潰鐨勪緥瀛愬寘鍚ā鏉跨殑缂撳瓨灏嗙嫭绔嬩簬鍏ㄥ眬妯℃澘缂撳瓨璁剧疆涔嬪銆�
{include 'sub_template.tpl' caching}
...
Example聽7.52.聽{include} 鍜岃祴鍊煎彉閲�
涓嬮潰鐨勪緥瀛愬皢nav.tpl鐨勫唴瀹硅祴鍊肩粰浜�$navbar 鍙橀噺,
璇ュ彉閲忓皢椤甸潰鐨勫ご閮ㄥ拰搴曢儴鏄剧ず銆�
<body>
{include 'nav.tpl' assign=navbar}
{include 'header.tpl' title='Smarty is cool'}
{$navbar}
{* body of template goes here *}
{$navbar}
{include 'footer.tpl'}
</body>
Example聽7.53.聽{include} 鐩稿璺緞
涓嬮潰鐨勪緥瀛愬寘鍚殑妯℃澘鏂囦欢閮芥槸鐩稿褰撳墠妯℃澘鐨勭洰褰曘��
{include 'template-in-a-template_dir-directory.tpl'}
{include './template-in-same-directory.tpl'}
{include '../template-in-parent-directory.tpl'}
Example聽7.54.聽鍚勭 {include} 璧勬簮渚嬪瓙
{* absolute filepath *}
{include file='/usr/local/include/templates/header.tpl'}
{* absolute filepath (same thing) *}
{include file='file:/usr/local/include/templates/header.tpl'}
{* windows absolute filepath (MUST use "file:" prefix) *}
{include file='file:C:/www/pub/templates/header.tpl'}
{* include from template resource named "db" *}
{include file='db:header.tpl'}
{* include a $variable template - eg $module = 'contacts' *}
{include file="$module.tpl"}
{* wont work as its single quotes ie no variable substitution *}
{include file='$module.tpl'}
{* include a multi $variable template - eg amber/links.view.tpl *}
{include file="$style_dir/$module.$view.tpl"}
鍙傝
{include_php},
{insert},
{php},
妯℃澘璧勬簮 鍜�
妯℃澘缁勪欢鍖�.
