|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Cybermage Smarty n00b
Joined: 30 May 2003 Posts: 1
|
Posted: Fri May 30, 2003 11:13 pm Post subject: to assign() or not to assign() |
|
|
is there a difference between the following examples? and why should i prefer one of them?
[php:1:58b11414d7]$smarty = new Smarty;
$smarty->assign('name', 'Bob');
$smarty->display('index.tpl');[/php:1:58b11414d7]
[php:1:58b11414d7]$smarty = new Smarty;
$name = 'Bob';
$smarty->display('index.tpl');[/php:1:58b11414d7] |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Fri May 30, 2003 11:36 pm Post subject: |
|
|
no, in the second example, $name cannot be addressed from within the template. you have to implicitely assign every variable you plan to use in your template
hth
David |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 31, 2003 12:09 am Post subject: |
|
|
I'm finding that I am using assigned objects more often:
[php:1:a1f2018bd1]<?php
class MyClass
{
var $var1 = "var1";
var $var2 = "var2";
function GetSomething( $text ) { return $text; }
}
$obj = MyClass;
$smarty->assign_by_ref( 'obj', $obj );
?>[/php:1:a1f2018bd1]
and then in template:
Code: | {$obj->var1}
{$obj->var2}
{$obj->GetSomething( "boots" )} |
For the safety concious, you would register the object directly:
[php:1:a1f2018bd1]<?php
class MyClass
{
var $var1 = "var1";
var $var2 = "var2";
function GetSomething( $text ) { return $text; }
}
$obj = MyClass;
$smarty->register_object( 'obj', $obj, array( 'var1'. 'var2', 'GetSomething' ) );
?>[/php:1:a1f2018bd1]
and then in template:
Code: | {obj->var1}
{obj->var2}
{obj->GetSomething p1="boots"} |
With registered objects, only the functions and parameters listed in the array passed to register_object will be allowed. Also notice that you don't use the var $ syntax. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|