|
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 |
apeiros Smarty n00b
Joined: 24 Apr 2003 Posts: 4
|
Posted: Mon Oct 06, 2003 12:11 pm Post subject: Hacked function.assign.php to allow array appends |
|
|
I wanted to assign a value to an array from within a template and realized that it wasn't possible (or me to stupid to understand how I was supposed to do so )
However, I hacked it into the standard-plugin "assign". Maybe somebody else can use it too or it might even make it into the smarty-code.
It's a quick hack and I have not yet tested it intensely so maybe there might come an update soon.
To append a value to an array you write e.g.:
Code: | {assign var="previews.number" value="20"} |
Which works similar to the nativ PHP code:
[php:1:e00571d5b7]<?php
$previews['number'] = '20';
?>[/php:1:e00571d5b7]
I choose the . as delimiter because you can't use it anyways in a variable name within smarty and because it reflects the usage of smarty to call an index of an array.
And here the code:
[php:1:e00571d5b7]<?php
function smarty_function_assign($params, &$smarty)
{
extract($params);
if (empty($var)) {
$smarty->trigger_error("assign: missing 'var' parameter");
return;
}
if (!in_array('value', array_keys($params))) {
$smarty->trigger_error("assign: missing 'value' parameter");
return;
}
//expand . to array structures
if (strpos($var,'.') !== false) {
$parts = split("\.",$var);
$varname = array_shift($parts);
$lastpart = array_pop($parts);
$array = array();
$previous = &$array;
foreach ($parts as $part) {
$previous[$part] = array();
$previous = &$previous[$part];
}
$previous[$lastpart] = $value;
$smarty->append($varname, $array, true);
} else {
$smarty->assign($var, $value);
}
}
?>[/php:1:e00571d5b7]
Hope it helps somebody |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Oct 06, 2003 12:20 pm Post subject: |
|
|
I think you will like this thread. |
|
Back to top |
|
apeiros Smarty n00b
Joined: 24 Apr 2003 Posts: 4
|
Posted: Mon Oct 06, 2003 12:34 pm Post subject: |
|
|
*mumble*
No, I don't like it. Not that I wouldn't like/appreciate his work but I hate to do work that's already been done - but well, my fault |
|
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
|