Get Smarty


Donate Bitcoin Bitcoin

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from and save up to 80%.

Where to buy prescription glasses? is your best choice!

Where to buy discount wedding dresses and cheap smart dresses free shipping -

Brautkleider auf

Buy New Arrival Cheap Prom Dresses 2015 at JDBRIDAL Prom Dress Store

Find Wedding Dresses Online at Canada from

Cheap bridesmaid dresses - rush delivery only 10 days delivery to Australia at

Wholesale Camping Equipment from China at


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 18.7. componentized template

function.load_ticker.php - drop file in $plugins 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);



{load_ticker symbol='SMARTY' assign='ticker'}

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


See also {include_php}, {include} and {php}.

No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Email: (not shown)
What is 12 plus 0? (Are you human?)