Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

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

Check reviews of PHP Web Hosting, compatible with Smarty Templates

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy wedding dresses on SiteSteer.com - Best Online Wedding Dress Store

Candy Crush Cheats

Tired of playing games? Visit the Game Supply to Sell World of Warcraft Accounts.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at Bestpriceglasses.com. Starts At $7.95.

Variable scopes

You have the choice to assign variables to the scope of the main Smarty object, data objects created with createData(), and template objects created with createTemplate(). These objects can be chained. A template sees all the variables of its own object and all variables assigned to the objects in its chain of parent objects.

By default templates which are rendered by $smarty->display(...) or $smarty->fetch(...) calls are automatically linked to the Smarty object variable scope.

By assigning variables to individual data or template objects you have full control which variables can be seen by a template.

Example 4.6. Variable scope examples



// assign variable to Smarty object scope
$smarty->assign('foo','smarty');

// assign variables to data object scope
$data = $smarty->createData();
$data->assign('foo','data');
$data->assign('bar','bar-data');

// assign variables to other data object scope
$data2 = $smarty->createData($data);
$data2->assign('bar','bar-data2');

// assign variable to template object scope
$tpl = $smarty->createTemplate('index.tpl');
$tpl->assign('bar','bar-template');

// assign variable to template object scope with link to Smarty object
$tpl2 = $smarty->createTemplate('index.tpl',$smarty);
$tpl2->assign('bar','bar-template2');

// This display() does see $foo='smarty' from the $smarty object
$smarty->display('index.tpl');

// This display() does see $foo='data' and $bar='bar-data' from the data object $data
$smarty->display('index.tpl',$data);

// This display() does see $foo='data' from the data object $data 
// and $bar='bar-data2' from the data object $data2
$smarty->display('index.tpl',$data2);

// This display() does see $bar='bar-template' from the template object $tpl
$tpl->display();  // or $smarty->display($tpl);

// This display() does see $bar='bar-template2' from the template object $tpl2
// and $foo='smarty' form the Smarty object $foo
$tpl2->display();  // or $smarty->display($tpl2);

    


See also assign(), createData() and createTemplate().

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

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
Buy from China @dhgate.com
dissertationteam.com
Buy Web Hosting @buy-hosting.net
WordPress Hosting Hub @wphostinghub.net