Page 1 of 1

News Module only show Image with a alt tag.

Posted: Sat Jan 01, 2022 9:58 pm
by andrewvideouk
This might be usefull if you want to show a image if got a alt text.

Add two Field Definition.

Image (file)
Alternate Text Image (Text Input)

In your news tempate add this.

Code: Select all

 {assign var='get_image' value='' }

{if isset($entry->fields)}
  {foreach from=$entry->fields item='field'}
  {if $field->type == 'file' && $field->value !=''  } 
               {capture assign='get_image' }{$entry->file_location}/{$field->value}{/capture}
               {$get_image = $get_image}  
    {/if} 
 {if $field->name == 'Alternate Text Image' }
 {assign var='alt' value=$field->value }
 {/if}
  {/foreach}
{/if}

{if $alt == '' || $get_image == "" }
{capture assign='get_image' }{root_url}/assets/images/newspaper.jpg{/capture}
          {SmartImage src=$get_image noembed=1 alt="Stock image of a newpaper for {$entry->title}" filter_resize='w,550' class="center"} 
{else}
 {SmartImage src=$get_image noembed=1 alt="{$alt}" }  
{/if}
Please fell free to change it a make it cleaner and post it here. I think there is a better way to do it. But its start. I am not sure how to use a variable with spaces in them. but this works.

Re: News Module only show Image with a alt tag.

Posted: Sun Jan 02, 2022 4:21 pm
by DIGI3
{capture} is quite inefficient and should really just be used to assign large blocks too complex for a regular assign. You can simply do:

Code: Select all

{$get_image = "{$entry->file_location}/{$field->value}"}
Alternately, you can save a fair bit of complexity by letting SmartImage do the work:

Code: Select all

{SmartImage src1=$entry->file_location src2=$field->value noembed=1 alt=$alt|default:"Stock image of a newspaper for {$entry->title}" filter_etc...}

Re: News Module only show Image with a alt tag.

Posted: Tue Jan 11, 2022 12:33 pm
by andrewvideouk
Thank you. That is very usefull.