Web page hosting and cheap domain registration register services
 

Sponsers:

 

 

 

Smarty Manual
PrevChapter 17. Tips & TricksNext

Componentized Templates

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 Smarty object, assign the variables and display the template. So lets say for example we have a stock ticker on our template. We would collect the stock data in our application, then assign these variables in the template and display it. Now wouldn't it be nice if you could add this stock ticker to any application by merely including the template, and not worry about fetching the data up front?

You can do this by writing a custom plugin for fetching the content and assigning it to a template variable.

Example 17-7. componentized template

<?php

// drop file "function.load_ticker.php" in plugin directory

// setup our function for fetching stock data
function fetch_ticker($symbol)
{
   
// put logic here that fetches $ticker_info
   // from some ticker resource
   
return $ticker_info;
}

function
smarty_function_load_ticker($params, &$smarty)
{
   
// call the function
   
$ticker_info = fetch_ticker($params['symbol']);
   
   
// assign template variable
   
$smarty->assign($params['assign'], $ticker_info);
}
?>
index.tpl
---------

{* Smarty *}

{load_ticker symbol="YHOO" assign="ticker"}

Stock Name: {$ticker.name} Stock Price: {$ticker.price}

PrevHomeNext
WAP/WMLUpObfuscating E-mail Addresses
 

 

 

 

Search engine submission by Seosubmit.com | Cheap domain by Cheap Domain Registrar  

Disclaimers: This document is provided as free service for the benefits of Active-Venture.com's web hosting customers only.

 
Quotes: The most common error made in matters of appearance is the belief that one should disdain the superficial and let the true beauty of one's soul shine through. If there are places on your body where this is a possibility, you are not attractive - you are leaking.