Comunitatea PHP Romania
 

 
Componentized Templates

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

<?php

// 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

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

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

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

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
la birou
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
Mascka Mascka
Braila
raul_ raul_
whooper whooper
Toronto ON
mihaitha mihaitha
Sibiu
gabysolomon gabysolomon
Bacau
oriceon oriceon
Constanta
garlicinicolae garlicinicolae

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Parteneriat

Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink Fast Loans | Xbox Mod Chips | New York Hotels | Hotel Las Vegas | Loans
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis