Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

to assign() or not to assign()

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Cybermage
Smarty n00b


Joined: 30 May 2003
Posts: 1

PostPosted: Fri May 30, 2003 11:13 pm    Post subject: to assign() or not to assign() Reply with quote

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
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Fri May 30, 2003 11:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat May 31, 2003 12:09 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP