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

Custom {include} tag

 
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
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Aug 11, 2003 6:57 am    Post subject: Custom {include} tag Reply with quote

I am just thinking of how to implement a custom include tag. Normally I would use the following way

Code:
function myInclude($params, &$smarty) {
  // [...] do some preprocessing here
  if (isset($params["foo"]))
    $smarty->assign("foo", $params["foo"]);
  if (isset($params["bar"]))
    $smarty->assign("bar", $params["bar"]);
  return $smarty->fetch($params["file"]);
}


But the problem with the above solution is that smarty will add "foo" and "bar" to global variable scope. This means that after the {myInclude} the template variables "foo" and "bar" will be overwritten. Imagine this:

Code:
{myInclude foo="blah" bar="blub" file="mytemplate1.tpl"}
{myInclude foo="hello world" file="mytemplate2.tpl"}


For the second myInclude "bar" isn't set but because it's assigned to global scope in first myInclude it's still set when calling "mytemplate2.tpl".

The next bad thing happens when using the following code:
Code:
{assign var="foo" value="blah"}
{myInclude foo="hello world" file="mytemplate.tpl"}
{$foo} {* THIS WILL PRINT "hello world" INSTEAD OF "blah" *}


Any ideas how to solve this in an elegant way? For now I am using the following code:

Code:
function myInclude($params, &$smarty) {
  // [...] do some preprocessing here
  $copyOfSmarty = $smarty; // copy smarty object
  $copyOfSmarty->caching = 0;
  $copyOfSmarty->clear_all_assign();
  // [...]
  if (isset($params["foo"]))
    $copyOfSmarty->assign("foo", $params["foo"]);
  if (isset($params["bar"]))
    $copyOfSmarty->assign("bar", $params["bar"]);
  return $copyOfSmarty->fetch($params["file"]);
}
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Mon Aug 11, 2003 12:49 pm    Post subject: Reply with quote

How about something like[php:1:a22ff91bff]function myInclude($params, &$smarty) {
$oldvars = array();
foreach($params as $k => $v) {
if(isset($smarty->_tpl_vars[$k])) {
$oldvars[$k] = $smarty->_tpl_vars[$k];
}
$smarty->assign($k, $v);
}
$smarty->clear_assign('file');
$return = $smarty->fetch($param['file']);
foreach($params as $k => $v) {
if(isset($oldvars[$k])) {
$smarty->assign($k, $oldvars[$k]);
} else {
$smarty->clear_assign($k);
}
}
return $return;
}[/php:1:a22ff91bff]of course I wrote this on the fly and it lacks error checking but you get the idea
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon Aug 11, 2003 2:46 pm    Post subject: Reply with quote

Yeah, I already thought of a solution like this but it's
a) too slow for alot of includes
b) not really nicer than the other solution. Wink

But I have just checked the implementation of the {include} function and noticed that it's done nearly the same way:

Code:
// [...]
$smarty_tpl_vars = $smarty->_tpl_vars;
$smarty->assign( ... );
$smarty->fetch( ... );
$smarty->_tpl_vars = $smarty_tpl_vars;
// [...]


Mhm... perhaps I should just accept that Wink

Thanks anyway,
andre
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Mon Aug 11, 2003 3:05 pm    Post subject: Reply with quote

Resistance is futile you will accept it Smile actually thats about the only logical way I can think of doing it.
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
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