plonezpttal

tal:condition for file attachment fields


I am using this code for a simple download link (and only appears if the 'attachment' field has value - an attachment) - (works fine):

<metal:field use-macro="python:here.widget('attachment', mode='view')" />

I would also like to provide a method of directly viewing the file directly without downloading. I am currently using this code snippet (works fine):

<a tal:attributes="href string:${context/absolute_url}/attachment">View Attached File</a>

I want to add a condition that IF the 'attachment' field actually has an attachment, then display the "view" link. However, I am unable to get tap:condition working. In testing this I tried these code snippets:

<span tal:condition="here/attachment">ATTACHMENT</span>

<span tal:condition="context/attachment">ATTACHMENT</span>

<span tal:condition="template/attachment">ATTACHMENT</span>

<span tal:condition="exists:template/attachment">ATTACHMENT</span>

<span tal:condition="string:${context/absolute_url}/attachment">ATTACHMENT</span> 

<span tal:condition="python:here.widget('attachment', mode='view')">ATTACHMENT</span> 

<span tal:condition="exists:python:here.widget('attachment', mode='view')">ATTACHMENT</span> 

<span tal:define="file_exists2 exists:string:${context/absolute_url}/attachment" tal:condition="python:file_exists2">ATTACHMENT</span>

(the above displayed regardless if the attachment was present or not)

Is there a simple solution that will allow a tal:condition to accurately recognize if a file field has value and NOT display when no attachment is present and display when it is?


Solution

  • You can check the attachment size:

    If it's an Archetype (call getSize on the AT Field, it's also public traversable):

    <span tal:condition="context/attachment/getSize">ATTACHMENT</span>
    

    AFAIK on DX it's different, since it's a NamedBlobFile:

    <span tal:condition="context/attachment/get_size">ATTACHMENT</span>