Question

I wrote a basic Smarty modifier that emulates JavaScript's push.

function smarty_modifier_push($array, $push){
    if(!isset($array)){
        $array = array($push);
    }elseif(is_array($array)){
        $array[] = $push;
    }
    return $array;
}

Here is my code in my .tpl file

Problems: 
<select name="problems[]" multiple="multiple">
    <option value="">Select Problems</option>
    {foreach from=$problems key=id item=name}
        {$newArray|push:$id}
        <option value="{$id}">{$name}</option>
    {/foreach}
</select>
{$newArray}

Problem: My dropdown consists of about 20 elements so $newArray should have about 20 id's in it but when I check $newArray, it is empty. So it seems that it never gets assigned back to the template after the modifier runs.

I know that I can use {assign var=newArray} in my .tpl file beforehand but I'm hoping that there is a way of assigning new variables from within a modifier function. I also know it's possible from a function plugin because it gets passed the $smarty variable, but I just want to know specifically, How I can make this work using a modifier plugin?

Was it helpful?

Solution

There is some code posted on the SMARTY FORUM that might help you out. Other people have pointed out that it is a way for you to do what you need but its a little complicated and is not guaranteed to work when new updates come out. Hope it helps you out though. Cheers.

OTHER TIPS

Try to push "&" before $array variable like that

function smarty_modifier_push(& $array, $push){
    if(!isset($array)){
        $array = array($push);
    }elseif(is_array($array)){
        $array[] = $push;
    }
    return $array;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top