0

I have a piece of code whereby I adjust the output of a file depending on it's file extension:

{section_1_files}
  {section_1_files:file}
    {if {extension} == 'jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF'}
      <figure>
        <img src="{url}" alt="{section_1_files:label}" />
      </figure>
    {if:else}
      <div class="file">
        <a href="{url}">{section_1_files:label}</a>
      </div>
    {/if}
  {/section_1_files:file}
{/section_1_files}

However it doesn't seem to be working properly. If I add {extension} below the {section_1_files:file} line then jpg is displayed on the front end. The following code currently outputs this:

  <div class="file">
    <a href="http://domain.co.uk/uploads/test-image.jpg">Test image</a>
  </div>

  <div class="file">
    <a href="http://domain.co.uk/uploads/test-file.docx">Test file</a>
  </div>

Any help would be greatly appreciated.

2

Just use the Contains and the Concatenation operators to check if the file extension appears on the list:

{section_1_files}
  {section_1_files:file}
    {if "|jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF|" *= "|" . extension . "|" }
      <figure>
        <img src="{url}" alt="{section_1_files:label}" />
      </figure>
    {if:else}
      <div class="file">
        <a href="{url}">{section_1_files:label}</a>
      </div>
    {/if}
  {/section_1_files:file}
{/section_1_files}

I don't have time now to check if the contains operator is case sensitive. If you could test it, please update this answer.

Why not to check for the {mime_type} using the begins operator?

{section_1_files}
  {section_1_files:file}
    {if mime_type ^= "image/" }
      <figure>
        <img src="{url}" alt="{section_1_files:label}" />
      </figure>
    {if:else}
      <div class="file">
        <a href="{url}">{section_1_files:label}</a>
      </div>
    {/if}
  {/section_1_files:file}
{/section_1_files}
| improve this answer | |
  • Both examples work perfectly. I've decided to go with the mime_type one as that looks a lot neater and will catch out anything I've missed (don't think I did though). Thank you very much for this. – Kieran McClung Sep 2 '14 at 8:13
2

change {if {extension} == 'jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF'} to

{if section_1_files:extension== 'jpg' || section_1_files:extension=='JPG' ...
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.