Setting dynamic crop on sorl.thumnail
-
07-07-2021 - |
سؤال
I have added a field on my model for specifying where the thumbnail should crop from, but I can't seem to add that to the template tag.
Here is how I do it now:
{% if entry.main_image.crop_value == "center" %}
{% thumbnail entry.main_image.image "350x100" crop="center" as im %}
<a href="{{ entry.get_absolute_url }}"><img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" alt=""></a>
{% endthumbnail %}
{% elif entry.main_image.crop_value == "top" %}
{% thumbnail entry.main_image.image "350x100" crop="top" as im %}
<a href="{{ entry.get_absolute_url }}"><img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" alt=""></a>
{% endthumbnail %}
{% elif entry.main_image.crop_value == "bottom" %}
{% thumbnail entry.main_image.image "350x100" crop="bottom" as im %}
<a href="{{ entry.get_absolute_url }}"><img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" alt=""></a>
{% endthumbnail %}
{% endif %}
This can't be the best way of doing it?
المحلول
You can use the variable in the template tag:
{% thumbnail entry.main_image.image "350x100" crop=entry.main_image.crop_value as im %}
لا تنتمي إلى StackOverflow