News Module only show Image with a alt tag.

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
andrewvideouk
Forum Members
Forum Members
Posts: 129
Joined: Thu Aug 20, 2020 10:49 pm

News Module only show Image with a alt tag.

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

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

Post 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...}
Not getting the answer you need? CMSMS support options
andrewvideouk
Forum Members
Forum Members
Posts: 129
Joined: Thu Aug 20, 2020 10:49 pm

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

Post by andrewvideouk »

Thank you. That is very usefull.
Post Reply

Return to “Tips and Tricks”