How to display the logged users username in Plone using TAL

How to display the logged in as link with the active users username or full name

When logged in to the default Plone theme, you can see your username in the top right-hand corner (Sunburst theme).

If this is hidden you can put the following code into a template file (anywhere you like really, main_template or somewhere in portal_view_customizations) which will display the username of the user which is logged in.

<tal:block tal:condition="not: context/portal_membership/isAnonymousUser">
(Logged in as: <span tal:replace="user/getId">(Username)</span>)
</tal:block>

Explanation:

The 'tal:condition' means this code will only show to logged in users, anonymous users see nothing at all.

Once logged in the 'tal:replace="user/getId"' puts the users' username in place of the span tag containing "(Username)" in this example.

The end result for a logged in user is (using dan, for example):

(Logged in as: dan)

Appearing somewhere on your website.

Full name

You can also display the full name of the user using the following code:

<tal:with-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:with-fullname>