Page 1 of 1

troubles with {if}]{elseif}{else}

Posted: Wed May 22, 2019 3:32 pm
by Sendlingur
I've been trying to make this if statement work as I want it to:

it is a part of a bigger loop that iterates through a bunch of articles.

If $entry->fields is empty it should get an image from {$entry->file_location}

If $entry->fields is still empty after that the code should look for image in the content.

and if there is no image still showing it should display the <img src="uploads/images/img-01.jpg" class="img-responsive"/>

The problem is when the code looks like it does below the "<img src="uploads/images/img-01.jpg" class="img-responsive"/>"

is the only image being displayed.

but if I use " !empty " for the {if} and {elseif} the "<img src="uploads/images/img-01.jpg" class="img-responsive"/>" in the {else} is never displayed

can someone help me out here?

Code: Select all

{if empty($entry->fields)}       
<figure>
  {foreach from=$entry->fields item='field'}
        {if $field->type == 'file'}
          {if isset($field->value) && $field->value}
           <img src="{$entry->file_location}/{$field->value}" class="img-responsive"/>
          {/if}
        {/if}
  {/foreach}
</figure>
  
{elseif empty($entry->fields)}
<figure>
  {cgsi_getimages assign='imageinfo' nocontent=1}{$entry->content}{/cgsi_getimages}
       {foreach from=$imageinfo item=image name=pic}
 {if $smarty.foreach.pic.first}<img src="{$image.src}" class="img-responsive"/>{/if}
{/foreach}  
</figure>
{else}
<figure>
<img src="uploads/images/img-01.jpg" class="img-responsive"/>  

</figure>
{/if}
  

Re: troubles with {if}]{elseif}{else}

Posted: Wed May 22, 2019 4:06 pm
by DIGI3
$entry->fields is probably never empty, you'll want to verify that first. You'll probably need to test the contents of the specific field, not just that extra fields exist.

In the sample you pasted, the first {if} and the {elseif} are testing the same thing, so the elseif will never be true.

Another thing you may want to look at to simplify your coden a bit is the Smarty |default modifier. You can just call the field and set a default if it's empty.
e.g. {$myimage|default:'path/image.jpg'}