CMS Made Simple Forums
https://forum.cmsmadesimple.org/

troubles with {if}]{elseif}{else}
https://forum.cmsmadesimple.org/viewtopic.php?f=8&t=80917
Page 1 of 1

Author:  Sendlingur [ Wed May 22, 2019 3:32 pm ]
Post subject:  troubles with {if}]{elseif}{else}

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:
{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}
 

Author:  DIGI3 [ Wed May 22, 2019 4:06 pm ]
Post subject:  Re: troubles with {if}]{elseif}{else}

$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'}

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/