{fetch}

{fetch}

{fetch} is used to fetch files from the local file system, http, or ftp and display the contents. If the file name begins with "http://", the web site page will be fetched and displayed. If the file name begins with "ftp://", the file will be fetched from the ftp server and displayed. For local files, the full system file path must be given, or a path relative to the executed php script.

If you supply the special "assign" attribute, the output of the {fetch} function will be assigned to this template variable instead of being output to the template. (new in Smarty 1.5.0)

Attribute NameTypeRequiredDefaultDescription
filestringYesn/athe file, http or ftp site to fetch
assignstringNon/athe template variable the output will be assigned to

Technical Note: This will not support http redirects, be sure to include a trailing slash on your web page fetches where necessary.

Technical Note: If template security is turned on and you are fetching a file from the local file system, this will only allow files from within one of the defined secure directories. ($secure_dir)

Example 8-6. 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"}

{* assign the fetched contents to a template variable *}
{fetch file="http://www.myweather.com/68502/" assign="weather"}
{if $weather ne ""}
  <b>{$weather}</b>
{/if}

See also {capture}, {eval} and fetch().

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.