Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is array in smarty returns error

Tags:

php

smarty

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 }

like image 378
Karem Avatar asked Sep 16 '11 15:09

Karem


People also ask

Is array in Smarty?

Arrays With Smarty Templatingphp' file will create the arrays and the '. tpl' file will be used for presenting the arrays within HTML. The easiest way to show how this can be done is to show you a sample '.

How check array is empty or not in Smarty?

PHP function smarty_modifier_is_empty($input) { return empty($input); } ?> This will output Array is empty . Save this answer.


2 Answers

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 }
---------------------------^^^
like image 143
Michael Berkowski Avatar answered Oct 24 '22 07:10

Michael Berkowski


You can do it like:

{if $yourArray|is_array}
do something with it
{/if}
like image 29
Olli Avatar answered Oct 24 '22 07:10

Olli