How to correctly display the user's `UserName` (username) in the AnQiCMS template?

Calendar 👁️ 71

In AnQiCMS templates, flexibly displaying user information is a key factor in enhancing website personalization and user experience. Whether it is to display the nickname of the comment publisher or to show the detailed information of registered users on a specific page, understanding how to correctly callUserNameAll are very important. AnQiCMS's powerful template engine provides multiple ways to achieve this goal, and we will delve into these methods next.

AnQiCMS's template system adopts a syntax similar to Django's template engine, which allows developers to build pages quickly in a concise and efficient manner. In the template files, we mainly call data through two methods: using double curly braces{{变量}}Output the value of the variable, as well as using curly braces and the percent sign.{% 标签 %}To perform logical operations, such as loops, conditional judgments, or calling specific function tags.

Display the username in the comment list.

A common requirement is to display the username of the comment author in the comments section of an article or product details page. AnQiCMS provides for thiscommentListTags, which can conveniently obtain detailed comment data, including the poster'sUserName.

To display a comment list on a page, we can usecommentListLabels to loop through all comments. For example, on a document detail page, you might write template code like this:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      <div>
        <span>
          {# 根据评论状态判断是否显示用户名,或者显示为审核中 #}
          {% if item.Status != 1 %}
          审核中:{{item.UserName|truncatechars:6}}
          {% else %}
          {{item.UserName}}
          {% endif %}
        </span>
        {# 如果是回复评论,可以显示被回复人的用户名 #}
        {% if item.Parent %}
        <span>回复</span>
        <span>
          {% if item.Parent.Status != 1 %}
          审核中:{{item.Parent.UserName|truncatechars:6}}
          {% else %}
          {{item.Parent.UserName}}
          {% endif %}
        </span>
        {% endif %}
        <span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
      </div>
      <div>
        {# 显示评论内容,注意安全输出 #}
        {{item.Content|safe}}
      </div>
    </div>
    {% endfor %}
{% endcommentList %}

In the above example,{% for item in comments %}in the loopitemRepresents each comment object. Through{{item.UserName}}We can directly output the name of the poster of the comment. To enhance user experience, we also added a judgment for the commentsStatuswhen the comment is under review(Status != 1When, the status can be displayed as "Under Review" and the username can be truncated to protect privacy.truncatechars:6It is a filter that truncates a string to a specified length and adds an ellipsis at the end.

Get and display the username of the specified user.

In addition to the comment list, sometimes we may need to display a registered user based on a known user ID in specific scenariosUserName. AnQiCMS providesuserDetailto achieve this purpose.

userDetailTags allow us to identify users by theiridto retrieve the user's details, which also includes theUserNamefield. Suppose you want to display the username of the user with ID1:

<div>
    {# 获取ID为1的用户的UserName #}
    注册用户昵称:{% userDetail userProfile with name="UserName" id="1" %}{{userProfile}}
</div>

In this example,{% userDetail userProfile with name="UserName" id="1" %}Will retrieve the user with ID 1'sUserName, and assigned it touserProfilevariable, then we use{{userProfile}}to output. If you need to retrieve and display more user fields, such as the user's avatar, you can also inuserDetailSpecify other tagsnameparameters such asAvatarURL.

It should be noted that usinguserDetailThe label requires that you know the target user's ID. For website visitors or the currently logged-in user, if the system does not directly provide a tag to retrieve the current username, you may need to use the backend interface to obtain the current user's ID and then display it using this tag.

Display the contact name of the website

AnQiCMS also providescontactThe label is used to obtain various contact information configured in the "Contact Settings" of the background. It also includes a name calledUserNameThe field is usually used to represent the name of the contact person for a website or company, rather than a specific registered user.

If you want to display the contact name of the company or site in the website footer or 'Contact Us' page, you can use the following code:

<div>
    我们的联系人:{% contact with name="UserName" %}
</div>

This will place in the 'Contact Information Settings' in the background联系人The value of the field is directly displayed on the page. ThisUserNameIs a concept different from the username of the registered user, please distinguish when using it

Universal skills and precautions

  • Safe output:When displaying user-generated content (such as comments)item.Contentit is recommended to use|safea filter such as{{item.Content|safe}}To ensure that the HTML code is parsed correctly and not displayed as plain text. But please make sure that the content source is reliable to prevent XSS attacks. For plain text ofUserNameThis is usually not necessary|safe.
  • Conditional judgment:In many cases, we may need to decide whether to display the username or different content based on the existence of the data. At this point,{% if ... %}and{% else %}Labels will be very useful. For example, if the username is empty, it can display 'Anonymous user'.
  • Debug variable: If you are unsure in the template whether a variable contains the data you want, you can use|dumpa filter to output the complete structure and value of the variable, which is very helpful for debugging, for example{{ item|dump }}.

Summary

By using the above methods, we can flexibly display in the AnQiCMS templateUserName: In the comment listitem.UserNameDisplay the commentor, throughuserDetailLabel combined with user ID to display the specified registered user, as well as throughcontactThe label displays the contact name of the website. Understanding the usage of these labels can help you better build dynamic and personalized AnQiCMS websites.


Frequently Asked Questions (FAQ)

Q1: How to display the current logged-in user's username directly in the template? A1: According to the current provided AnQiCMS document, there is no direct provision of a namedCurrentUserOr similar tags to get the current logged-in user'sUserName.userDetailThe label needs a known user ID to retrieve information. If you have this need, you may need to combine backend development, pass the current logged-in user ID through the interface to the template, and then utilizeuserDetailThe tag is displayed.

Q2: Why is the username sometimes{{ item.UserName }}, sometimes it is{% userDetail with name="UserName" id="1" %}What is the difference? A2: These two ways of writing depend on the scenario in which you obtain the username.{{ item.UserName }}usually used in loops, for examplecommentListorarchiveListsuch as tags,forwithin the loop,itemrepresents the current data object being looped over (such as a comment or a document), it already containsUserNamefield. And{% userDetail with name="UserName" id="1" %}It is used when you need to get independent information for a specific user (specified by its ID) without depending on the loop context.In simple terms, the former is used for batch display, and the latter is used for precise queries.

Q3: If the user has not set a username or the username contains special characters, how will the template display? A3: If the user has not set a username,UserNameFields usually return empty values or default values (depending on the system settings and database storage). You can use them in the template.{% if item.UserName %}{{item.UserName}}{% else %}匿名用户{% endif %}Perform judgment and processing. For usernames containing special characters, the AnQiCMS template engine will default to HTML entity escaping to prevent security issues. Usually, no additional processing is required unless these special characters are intended to be parsed as HTML by the browser (but this is not recommended for ordinary usernames).

Related articles

How to use the `userDetail` tag to obtain and display the ID information of a specified user?

AnQiCMS with its flexible and powerful template tag system makes the display of website content very convenient.Whether it is articles, products, or user information, they can be easily displayed on the page with simple tags.Today, let's talk about how to use the `userDetail` tag to accurately obtain and display the ID information of a specified user.

2025-11-07

How to display the link to the previous and next article on the AnQiCMS article detail page?

To add links to the previous and next articles on the article detail page in AnQi CMS is a very practical feature for enhancing user reading experience and the internal link structure of the website.AnQiCMS's powerful template tag system makes this feature intuitive and efficient.

2025-11-07

What are the commonly used fields that can be called with the `archiveDetail` tag on the article detail page?

AnQiCMS (AnQiCMS) provides great convenience for website operators with its flexibility and powerful content management capabilities.When we carefully craft an article or product page and hope to accurately display its colorful content to visitors, the `archiveDetail` tag is an indispensable tool at our disposal.It is like a treasure trove of information, allowing us to easily call up all related data of the current content on the article detail page.

2025-11-07

How to retrieve the list of articles under a specified category and implement pagination in AnQiCMS?

In website content operation, we often need to display articles under specific categories and provide pagination for these lists to enhance user experience and website loading efficiency.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag system can easily meet this requirement. AnQiCMS template system adopts syntax similar to Django template engine, using double curly braces `{{variable}}` to reference variables, as well as single curly braces with a percent sign `{% tag %}` to call tags.This makes the customization of content display very intuitive

2025-11-07

How to use the `userDetail` tag to retrieve the user's real name `RealName` on the frontend page?

In website operations, personalization and user experience are the key to enhancing brand image.Sometimes, we are not satisfied with just displaying the user's nickname or avatar. In certain scenarios, such as the member center, profile page, or some activity leaderboard, we may also need to display the user's real name.AnQi CMS is a highly flexible and feature-rich Go language content management system that provides intuitive template tags, allowing front-end developers to easily retrieve and display this information.Today, let's delve into how to use the `userDetail` tag of Anqi CMS

2025-11-07

The `userDetail` tag supports which parameters to accurately find specific user information?

In AnqiCMS template development, to display specific user information on the page, we often use a very practical tag - `userDetail`.This tag allows us to accurately retrieve and present detailed user data based on specific conditions.Understanding the parameters it supports is crucial for flexibly building user-related page functions.The core function of the `userDetail` tag is to obtain data based on a clear user identifier.When in use, it requires us to specify the user ID to be queried through the `id` parameter

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 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