{fetch}
{fetch}
用于从本地文件系统、http 或 ftp 检索文件并显示内容。
属性
属性 | 必需 | 说明 |
---|---|---|
文件 | 是 | 要获取的文件、http 或 ftp 网站 |
分配 | 否 | 输出将分配给的模板变量 |
-
如果文件名以
http://
开头,则将获取并显示网站页面。注意事项
这将不支持 http 重定向,请务必在必要时在你的网页获取中包含尾部斜杠。
-
如果文件名以
ftp://
开头,则将从 ftp 服务器下载文件并显示。 -
对于本地文件,必须给出完整的系统文件路径或相对于执行 PHP 脚本的路径。
注意事项
如果启用安全性并且你要从本地文件系统获取文件,则
{fetch}
仅允许安全策略的$secure_dir
路径中的文件。有关详细信息,请参阅 安全性 部分。 -
如果设置了
assign
属性,则{fetch}
函数的输出将分配给此模板变量,而不是输出到模板。
示例
{* include some javascript in your template *}
{fetch file='/export/httpd/www.example.com/docs/navbar.js'}
{* embed some weather text in your template from another web site *}
{fetch file='http://www.myweather.com/68502/'}
{* fetch a news headline file via ftp *}
{fetch file='ftp://user:password@ftp.example.com/path/to/currentheadlines.txt'}
{* as above but with variables *}
{fetch file="ftp://`$user`:`$password`@`$server`/`$path`"}
{* assign the fetched contents to a template variable *}
{fetch file='http://www.myweather.com/68502/' assign='weather'}
{if $weather ne ''}
<div id="weather">{$weather}</div>
{/if}