How to use `AvatarURL` or `FullAvatarURL` to display a user avatar in the template?

Calendar 👁️ 214

In website operation, the user avatar is not just a decoration, it carries the user's identity, and is an important part of community interaction and personalized experience.Whether it is in the comment area, author introduction, or member center, a clear user avatar can significantly enhance the website's亲和力 and professionalism.AnQiCMS as an efficient content management system naturally also provides us with the powerful function of flexibly displaying user avatars in templates.Today, let's talk about how to use in AnQiCMS templateAvatarURLandFullAvatarURLThese fields make it easy for the user's avatar to jump onto the screen.


The foundation for getting the user's avatar:userDetailTag

In AnQiCMS, the user avatar information is closely associated with specific user data. To obtain a user's avatar, we need to rely on the system's built-inuserDetailThe tag is specifically used to retrieve detailed information about a specified user, including their username, email, and of course, their avatar.

UseuserDetailThe most critical step when creating a tag is to specify the user.idFor example, if you want to get the avatar of a user with ID 1,id="1"is a necessary parameter. In practice, this user ID usually comes from the author ID of the current article (for example,archive.UserIdOr the ID of the comment author (for exampleitem.UserId), so we can dynamically retrieve the avatar information of different users.

AvatarURLwithFullAvatarURL: Choose the appropriate avatar path

When we successfully pass throughuserDetailAfter the label locates a user, it provides two main fields to help us display the avatar:AvatarURLandFullAvatarURLThey all point to the image address of the user avatar, but there is a subtle yet important difference:

  • AvatarURLIt usually returns arelative path. This means it may be similar to/uploads/avatars/user_123.jpgThis path is very convenient when used internally on your website, the system will automatically parse the complete URL based on your website root directory.Its advantage is that when the domain name of your website changes, these relative paths do not need to be modified.
  • FullAvatarURLIt means, it provides the avatar image.Complete absolute pathIt will include the protocol, domain, and complete file path, for examplehttps://您的域名/uploads/avatars/user_123.jpg. When you need to reference a user avatar on external platforms (such as social media sharing) or ensure the integrity of the URL in specific scenarios,FullAvatarURLit would be a safer choice.

In short, for the daily display within the website,AvatarURLit is sufficiently convenient; however, when it comes to cross-domain or external references,FullAvatarURLit can ensure the accuracy of the links.

Apply in the template: Display user avatar

Using these two fields to display the avatar in the template is very intuitive. We will use<img>label'ssrcAn attribute to carry the avatar URL. To avoid the page displaying a broken image when the user has not set an avatar, we usually combineiflogical judgment to display a default avatar.

Suppose we want to display the author's avatar on an article detail page:

{# 假设当前页面是文章详情页,并且可以通过archive.UserId获取作者ID #}
{% set authorId = archive.UserId %}

{# 获取作者头像的相对路径 #}
{% userDetail authorAvatarPath with name="AvatarURL" id=authorId %}

<div class="author-info">
    {% if authorAvatarPath %}
        <img src="{{ authorAvatarPath }}" alt="作者头像" class="author-avatar" />
    {% else %}
        {# 如果用户未设置头像,显示一个默认头像 #}
        <img src="/static/images/default-avatar.png" alt="默认头像" class="author-avatar" />
    {% endif %}
    <span class="author-name">{% userDetail authorName with name="UserName" id=authorId %}{{ authorName }}</span>
</div>

{# 如果您需要作者头像的完整绝对路径,可以这样做: #}
{% userDetail authorFullAvatarPath with name="FullAvatarURL" id=authorId %}
{% if authorFullAvatarPath %}
    <meta property="og:image" content="{{ authorFullAvatarPath }}" /> {# 例如用于社交媒体分享 #}
{% endif %}

In the above example, we first obtain the article author's ID byarchive.UserIdThen useuserDetailtags to retrieveAvatarURL.{% if authorAvatarPath %}Ensure that the user's uploaded avatar is displayed only when the avatar path exists, otherwise, it will gracefully degrade to display a preset default avatar.

Similarly, it is also simple to display user avatars in a loop in the list. For example, displaying the avatar of each comment author in a comment list:

`twig {# This is part of the comment list, item represents each comment #} {% commentList comments with archiveId=archive.Id type=“list” limit=“5” %}

Related articles

Can the `userDetail` tag retrieve the user's email `Email` and phone number `Phone` and display it on the front end?

In website operation and user experience design, sometimes we need to display specific user information on the front-end page, such as contact email or phone number, in order to provide personalized services or facilitate users to view/update their personal information.It is crucial for users of AnQiCMS to understand how to implement this safely and efficiently.This article will delve into the `userDetail` tag of AnQiCMS, discussing whether it can retrieve and display the user's email (`Email`) and phone number (`Phone`), as well as the matters that need to be paid attention to during use

2025-11-07

How to get the user's account balance `Balance` and cumulative earnings `TotalReward` through the `userDetail` tag?

How to retrieve and display a user's account balance and cumulative earnings in AnQi CMS template?For operators, it is a key element to enhance user experience and manage the membership system by flexibly displaying user-specific information on the website, such as their account balance or cumulative earnings.AnQi CMS is a powerful and practical content management system that provides a very convenient way to obtain these user data.Today, let's talk about how to use your template

2025-11-07

How to dynamically display exclusive content for distributors based on the user's `IsRetailer` status in AnQiCMS templates?

Today, providing customized experiences for users of different types has become the key to improving user satisfaction and business efficiency.For businesses with a distribution system, how to ensure that distributors can easily access their exclusive content without causing interference to ordinary users is a common need.AnQiCMS with its flexible template engine and powerful user management features, can help you easily achieve this goal.

2025-11-07

How to format `LastLogin` (last login time) into a readable date using the `stampToDate` tag?

In website operation and management, user behavior data is an important basis for improving user experience and formulating strategies.Among them, the "LastLogin" is a direct indicator of user activity.However, this data is usually stored in the form of raw timestamps, and for ordinary users, a string of numbers is far less friendly and understandable than a direct date and time.AnQi CMS provides powerful template tag functions, among which the `stampToDate` tag is a powerful tool to solve this problem, it can help us easily convert abstract timestamps into clear and readable date formats

2025-11-07

How to get the `GroupId` of the user through the `userDetail` tag and further get the detailed information of the user group?

In AnQiCMS, providing personalized content and experience to users is a key link to enhance website stickiness.It is particularly important to understand the details of the user's group to display exclusive content based on user level or provide differentiated service information.Today, let's delve into how to make use of the powerful template tag features of AnQiCMS, through the `userDetail` tag to obtain the `GroupId` of the user, and further use the `userGroupDetail` tag to display the detailed information of these user groups on the website front end

2025-11-07

How to display the personal introduction filled in the `Introduce` field on the user's profile page?

In website operation, providing users with personalized profile display, especially the personal introduction filled in by users themselves, can greatly enhance the vitality and sense of belonging of the community.For users of AnQiCMS, it is actually a very intuitive and flexible operation to display the personal introduction filled in by the user in the `Introduce` field on the profile page, mainly relying on the powerful template tag system of AnQiCMS.### Understand the template mechanism of AnQiCMS In AnQiCMS

2025-11-07

How to retrieve and display the user's exclusive `InviteCode` (invitation code) with the `userDetail` tag?

In AnQiCMS, if you want to display users' exclusive invitation codes on the website page, this usually involves the application of the membership system or referral mechanism.AnQiCMS provides flexible template tags that can help us easily obtain and display various user information, including invitation codes (`InviteCode`). To implement this feature, we mainly use the AnQiCMS `userDetail` tag.

2025-11-07

How to use the `stampToDate` tag for date formatting and conditional judgment for the `ExpireTime` (VIP expiration time) field?

In the daily operation of AnQi CMS, we often need to handle time-related fields, such as the VIP expiration time of users (`ExpireTime`).Correctly display this time information and make flexible conditional judgments based on its status, which is crucial for improving user experience and implementing personalized content display.This article will focus on the `ExpireTime` field, discussing in detail how to use the `stampToDate` tag for date formatting, and combine conditional judgment to implement intelligent management of VIP status.###

2025-11-07