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

Using smarty within custom php functions.

 
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
programmer79
Smarty Rookie


Joined: 03 Sep 2003
Posts: 13

PostPosted: Wed Sep 03, 2003 12:07 am    Post subject: Using smarty within custom php functions. Reply with quote

Is there anyway to use smarty within a function without changing global settings and without registering the function within smarty? It seems like it would be overkill to register every function if you had a large amount of them.

ex. (I realize that this isn't a very useful example, but I was going for simplicity.)
[php:1:a789b05a11]<?php

$smarty = new Smarty;

test_function();

function test_function(){
$smarty->assign('test_assignment','blah');
}
?>[/php:1:a789b05a11]
Back to top
View user's profile Send private message Visit poster's website
mocean
Smarty Rookie


Joined: 21 May 2003
Posts: 30
Location: The Netherlands

PostPosted: Wed Sep 03, 2003 5:35 pm    Post subject: Re: Using smarty within custom php functions. Reply with quote

try passing a reference to the object to the function
[php:1:b971e0a8c0]

<?php

$smarty = new Smarty;

test_function($smarty);

function test_function(&$obj){
$obj->assign('test_assignment','blah');
}
?>

[/php:1:b971e0a8c0]
_________________
Visit My Smarty Enabled Website (Dutch Only) at www.habiforum.nl
and read how it works.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
CirTap
Smarty Pro


Joined: 04 Jun 2003
Posts: 106

PostPosted: Thu Sep 04, 2003 12:16 am    Post subject: Reply with quote

Hi,
altough it looks "ugly" this works as well, and is handy for functions that need to have their arguments be "locked down"
Quote:
function testy() {
$GLOBALS['smarty']->assign("var", "value");
}

// or you can get the reference the other ways round
function testx() {
...
$page =& $GLOBALS['smarty'];
$page->assign("var1", "value1");
$page->assign("var2", "value2");
// mucho assign
$page->assign("varN", "valueN");
// other non-smarty stuff
...
}

Any of this may be the only way if test() is in some include file that runs itself in a function()
I used sth. similiar to the last one for a 3rd party tool where I wanted to use "some Smarty instance" instead of the built-in "template engine", so I wrote some wrapper that allowed me to optionally use Smarty for certain parts.

CirTap
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