Table of Contents
模��以是����的���供的。
å½“ä½ display() 或者
fetch()一个模�,
å�ˆæˆ–è€…å½“ä½ åŒ…å�«ä¸€ä¸ªæ¨¡æ�¿åˆ°å�¦ä¸€ä¸ªæ¨¡æ�¿ä¸çš„æ—¶å€™ï¼Œä½ 需è¦�æ��供一个资æº�类型ã€�路径å�Šæ¨¡æ�¿å��。
如果没有明确指明资�类型,则将�定是使用
$default_resource_type
(默认是“file�)。
Smarty通常�行在内置的�基于文件系统的模�资�上。
默认资�是file:。
如果修改了$default_resource_type
那么资æº�关键å—file:就必须è¦�指明。
如果找�到需�的模�文件,那么 $default_template_handler_func将会被调用。
说明
在Smarty 3.1开始,除�开�了
$use_include_path�置,�则文件资��会��索
include_path
的路径了。
Smarty从$template_dir设置的目录ä¸ï¼Œ
获�模�文件资�。
多个模æ�¿ç›®å½•将以在数组ä¸çš„顺åº�进行æ�œç´¢ï¼Œåœ¨å¯»æ‰¾åˆ°ç¬¬ä¸€ä¸ªåŒ¹é…�的模æ�¿æ—¶å°†è¿”回。
Example 16.1. 使用$template_dir
<?php
$smarty->display('index.tpl');
$smarty->display('file:index.tpl'); // �上
?>
模æ�¿ä¸ä½¿ç”¨
{include file='index.tpl'}
{include file='file:index.tpl'} {* �上 *}
Smarty 3.1 æ��供“归类符å�·â€�æ�¥å®šä¹‰$template_dirä¸çš„ç‰¹å®šå…ƒç´ ã€‚
æ¤ç‰¹æ€§å�¯å…�许网站ä¸ä½¿ç”¨å’Œæ›´å¥½åœ°ç®¡ç�†å¤šä¸ªæ¨¡æ�¿é›†å�ˆã€‚
“归类符���以用在任何定义了file:资�类型的地方。
Example 16.2. 特定的$template_dir
<?php
// 设置模�目录
$smarty->setTemplateDir(array(
'./templates', // element: 0, index: 0
'./templates_2', // element: 1, index: 1
'10' => 'templates_10', // element: 2, index: '10'
'foo' => 'templates_foo', // element: 3, index: 'foo'
));
/*
�定模�目录结构如下:
./templates/foo.tpl
./templates_2/foo.tpl
./templates_2/bar.tpl
./templates_10/foo.tpl
./templates_10/bar.tpl
./templates_foo/foo.tpl
*/
// æ£å¸¸è¯»å�–
$smarty->display('file:foo.tpl');
// 将载入 ./templates/foo.tpl
// 默认使用数å—ä¸‹æ ‡
$smarty->display('file:[1]foo.tpl');
// 将载入 ./templates_2/foo.tpl
// 使用å—ç¬¦ä¸²çš„ä¸‹æ ‡ï¼ˆ'10'看起æ�¥åƒ�æ•°å—ä¸‹æ ‡ï¼Œä½†å�´æ˜¯æœ‰å�•引å�·çš„å—符串)
$smarty->display('file:[10]foo.tpl');
// 将载入 ./templates_10/foo.tpl
// 使用å—ç¬¦ä¸²çš„ä¸‹æ ‡
$smarty->display('file:[foo]foo.tpl');
// 将载入 ./templates_foo/foo.tpl
// 使用 "未知" æ•°å—ä¸‹æ ‡ (ç”¨å…ƒç´ çš„æ•°å—)
$smarty->display('file:[2]foo.tpl');
// 将载入 ./templates_10/foo.tpl
?>
模æ�¿ä¸è°ƒç”¨
{include file="file:foo.tpl"}
{* 将载入 ./templates/foo.tpl *}
{include file="file:[1]foo.tpl"}
{* 将载入 ./templates_2/foo.tpl *}
{include file="file:[foo]foo.tpl"}
{* 将载入 ./templates_foo/foo.tpl *}
在$template_dir之外的模�,
file:将需�使用�对路径�获�模�。
Note
当Security开�,
在$template_dir之外的模æ�¿æ˜¯ä¸�å…�许读å�–的,除é�žä½ 将这些目录都设置在$secure_dirä¸ã€‚
Example 16.3. 任�目录的模�
<?php
$smarty->display('file:/export/templates/index.tpl');
$smarty->display('file:/path/to/my/templates/menu.tpl');
?>
模æ�¿ä¸ï¼š
{include file='file:/usr/local/share/templates/navigation.tpl'}
如果使用Windows的机器,那么文件路径将以驱动盘符(C:)开头。
确�file:的路径��命�空间冲�,达到需�的结果。
Example 16.4. 使用windows的文件路径
<?php
$smarty->display('file:C:/export/templates/index.tpl');
$smarty->display('file:F:/path/to/my/templates/menu.tpl');
?>
在模æ�¿ä¸ï¼š
{include file='file:D:/usr/local/share/templates/navigation.tpl'}
