4

I would like to do {unset($array['index'])} into a Smarty 3 template.

Is such a syntax (or similar) supported ? After Googling and doc reading I can't find something satisfying.

Maybe I should ask for a feature request to Smarty dev team ? :)

Anyway, how would you do this given the currently available template functions ?

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • Thank you all for your answers. Seems that the problem is indeed in the controller/view separation. But the way things were done in the original code led me "wanting to unset an array index in the template"... – Maxime Pacary Sep 19 '11 at 09:20

9 Answers9

5

There is a way though :-)

{$array=$array|array_diff_key:(['index']|array_flip)}

Even though it is not a good idea to do it in templates, sometimes it might save you time.

DUzun
  • 1,693
  • 17
  • 15
5

I don't think there's a direct support for this in smarty. You can always do this with smarty's {php} tag, however I would strongly discourage you from doing so. Logic doesn't belong in a presentation-level template.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 3
    If "logic doesn't belong in presentation" you don't need Smarty, just `strtr()` :-P However, in order to make this comment helpful, have you considered that you might not need to do what you suggest because you could just loop the thing and use an `{if}` and `{continue}` to skip the key you don't need? As in the [example 7.42](http://www.smarty.net/docs/en/language.function.foreach.tpl) – artfulrobot Jul 08 '13 at 12:46
  • @artfulrobot Using `{if}` and `{continue}` is fundamentally different, because they do not modify the data. Data modification does not belong in the presentation layer. – Aleks G Jul 08 '13 at 12:48
  • 3
    @AleksG it's quite reasonable to want to 'modify' data for presentation, which could reasonably require a new variable, e.g. a sorted view of a subset of the data. Perhaps here the questionner thought that rather than duplicate template input data in a 2nd variable, it might be more efficient to modify the input data, which might be acceptable if no further presentation is required later in the template. To turn your argument around: you would not want presentation logic in the controller. – artfulrobot Jul 09 '13 at 09:29
2

Two steps:

{$array.index = null}
{$array = $array|array_filter}

It work in Smarty3.

Example with a dynamic index:

{foreach $array as $item}
    {if $item.foo == 'bar'}
        <h1>{$item.text nofilter}</h1>
        {* unset item from array *}
        {$array[$item@key] = null}
        {$array = $array|array_filter}
        {break}
    {/if}
{/foreach}

{if $array}
    <ul>
    {foreach $array as $item}
        <li>{$item.text nofilter}</li>
    {/foreach}
    </ul>
{/if}
Aperturer
  • 21
  • 2
2

try this

{$array.index = null}
Undefined
  • 11,234
  • 5
  • 37
  • 62
Roman Rhrn Nesterov
  • 3,538
  • 1
  • 28
  • 16
  • Thanks for the idea, but unfortunately it doesn't do exactly what I want: starting with `Array ( [a] => 1 [b] => 2 )`, after doing `{$array.a = null}`, I get `Array ( [a] => [b] => 2 )` instead of `Array ( [b] => 2 )` – Maxime Pacary Feb 27 '12 at 14:00
1

The main idea behind a template engine is that you can do all the loading, logic, unsetting etc. before you parse the view. With that being said you shouldn't be unsetting data in your template, and I'm pretty sure they will not implement that feature request.

I also don't get it why you'd want to unset a smarty variable: just don't use it and it won't get displayed.

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
0
{$array=$links.lists|array_diff_key:(['10']|array_flip)}
{$array|print_r}

Here 10 is an array index.

iknow
  • 8,358
  • 12
  • 41
  • 68
0

I think, that you shouldn't want this, 'cause all logic must be in code not in templates.

But you can write your own modifier http://www.smarty.net/docs/en/plugins.modifiers.tpl

Nick Pavluk
  • 379
  • 2
  • 7
0

you don't you overwrite the value?

{assign var="array" value=array()}
{$array['index']='1'}
{$array['index2']='2'}
{$array['index']=''}
{$array|print_r}

this worked for me in smarty <3, don't know if still works as they really messed up smarty.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
-1

try

{assign var=$array.index value=null)}

Sabbir
  • 1,374
  • 1
  • 15
  • 23