I would like to render a random connected media item in Zotonic as follows:
{% with m.rsc[id].banner as media %}
{% if media %}
{% with media.random as m %}
{% media m %}
{% endwith %}
{% endif %}
{% endwith %}
How do you choose a media item at random from开发者_高级运维 a Page Connection in a Zotonic template?
Not all filters take all kinds of semi-lists. (ie. data structures that can act like a list but aren't, search results and resource lists are examples.)
You can force a value to be a list and then apply the random filter:
{% media m.rsc[id].banner|make_list|random %}
Also makes use of the fact that {% media %}
on an undefined value returns the empty list.
Arjan has a partial answer here: http://groups.google.com/group/zotonic-users/msg/021ac08702176745
However, the random
filter does not appear to work with my resource collections.
Ideally, using Arjan's solution, the template would be the beautifully concise:
{% with m.rsc[id].banner|random as m %}
{% if m %}
{% media m %}
{% endif %}
{% endwith %}
This looks like a bug in the random
filter, so I am going to debug it and post back here.
精彩评论