From 9bcc04d1ebbb5d250d40d63a7309ab6c8fecad3a Mon Sep 17 00:00:00 2001 From: mohrt Date: Mon, 1 Mar 2004 20:08:54 +0000 Subject: [PATCH] update componentized template example to something useful --- docs/appendixes.sgml | 68 ++++++++++---------------------------------- 1 file changed, 15 insertions(+), 53 deletions(-) diff --git a/docs/appendixes.sgml b/docs/appendixes.sgml index a82585ac..647f2084 100644 --- a/docs/appendixes.sgml +++ b/docs/appendixes.sgml @@ -269,10 +269,6 @@ Pretty easy isn't it? Componentized Templates - This tip is a bit of a hack, but still a neat idea. Use at your own - risk. ;-) - - Traditionally, programming templates into your applications goes as follows: First, you accumulate your variables within your PHP 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? - You can embed PHP into your templates with the {php}{/php} tags. - With this, you can setup self contained templates with their own - data structures for assigning their own variables. With the logic - embedded like this, you can keep the template & logic together. This - way no matter where the template source is coming from, it is always - together as one component. + You can do this by writing a custom plugin for fetching the content and + assigning it to a template variable. componentized template -{* Smarty *} - -{php} - - // setup our function for fetching stock data - function fetch_ticker($symbol,&$ticker_name,&$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} - - - 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 include_php function for - more information. - - -componentized template with include_php - -load_ticker.php +function.load_ticker.php --------------- <?php +function smarty_function_load_ticker($params, &$smarty) { // setup our function for fetching stock data - function fetch_ticker($symbol,&$ticker_name,&$ticker_price) { - // put logic here that fetches $ticker_name - // and $ticker_price from some resource + function fetch_ticker($params['symbol']) { + // put logic here that fetches $ticker_info + // from some resource + return $ticker_info; } // call the function - fetch_ticker("YHOO",$ticker_name,$ticker_price); + $ticker_info = fetch_ticker("YHOO",$ticker_info); - // assign template variables - $this->assign("ticker_name",$ticker_name); - $this->assign("ticker_price",$ticker_price); + // assign template variable + $smarty->assign($params['assign'],$ticker_info); +} ?> - index.tpl --------- {* Smarty *} -{include_php file="load_ticker.php"} +{load_ticker symbol="YHOO" assign="ticker"} -Stock Name: {$ticker_name} Stock Price: {$ticker_price} +Stock Name: {$ticker.name} Stock Price: {$ticker.price} @@ -400,4 +362,4 @@ Send inquiries to check the website. - \ No newline at end of file +