Is array in smarty returns error

10,487

Solution 1

You need to remove the space before }. Smarty will not permit whitespace before a closing brace, or after an opening brace. I tested this in some of my own templates and could reproduce your error by placing a space before the closing brace.

{if is_array($dietcontent) }
-------------------------^^^

{if $rapportExists == false }
---------------------------^^^

Solution 2

You can do it like:

{if $yourArray|is_array}
do something with it
{/if}
Share:
10,487

Related videos on Youtube

Karem
Author by

Karem

Updated on June 04, 2022

Comments

  • Karem
    Karem almost 2 years
    Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "./templates/diet-report.tpl" on line 3 "{if is_array($dietcontent) }" - Unexpected " }"' in ...
    

    I did this:

        {if is_array($dietcontent) }
        There is something..
        {else}
        Noope...
        {/if}
    

    When i output {$dietcontent} i get "Array". But on pages where i dont get "Array" I wish to output a text.

    Why am i getting error?

    I even tried in my controller (this above is in template) :

    $data['rapportExists'] = is_array($data['dietcontent']) ? true: false;
    

    and then in my template:

    {if $rapportExists == false }
    noope
    {/if}
    

    Still receives the same error unexpected }

  • Michael Berkowski
    Michael Berkowski over 12 years
    @AlienWebguy Those are Smarty 2 docs. I just tested it in Smarty 3 and it fails with the error described.
  • Michael Berkowski
    Michael Berkowski over 12 years
    @AlienWebguy I suspect the two examples on that page which have spaces before the } are typos. Notice none of the other examples have spaces.
  • Karem
    Karem over 12 years
    @Michael wow, did not know that the space would generate error. It worked without, thanks! accepted