How to rewrite the initial array and remove items by ID in the smarty view?

1

I'm using the View rendering library with Smarty .

In my view, I have an output with the array of product information inside a foreach:

{assign var='remove_products' [712, 716, 717, 718, 719, 720, 745, 755, 758]}
  {foreach from=$products item=product name=products}
    {if !in_array($product.id_product, $remove_products)}
        {$product.id_product|escape:'htmlall':'UTF-8'}
        {$product.name|escape:'htmlall':'UTF-8'}
        {$product.legend|escape:'htmlall':'UTF-8'}
        <!-- OBS: gostaria de salvar todos os dados acima
                em um novo array e usar a coleção novamente abaixo
        -->
    {/if}
  {/foreach]}  

 <!-- usando a coleção (nova) só que com os itens removidos -->

 {$products}     

How could I do this. Anybody know? I tried to do this but it did not roll:

   {assign var='remove_products' [712, 716, 717, 718, 719, 720, 745, 755, 758]}
    {assign var='n_products' []}
    {foreach $products as $key => $value}
        {if !in_array($value.id_product, $remove_products)}
            {$n_products[$key] = $value}   
        {/if}
    {/foreach}
    {$products = $n_products}
    
asked by anonymous 08.12.2015 / 19:42

2 answers

1

If you are really using smarty 2, there is no legal way to do this, you need to do what you should not, which is to use the php tag inside the template.

{php} 
    $this->assign("remove_products", array(712, 716, 717, 718, 719, 720, 745, 755, 758)); 
{/php}

Based on: How to assign an array within a smarty template file?

    
08.12.2015 / 20:35
0

I was able to solve the problem by doing this:

 {if isset($products)}
    {assign var='remove_products' [712, 716, 717, 718, 719, 720, 745, 755, 758]}
    {assign var='n_products' []}
    {foreach from=$products key=key item=product name=products}
       {if !in_array($product.id_product, $remove_products)}
           {$n_products[$key] = $product}
       {/if}
    {/foreach}
 {$products = $n_products}
{/if}
    
09.12.2015 / 16:31