How to get an image of Dexterity content types and show as a package list?

I need to create a batch list view of a folder with Dexterity based content types (Plone 4.2) that also display their image. I found two ways to get an image of objects.

Method 1:

in the template (within the batch)

         ...
         <div tal:define="item_object item/getObject;">
              <img tal:condition="exists:item_object/@@images/image1"
                   tal:replace="structure item_object/@@images/image1/mini" />
         </div>

      

Method 2:

in view class

def get_item_image(self, item):
    itemobj = item.getObject()
    scales = getMultiAdapter((itemobj, self.request), name='images')
    scale = scales.scale('image1', scale='mini')
    imageTag = None
    if scale is not None:
       imageTag = scale.tag()
    return imageTag

      

and in the template

         ...
         <div tal:define="item_image python:view.get_item_image(item)">
              <img tal:condition="item_image"
                   tal:replace="structure item_image" />
         </div>

      

Can anyone advise me which way is the best (if any) to avoid waking up?

+3


source to share


2 answers


I think the current one plone.app.imaging

has to wake up the object anyway in order to access its image scales (they are stored inside the object after all). So creating a list of objects without waking up is not that easy.

The good news is that Dexterity content types are lighter than Archetypes content types, and it folder.contentItems()

shouldn't be that expensive to make the initial list iterate over more in your template. In fact, it might even be faster than requesting a catalog. So don't worry about waking up objects and just use the method plone.app.imaging

:



https://github.com/plone/plone.app.imaging

+2


source


You really need to wake up the object and get the field from the schema object.



<tal:def tal:define="item_object python:item.getObject()">
    <tal:if tal:condition="python:hasattr(item_object, 'Schema')">
        <tal:d2 tal:define="image python:item_object.Schema().getField('image');
                        image python:image and image.getAccessor(item_object)();"
            tal:condition="image">
            <img id="image-rubrique" alt="" tal:attributes="src string:${item_object/absolute_url}/image_mini" />
        </tal:d2>
    </tal:if>
</tal:def>

      

+1


source







All Articles