update componentized template example to something useful

This commit is contained in:
mohrt
2004-03-01 20:08:54 +00:00
parent 9ddf034f6f
commit 9bcc04d1eb

View File

@@ -269,10 +269,6 @@ Pretty easy isn't it?
<sect1 id="tips.componentized.templates"> <sect1 id="tips.componentized.templates">
<title>Componentized Templates</title> <title>Componentized Templates</title>
<para> <para>
This tip is a bit of a hack, but still a neat idea. Use at your own
risk. ;-)
</para>
<para>
Traditionally, programming templates into your applications goes as Traditionally, programming templates into your applications goes as
follows: First, you accumulate your variables within your PHP follows: First, you accumulate your variables within your PHP
application, (maybe with database queries.) Then, you instantiate your application, (maybe with database queries.) Then, you instantiate your
@@ -284,74 +280,40 @@ Pretty easy isn't it?
template, and not worry about fetching the data up front? template, and not worry about fetching the data up front?
</para> </para>
<para> <para>
You can embed PHP into your templates with the {php}{/php} tags. You can do this by writing a custom plugin for fetching the content and
With this, you can setup self contained templates with their own assigning it to a template variable.
data structures for assigning their own variables. With the logic
embedded like this, you can keep the template &amp; logic together. This
way no matter where the template source is coming from, it is always
together as one component.
</para> </para>
<example> <example>
<title>componentized template</title> <title>componentized template</title>
<programlisting> <programlisting>
{* Smarty *} function.load_ticker.php
{php}
// setup our function for fetching stock data
function fetch_ticker($symbol,&amp;$ticker_name,&amp;$ticker_price) {
// put logic here that fetches $ticker_name
// and $ticker_price from some resource
}
// call the function
fetch_ticker("YHOO",$ticker_name,$ticker_price);
// assign template variables
$this->assign("ticker_name",$ticker_name);
$this->assign("ticker_price",$ticker_price);
{/php}
Stock Name: {$ticker_name} Stock Price: {$ticker_price}</programlisting>
</example>
<para>
As of Smarty 1.5.0, there is even a cleaner way. You can include php in
your templates with the {include_php ...} tag. This way you can keep
your PHP logic separated from the template logic. See the <link
linkend="language.function.include.php">include_php</link> function for
more information.
</para>
<example>
<title>componentized template with include_php</title>
<programlisting>
load_ticker.php
--------------- ---------------
&lt;?php &lt;?php
function smarty_function_load_ticker($params, &amp;$smarty) {
// setup our function for fetching stock data // setup our function for fetching stock data
function fetch_ticker($symbol,&amp;$ticker_name,&amp;$ticker_price) { function fetch_ticker($params['symbol']) {
// put logic here that fetches $ticker_name // put logic here that fetches $ticker_info
// and $ticker_price from some resource // from some resource
return $ticker_info;
} }
// call the function // call the function
fetch_ticker("YHOO",$ticker_name,$ticker_price); $ticker_info = fetch_ticker("YHOO",$ticker_info);
// assign template variables // assign template variable
$this->assign("ticker_name",$ticker_name); $smarty->assign($params['assign'],$ticker_info);
$this->assign("ticker_price",$ticker_price); }
?&gt; ?&gt;
index.tpl index.tpl
--------- ---------
{* Smarty *} {* Smarty *}
{include_php file="load_ticker.php"} {load_ticker symbol="YHOO" assign="ticker"}
Stock Name: {$ticker_name} Stock Price: {$ticker_price}</programlisting> Stock Name: {$ticker.name} Stock Price: {$ticker.price}</programlisting>
</example> </example>
</sect1> </sect1>
<sect1 id="tips.obfuscating.email"> <sect1 id="tips.obfuscating.email">