|
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 |
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Mon Aug 11, 2003 6:57 am Post subject: Custom {include} tag |
|
|
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 |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Mon Aug 11, 2003 12:49 pm Post subject: |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Mon Aug 11, 2003 2:46 pm Post subject: |
|
|
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.
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
Thanks anyway,
andre |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Mon Aug 11, 2003 3:05 pm Post subject: |
|
|
Resistance is futile you will accept it 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 |
|
|
|
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
|