How to display detailed information of the currently logged-in user (such as username, avatar, VIP status)?

Calendar 👁️ 63

In AnQi CMS, displaying detailed information about the currently logged-in user, such as username, avatar, and VIP status, is a key step in enhancing user experience and building a personalized website.AnQi CMS with its flexible template engine allows us to easily implement these functions.This article will provide a detailed introduction on how to display this information on your website front-end.

Get the basic information of the currently logged-in user

The Anqi CMS template system providesuserDetailThe tag is the core tool to obtain various information of the currently logged-in user. This tag can directly obtain user data from the current session without manually passing the user ID.

We can use it like thisuserDetailLabel to get user's basic information:

{# 假设我们想获取当前用户的用户名和头像 #}
{% userDetail currentUser with name="UserName" %}
{% userDetail userAvatar with name="AvatarURL" %}

{% if currentUser %}
    <div class="user-info">
        <img src="{{ userAvatar }}" alt="{{ currentUser }}" class="user-avatar" />
        <span class="username">{{ currentUser }}</span>
    </div>
{% else %}
    <div class="user-info">
        <a href="/login">请登录</a>
    </div>
{% endif %}

In the code above:

  • {% userDetail currentUser with name="UserName" %}Assign the username of the currently logged-in user tocurrentUserVariable.
  • {% userDetail userAvatar with name="AvatarURL" %}Then get the user's avatar URL and assign it touserAvatarVariable.
  • We first pass through{% if currentUser %}Determine if the user is logged in. IfcurrentUserThe variable has a value, indicating that the user is logged in, and we can display their avatar and username.
  • AvatarURLThe field will provide the link to the user's avatar image. You can also useFullAvatarURLGet the complete avatar link (if your storage configuration is different).

Show the user's VIP status and expiration time

The AnQi CMS is built-in with user group management and VIP system, so we can combine the user's group information and VIP expiration time to display the VIP status.

First, we can start fromuserDetailthe tag to get the user'sGroupId(User's group ID) andExpireTime(VIP expiration time). Then, useuserGroupDetailLabel based onGroupIdTo obtain detailed information about user groups, such as the name of the user group (e.g., "VIP Member", "Senior Member").

{# 获取当前用户的所有详细信息,包括GroupId和ExpireTime #}
{% userDetail user with name="Id" %} {# 确认获取到用户ID以判断登录状态 #}
{% if user.Id %}
    {% userDetail userDetailData with name="UserName" %}
    {% userDetail userAvatar with name="AvatarURL" %}
    {% userDetail userGroupId with name="GroupId" %}
    {% userDetail vipExpireTime with name="ExpireTime" %}

    <div class="user-profile">
        <img src="{{ userAvatar }}" alt="{{ userDetailData }}" class="user-avatar" />
        <span class="username">{{ userDetailData }}</span>

        {% if userGroupId %}
            {# 根据GroupId获取用户组详情,例如用户组的标题(VIP等级名称) #}
            {% userGroupDetail userGroup with id=userGroupId %}
            {% if userGroup %}
                <span class="user-vip-status">
                    {% if userGroup.Title %}
                        {{ userGroup.Title }}
                    {% else %}
                        普通用户
                    {% endif %}
                </span>
                {# 格式化VIP过期时间,并判断是否过期 #}
                {% if vipExpireTime > 0 %}
                    {% set formattedExpireTime = stampToDate(vipExpireTime, "2006-01-02") %}
                    {% set currentTime = now "2006-01-02" | stampToDate %} {# 获取当前日期并转换为时间戳 #}
                    {% if vipExpireTime > currentTime %}
                        <span class="vip-expiration">(有效期至:{{ formattedExpireTime }})</span>
                    {% else %}
                        <span class="vip-expired">(VIP已过期)</span>
                    {% endif %}
                {% endif %}
            {% endif %}
        {% else %}
            <span class="user-vip-status">普通用户</span>
        {% endif %}
    </div>
{% else %}
    <div class="user-profile">
        <a href="/login">您尚未登录,请点击登录</a>
    </div>
{% endif %}

In this code, we:

  • First use{% userDetail user with name="Id" %}Get the current logged-in user's ID and use it to determine if the user is logged in.
  • If the user is logged in, we will continue to retrieve the username, avatar URL, user group ID, and VIP expiration time.
  • {% userGroupDetail userGroup with id=userGroupId %}Tags are used to classify according touserGroupIdGet the detailed information of the corresponding user group and assign it touserGroupVariable.
  • userGroup.TitleIt will display the name of the user group, such as "VIP member".
  • vipExpireTimeIt is a timestamp, we use{{ stampToDate(vipExpireTime, "2006-01-02") }}The filter formats it into a readable date.
  • To determine if the VIP is expired, we also need to get the current timestamp and compare it withvipExpireTimeto compare. IfvipExpireTimeIf greater than the current time, display the validity period; otherwise, display expired.

By combining these tags, you can flexibly and clearly display various details of the currently logged-in user at any location on the website, such as the top navigation bar, sidebar, or personal center page, greatly enhancing the user experience.The powerful and easy-to-use nature of Anqi CMS makes these seemingly complex personalized features accessible.


Frequently Asked Questions (FAQ)

  1. Question: How to determine if the user is logged in?Answer: In the AnQi CMS template, you can determine if a user is logged in by checking if a certain field exists in the user object. For example, you can use{% userDetail user with name="Id" %}to get the user ID, then use{% if user.Id %}or{% if userDetailData %}(If the username has been assigned touserDetailData) to determine if the user is logged in. If the user is not logged in, these fields are usually empty or zero.

  2. How do I fix the issue where the user's avatar does not display or shows a default avatar?If the user's avatar does not display, please first checkAvatarURLwhether the field has a value. You can set it in the template.<img>Label a backup default avatar image link to ensure that the page can display normally even if the user has not uploaded an avatar. For example:<img src="{{ userAvatar if userAvatar else '/static/images/default-avatar.png' }}" alt="{{ currentUser }}" class="user-avatar" />Make sure that the avatar has been uploaded in the background settings and the image path is correct and accessible.

  3. Ask: Why does the VIP status display 'General User' instead of the specific level (such as 'Gold Member')?The answer is usually due to the currently logged-in user'sGroupIdNot correctly associated with the corresponding VIP user group oruserGroupDetailLabel could not be retrieved

Related articles

How to configure and display thumbnails with different processing methods (such as cropping, scaling)?

##驾驭安企CMS缩略图:精细化的配置与灵活的展示,打造视觉冲击力 在网站运营中,图片无疑是吸引用户、提升内容质量的关键元素。While thumbnails, as the first visual touchpoint of website content, their display effect directly affects users' desire to click and the overall beauty of the page.AnQi CMS knows this and provides us with flexible thumbnail configuration and display capabilities, helping us easily manage and present high-quality image content.### Core Configuration

2025-11-08

How to automatically compress images based on size and display them on the front-end?

In website operation, images are an important element to attract visitors and convey information.However, large, unoptimized images often become the 'killer' of website loading speed, not only affecting user experience, but also possibly dragging down search engine rankings.Luckyly, AnQiCMS (AnQiCMS) provides very intelligent and practical image processing functions that can help us easily cope with these challenges, making images clear and fast when displayed on the front end.### Smart compression, goodbye to manual adjustment of large images Have you ever encountered such a situation: in order to maintain image quality

2025-11-08

How to batch generate and display images in Webp format in AnQi CMS?

Website performance is one of the key factors that determine user experience and search engine rankings.Among various optimization methods, image optimization plays a vital role.WebP is a modern image format that boasts excellent compression performance and almost lossless visual quality, and is gradually becoming the new standard for website optimization.How can we efficiently generate and display WebP images in the Anqi CMS management website?AnQi CMS provides very convenient and powerful functional support in this aspect, allowing you to easily achieve the WebPization of website images without delving into technical details

2025-11-08

How to truncate long text content and display an ellipsis?

In the operation of daily websites, we often encounter such situations: the content of the article detail page is rich, but the list page or recommendation module needs to display a concise version of the content for the sake of page layout, usually by truncating a part of the text and adding an ellipsis at the end.AnQi CMS understands the importance of content display and provides very convenient and powerful tools to handle such needs.Let's take a look at how to elegantly implement the truncation of long text content and display an ellipsis in Anqi CMS, making the website interface both beautiful and professional.The need and value of content truncation in many display scenarios

2025-11-08

How to display the names and introduction information of different user groups?

Displaying different user group names and introduction information in AnQi CMS is an important part of website personalized operation and content distribution.Whether you want to display exclusive identification for VIP members or provide customized content for users with different permissions, Anqi CMS's flexible template tag system can help you easily achieve this. ### Understanding the Core Concepts of User Group Information To display the name and introduction of the user group, it is first necessary to clarify two key points: whether the user is logged in, and which user group the user belongs to. Safe CMS provides powerful template tags

2025-11-08

How to conditionally display different content or styles within a loop?

In website content display, we often encounter the need to display different content or assign unique styles to elements according to the order of data cycles, specific attributes, or states.For example, odd and even rows in the list need different background colors, special markings for certain types of products, or when a field is empty, hide the corresponding area.With its flexible and powerful Django-like template engine, AnQi CMS makes these requirements simple and easy to implement.By cleverly combining loop tags (`{% for ... %}`) and conditional judgment tags (`{% if ... %}`)

2025-11-08

How to define and display temporary variables in the template?

In Anqi CMS template development, mastering how to define and display temporary variables in the template is a very practical skill.It can help us handle data more flexibly, optimize template structures, and write more concise and efficient code.Temporary variables are like the "small tags" you grab on the fly while making a page, used to temporarily store some data for later use.### Why do we need temporary variables? Imagine a scenario: you might need to get a value from a label and then perform a series of operations on it (such as truncating strings, formatting time)

2025-11-08

How to reference and display common code snippets (such as header, footer) in AnQiCMS templates?

It is crucial to maintain consistency in page structure and code maintainability in website construction and content management.For a content management system like AnQiCMS, effectively referencing and managing common code snippets, such as the website header (Header) and footer (Footer), is the key to achieving this goal.Through AnQiCMS's flexible template mechanism, even users with little understanding of technical details can easily implement these features, thus building a website with clear structure and easy maintenance.###

2025-11-08