我试图在列表中显示每个新闻项的全名。目前我只有用户ID(昵称)。
是否有一种简单的方法(在现有的.pt文件中)显示创建者或所有者的全名而不是昵称?
该页面也必须适用于匿名用户。我的意思是 - 页面必须是公开的。
一些细节:
<div class="container-fluid news-list-container"
tal:define="news_items python:context.getFolderContents(contentFilter={'portal_type':['News Item'], 'sort_on': 'Date', 'sort_order': 'descending',});
Batch python:modules['Products.CMFPlone'].Batch;
b_size python:4;
b_start python:0;
b_start request/b_start | b_start;
batch python:Batch(news_items, b_size, int(b_start), orphan=0);"
tal:condition="news_items">
<div class="news-list-items">
<tal:items tal:repeat="news_item batch">
<!-- News item -->
<div class="row news-item"
tal:define="news_object python:news_item.getObject();
news_date python:news_object.getField('modification_date').getAccessor(news_object)();
news_title python:news_object.getField('title').getAccessor(news_object)();
news_description python:news_object.getField('description').getAccessor(news_object)();
news_image python:news_object.getField('image').getAccessor(news_object)();
news_url python:news_object.absolute_url();
news_creators python:news_object.getField('creators').getAccessor(news_object)(); .... ...
答案 0 :(得分:2)
<tal:fullname define="membership context/portal_membership;
info python:membership.getMemberInfo(user.getId());
fullname info/fullname">
You are are <span class="name" tal:content="fullname" />
</tal:fullname>
此示例取自plone documentation
答案 1 :(得分:2)
您可以通过以下代码获得很多灵感:
<tal:name tal:condition="item_creator"
tal:define="author python:context.portal_membership.getMemberInfo(item_creator)">
<span i18n:translate="label_by_author">Posted by
<a href="#"
title="Read more posts by this author"
tal:attributes="href string:${context/portal_url}/author/${item_creator}"
tal:content="python:author and author['fullname'] or item_creator"
tal:omit-tag="not:author"
i18n:domain="scrawl"
i18n:name="author"
i18n:attributes="title author_title">
Bob Dobalina
</a>
</span>
</tal:name>
注意可能的性能问题。
缓存的视图方法可以更好地工作,例如:
@memoize
def userid2fullname(self, userid):
pm = api.portal.get_tool('portal_membership')
memberinfo = pm.getMemberInfo(userid)
return memberinfo and memberinfo['fullname'] or userid