Ace
Ace

Reputation: 664

Massage Smarty array in PHP andsend it back to Smarty . . .?

How would you a) fix a Smarty array in PHP and b) stick the result back into the Smarty array variable?

Have you ever worked on a problem, and you thought you were SO close to resolving it, but the target keeps on slipping farther and farther away? I spent half a day on this, looking up documentation, searching for examples...but I must be missing something very basic.

Premise: I have a Smarty array with duplicate entries (and for now I cannot change the way this gets created, because the Smarty code is encoded in a template). This causes problems, because now I get multiple entries for the same product in the shopping cart. RIGHT NOW, I honestly cannot change the logic of how this was put together.

Here's what I got:

Smarty:
         {$products}

In my scenario, {$products} contains four entries like so:

array(1) { [0]=> array(12) { ["pid"]=> string(2) "13" ["domain"]=> NULL
["billingcycle"]=> string(4) "free" ["configoptions"]=> string(0) "" 
["customfields"]=> array(0) { } ["addons"]=> array(0) { } ["server"]=> string(0) ""
["productinfo"]=> array(9) { ["pid"]=> string(2) "13" ["gid"]=> string(1) "2" 
["type"]=> string(5) "other" ["groupname"]=> string(13) "Beta Products" ["name"]=>
string(44) "BetterStuff "Free Until 2014" Beta" ["description"]=> string(21) 
"BetterStuff installer" ["freedomain"]=> string(0) "" ["freedomainpaymentterms"]=> 
array(1) { [0]=> string(0) "" } ["freedomaintlds"]=> array(1) {[0]=> string(0) ""}}
["allowqty"]=> string(1) "0" ["qty"]=> int(1) ["pricing"]=> array(4) { ["baseprice"]=>
string(9) "$0.00 USD" ["setup"]=> string(9) "$0.00 USD" ["recurring"]=> array(0){}
["totaltoday"]=> string(9) "$0.00 USD" } ["pricingtext"]=> string(5) "FREE!" } }    

In PHP, I can easily use array-unique, to get rid of the 3 exact copies within this array, and be left with just one (as the one I just showed above).

{php}
    $var = $this->get_template_vars('products');
    $var = array_unique($var);
    $smarty = new Smarty();
    $smarty->assign('newproducts', $var = array_unique($var));
    var_dump($var);
{/php}

This works perfectly, in PHP, and the var_dump($var) contains one array item (just like the one I showed above). In PHP, when I check $var with is_array, the result is true.

Back in Smarty, however, {$newproducts} is NULL.

What am I doing wrong here? Do I need to explode or split my PHP array somehow, so that I can turn it into a Smarty variable?

And how can I "reset" the original {$products} array in Smarty to the new unique array value?

Anyone?

Upvotes: 0

Views: 932

Answers (2)

Sjiep
Sjiep

Reputation: 758

Since there is no real answer and I was stuck with the same problem, that is, not being able to call array_unique from the php side since I had no access to the code generating it.

Here is the code that worked for me in Smarty:

    {foreach from=$items item=item}
        {if !in_array($item, $array)} 
            <li>{$item}</li>
            {append var='array' value=$item}                       
        {/if}
    {/foreach} 

It makes use of the smarty append command, which will add items to an array. Furthermore I made use of in_array (scroll down to the bullet regarding php_functions) which is, among some other php functions available in Smarty.

I hope this might help someone else out!

Upvotes: 1

rodneyrehm
rodneyrehm

Reputation: 13557

{php}
    // read data from CURRENT INSTANCE
    $var = $this->get_template_vars('products');
    $var = array_unique($var);
    // create NEW INSTANCE
    $smarty = new Smarty();
    // assign to NEW INSTANCE
    $smarty->assign('newproducts', $var = array_unique($var));
    var_dump($var);
{/php}

so, you're creating a new smarty instance and assigning data to it. Is there a reason you believe said data would be available in the current instance?

try this:

{php}
    // read data from CURRENT INSTANCE
    $var = $this->get_template_vars('products');
    $var = array_unique($var);
    // assign to CURRENT INSTANCE
    $this->assign('newproducts', $var = array_unique($var));
{/php}

And {php} is deprecated. You might want to look into Extending Smarty With Plugins

Upvotes: 0

Related Questions