Smarty:fetch函數(shù)

2018-10-15 08:30 更新

{fetch}

{fetch}用于獲取文件內(nèi)容、HTTP或者FTP內(nèi)容,以便輸出。

  • 如果提供的文件名是以http://開頭,那么網(wǎng)站的內(nèi)容將被獲取并顯示。

    溫馨提示:

    該函數(shù)不支持直接的HTTP訪問,請確認(rèn)網(wǎng)頁的地址是以斜杠結(jié)尾。

  • 如果文件名是以ftp://開頭,那么文件會被從FTP中下載并顯示。

  • 本地文件而言,可以使用絕對路徑的文件,或者相對于當(dāng)前執(zhí)行的PHP腳本的路徑文件均可。

    溫馨提示:

    如果開啟了安全機(jī)制,那么從本地獲取文件內(nèi)容,{fetch}函數(shù)僅能讀取在 $secure_dir安全目錄下的文件。 參見安全機(jī)制。

  • 如果你提供了assign 屬性, {fetch}函數(shù)的輸出將不會顯示,而是賦值給模板變量。

參數(shù)名稱類型必選參數(shù)默認(rèn)值說明
filestringYesn/a獲取的文件名、HTTP或者FTP網(wǎng)址
assignstringNon/a用于賦值的變量名

Example 8.5. {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}

參見 {capture}{eval}{assign} 和 fetch().

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號