Files
smarty/docs/en/programmers/api-functions/api-fetch.xml

157 lines
3.9 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<refentry id="api.fetch">
<refnamediv>
<refname>fetch()</refname>
<refpurpose>returns the template output</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>string</type><methodname>fetch</methodname>
<methodparam><type>string</type><parameter>template</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>cache_id</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>$compile_id</parameter>
</methodparam>
</methodsynopsis>
<para>
This returns the template output instead of
<link linkend="api.display">displaying</link> it.
Supply a valid <link
linkend="template.resources">template resource</link>
type and path. As an optional second parameter, you can pass a
cache id. See the <link linkend="caching">caching
section</link> for more information.
</para>
&parameter.compileid;
<para>
<example>
<title>fetch()</title>
<programlisting role="php">
<![CDATA[
<?php
include('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;
// only do db calls if cache doesn't exist
if(!$smarty->is_cached('index.tpl')) {
// dummy up some data
$address = '245 N 50th';
$db_data = array(
'City' => 'Lincoln',
'State' => 'Nebraska',
'Zip' => '68502'
);
$smarty->assign('Name','Fred');
$smarty->assign('Address',$address);
$smarty->assign($db_data);
}
// capture the output
$output = $smarty->fetch('index.tpl');
// do something with $output here
echo $output;
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Using fetch() to send an email</title>
<para>
The email_body.tpl template
</para>
<programlisting>
<![CDATA[
Dear {$contact.name},
Welcome and thankyou for signing up as a member of our user group,
Click on the link below to login with your user name of '{$contact.login_id}'
so you can post in our forums.
http://{$smarty.server.SERVER_NAME}/index.php?page=login
List master
Some user group
{include file="email_disclaimer.tpl"}
]]>
</programlisting>
<para>
The email_disclaimer.tpl template which uses the
<link linkend="language.function.textformat">{textformat}</link> modifier.
</para>
<programlisting>
<![CDATA[
{textformat wrap=40}
Unless you are named "{$contact.name}", you may read only the "odd numbered
words" (every other word beginning with the first) of the message above. If you have
violated that, then you hereby owe the sender 10 GBP for each even
numbered word you have read
{/textformat}
]]>
</programlisting>
<para>
and the php script using the PHP
<ulink url="&url.php-manual;function.mail">mail()</ulink> function
</para>
<programlisting role="php">
<![CDATA[
<?php
// get contact from database eg using pear or adodb
$query = 'select name, email, login_id from contacts where contact_id='.$contact_id;
$contact = $db->getRow($sql);
$smarty->assign('contact', $contact);
mail($contact['email'], 'Subject', $smarty->fetch('email_body.tpl'));
?>
]]>
</programlisting>
</example>
</para>
<para>
See also
<link linkend="language.function.fetch">{fetch}</link>
<link linkend="api.display">display()</link>,
<link linkend="language.function.eval">{eval}</link>,
and
<link linkend="api.template.exists">template_exists()</link>.
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->