Forum Discussion

KPNOnline's avatar
KPNOnline
Mentor
10 years ago

Check if asset exists

Is there a way to check if an asset exists, in freemarker?

 

So without javascript (wait if an 404 is triggered).

 

Thanks

  • You can use the following trick:

     

    <#assign assetExistsPath = asset.get('/html/assets/ico_publish.png')/>
    <#assign assetDoesNotExistsPath = asset.get('/html/assets/ico_publish-DOES-NOT-EXIST.png')/>
    <p>assetExistsPath: ${assetExistsPath}</p>
    <p>assetDoesNotExistsPath: ${assetDoesNotExistsPath}</p>

     The output of this code is:

    assetExistsPath: http://<community URL>/html/assets/ico_publish.png?AE78400E7F6823769365D7E542C262A2
    
    assetDoesNotExistsPath: http://<community URL>/html/assets/ico_publish-error.png

     You can now build a condition based on whether the checksum is present or not. It will be present when the asset exists and it will not be present if it does not. So you can test assetPath?contains("?") or you can build a function like this

     

    <#function assetExists assetName>
     <#local fileEnding = assetName?split(".")?last />
     <#local assetPath = asset.get(assetName) />
     <#if assetPath?ends_with(fileEnding)>
       <#return false />
     </#if>  
     <#return true /> 
    </#function>  
    
    <#assign assetExistsTest = assetExists('/html/assets/ico_publish.png')/>
    <#assign assetDoesNotExistsTest = assetExists('/html/assets/ico_publish-error.png')/>
    <p>assetExistsTest: ${assetExistsTest?string('yes', 'no')}</p>
    <p>assetDoesNotExistsTest: ${assetDoesNotExistsTest?string('yes', 'no')}</p>

     

2 Replies

  • RafaelC's avatar
    RafaelC
    Khoros Alumni (Retired)
    10 years ago

    You can use the following trick:

     

    <#assign assetExistsPath = asset.get('/html/assets/ico_publish.png')/>
    <#assign assetDoesNotExistsPath = asset.get('/html/assets/ico_publish-DOES-NOT-EXIST.png')/>
    <p>assetExistsPath: ${assetExistsPath}</p>
    <p>assetDoesNotExistsPath: ${assetDoesNotExistsPath}</p>

     The output of this code is:

    assetExistsPath: http://<community URL>/html/assets/ico_publish.png?AE78400E7F6823769365D7E542C262A2
    
    assetDoesNotExistsPath: http://<community URL>/html/assets/ico_publish-error.png

     You can now build a condition based on whether the checksum is present or not. It will be present when the asset exists and it will not be present if it does not. So you can test assetPath?contains("?") or you can build a function like this

     

    <#function assetExists assetName>
     <#local fileEnding = assetName?split(".")?last />
     <#local assetPath = asset.get(assetName) />
     <#if assetPath?ends_with(fileEnding)>
       <#return false />
     </#if>  
     <#return true /> 
    </#function>  
    
    <#assign assetExistsTest = assetExists('/html/assets/ico_publish.png')/>
    <#assign assetDoesNotExistsTest = assetExists('/html/assets/ico_publish-error.png')/>
    <p>assetExistsTest: ${assetExistsTest?string('yes', 'no')}</p>
    <p>assetDoesNotExistsTest: ${assetDoesNotExistsTest?string('yes', 'no')}</p>

     

  • KPNOnline's avatar
    KPNOnline
    Mentor
    10 years ago
    Very nice!! Thank you for writing an example for us.

    Kudos for you!