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 specific pages, understanding how to correctly callUserNameIt is all very important. AnQiCMS's powerful template engine provides various ways to achieve this goal, and we will delve into these methods in the following.

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

Display the username in the comment list

AnQiCMS provides a common requirement that the username of the comment publisher is displayed in the comment section of an article or product detail page.commentListTags, which can conveniently obtain detailed comment data, including the poster'sUserName.

To display a list of comments on a single page, we can usecommentListTag 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.{{item.UserName}},we can directly output the name of the reviewer of the comment. To enhance the user experience, we also added a judgment for the commentStatusunder review (Status != 1When [auto], it can display "Under review" and truncate the username to protect user privacy.truncatechars:6is a filter that truncates a string to a specified length and appends an ellipsis at the end.

Get and display the username of a 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 scenariosUserNameAnQiCMS providesuserDetailtags to achieve this purpose.

userDetailLabels allow us to identify users by theiridTo obtain the user's details, which also includesUserNamefield. 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" %}获取ID为1的用户的UserNameand assigned it touserProfilethe variable, and then we go through{{userProfile}}输出。如果需要同时获取并展示更多用户字段,例如用户的头像,也可以在userDetail标签中指定其他nameparameter, such asAvatarURL.

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

Display the contact name of the website

AnQiCMS also providescontactLabel, used to retrieve various contact information configured in the "Contact Settings" of the background. It also includes a contact namedUserNameThe field, which 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 enterprise or site in the footer of the website or on the "Contact Us" page, you can use the following code:

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

This will set the "Contact Information Settings" in the background联系人The value of the field is displayed directly on the page.UserNameIt is a different concept from the username of the registered user. Please distinguish it when using.

General skills and precautions

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

Summary

By the above several methods, we can flexibly display in AnQiCMS templateUserName:Show comments in the comment listitem.UserNameShow the commentor, throughuserDetailShow the specified registered user combined with the tag ID, and throughcontactLabel displays the contact name of the website. Understanding the usage of these labels can help you better build dynamic and personalized AnQiCMS websites.


Common Questions (FAQ)

Q1:How to directly display the current logged-in user's username in the template? A1:According to the current provided AnQiCMS document, there is no direct provision of a name calledCurrentUseror 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 to the template through the interface, and then useuserDetailLabel is displayed.

Q2:Why is the username sometimes{{ item.UserName }}, sometimes{% userDetail with name="UserName" id="1" %}, and what's the difference? A2:These two writing methods depend on the scenario in which you obtain the username.{{ item.UserName }}It is usually used in loops, for examplecommentListorarchiveListof the tagsforInside the loop,itemrepresents the current data object within the loop (such as a comment or a document), it already includesUserNamefields.{% userDetail with name="UserName" id="1" %}It is used when you need to retrieve independent information for a specific user (specified by its ID), and it does not depend on the loop context.In simple terms, the former is used for batch display, and the latter is used for precise query.

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,UserNameThe field usually returns an empty value or a default value (depending on the system settings and database storage). You can use it in the template.{% if item.UserName %}{{item.UserName}}{% else %}匿名用户{% endif %}Perform judgment and processing.For usernames containing special characters, 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).