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