# HG changeset patch # User Thierry Florac # Date 1531736061 -7200 # Node ID e6e53cb1785cfa25b73c3721300a86cec3e5ef6c # Parent 31c780a74151afcb2e13b4d4e9ebc5840d459a53 Added 'picture' TALES extension diff -r 31c780a74151 -r e6e53cb1785c src/pyams_file/image.py --- a/src/pyams_file/image.py Thu Jul 12 18:18:29 2018 +0200 +++ b/src/pyams_file/image.py Mon Jul 16 12:14:21 2018 +0200 @@ -9,6 +9,7 @@ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # +from pyramid.renderers import render __docformat__ = 'restructuredtext' @@ -21,10 +22,11 @@ # import interfaces from pyams_file.interfaces import IImage, IThumbnailer, IThumbnailGeometry, IThumbnails, IResponsiveImage +from pyams_utils.interfaces.tales import ITALESExtension # import packages -from pyams_utils.adapter import ContextAdapter, adapter_config -from zope.interface import implementer +from pyams_utils.adapter import adapter_config, ContextAdapter, ContextRequestViewAdapter +from zope.interface import implementer, Interface from zope.schema.fieldproperty import FieldProperty from pyams_file import _ @@ -245,3 +247,35 @@ label = _("Large screen thumbnail") weight = 13 + + +# +# Responsive 'picture' TALES extension +# + +@adapter_config(name='picture', + context=(Interface, Interface, Interface), + provides=ITALESExtension) +class PictureTALESExtension(ContextRequestViewAdapter): + """Picture TALES adapter + + This TALES adapter can be used to automatically create a 'picture' HTML tag + embedding all image attributes. + """ + + def render(self, context=None, lg_thumb='lg', lg_width=12, md_thumb='md', md_width=12, + sm_thumb='sm', sm_width=12, xs_thumb='xs', xs_width=12, css_class=''): + if context is None: + context = self.context + return render('templates/picture.pt', + {'image': context, + 'lg_thumb': lg_thumb, + 'lg_width': lg_width, + 'md_thumb': md_thumb, + 'md_width': md_width, + 'sm_thumb': sm_thumb, + 'sm_width': sm_width, + 'xs_thumb': xs_thumb, + 'xs_width': xs_width, + 'css_class': css_class}, + request=self.request) diff -r 31c780a74151 -r e6e53cb1785c src/pyams_file/templates/picture.pt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_file/templates/picture.pt Mon Jul 16 12:14:21 2018 +0200 @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + +