如何从 Zotonic 模板中的页面连接中随机选择媒体项目?
我想在 Zotonic 中渲染随机连接的媒体项目,如下所示:
{% with m.rsc[id].banner as media %}
{% if media %}
{% with media.random as m %}
{% media m %}
{% endwith %}
{% endif %}
{% endwith %}
如何从 Zotonic 模板中的页面连接中随机选择媒体项目?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并非所有过滤器都采用所有类型的半列表。 (即,数据结构可以像列表一样工作,但事实并非如此,搜索结果和资源列表就是示例。)
您可以强制一个值成为列表,然后应用随机过滤器:
还利用了以下事实:
{% media %}
对于未定义的值返回空列表。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:
Also makes use of the fact that
{% media %}
on an undefined value returns the empty list.Arjan 在这里有部分答案:
http://groups.google.com/group/zotonic-users/msg/021ac08702176745
但是,
random
过滤器似乎不适用于我的资源集合。理想情况下,使用 Arjan 的解决方案,模板将非常简洁:
这看起来像是随机过滤器中的错误,因此我将对其进行调试并发布回此处。
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:
This looks like a bug in the
random
filter, so I am going to debug it and post back here.