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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Sendlingur
Forum Members
Forum Members
Posts: 60
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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}
  
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1103
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

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

Post 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'}
Not getting the answer you need? CMSMS support options
Post Reply

Return to “CMSMS Core”