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

Hacked function.assign.php to allow array appends

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
apeiros
Smarty n00b


Joined: 24 Apr 2003
Posts: 4

PostPosted: Mon Oct 06, 2003 12:11 pm    Post subject: Hacked function.assign.php to allow array appends Reply with quote

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


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

PostPosted: Mon Oct 06, 2003 12:20 pm    Post subject: Reply with quote

I think you will like this thread.
Back to top
View user's profile Send private message
apeiros
Smarty n00b


Joined: 24 Apr 2003
Posts: 4

PostPosted: Mon Oct 06, 2003 12:34 pm    Post subject: Reply with quote

boots wrote:
I think you will like this thread.


*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 Rolling EyesEmbarassed
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 -> Plugins 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