How to display user details such as avatar and username in AnQiCMS templates?

Calendar 👁️ 104

In AnQiCMS, flexibly displaying users' detailed information in templates, such as avatars and usernames, is an important step to achieve personalization and enhance website interaction.AnQiCMS's powerful template engine provides an intuitive and efficient way to integrate these data.

Understand the data calling mechanism of AnQiCMS templates

AnQiCMS's template system adopts a syntax style similar to Django, which makes template writing both intuitive and powerful.The core lies in using specific template tags to call back-end data.For displaying user information, we mainly rely onuserDetail.

userDetailThe tag is a built-in tag used to obtain and display user information. It allows you to retrieve various attributes of a specified user through the user ID and use it flexibly in the front-end template.

How to useuserDetailLabel to get user data

userDetailThe basic usage of the tag is as follows:

{% userDetail 变量名 with id="用户ID" %}

Here:

  • 变量名You can define a variable to store all the information obtained from the user, for exampleuserInfo. This is beneficial because you can directly access it in subsequent code.userInfo.字段名Access all user properties to avoid repeated calls to the tag.
  • id="用户ID"This is a required parameter, you need to provide a unique user ID.This ID usually comes from other data (such as the author ID of an article) or is automatically provided by the system on a specific user page (such as a personal center).

Display of core user information

Once you pass throughuserDetailThe tag gets user data and assigns it to a variable (for exampleuserInfo),You can access the various fields it contains:

  1. User Avatar (Avatar)The user's avatar is usually obtained throughAvatarURLorFullAvatarURLfield to retrieve.

    • AvatarURL: It is usually a processed (such as cropped, compressed) avatar thumbnail link, suitable for display in lists or small-sized areas.
    • FullAvatarURL: May provide the original or larger size avatar link, suitable for user profiles and other scenarios requiring high-definition display.

    Example:

    <img src="{{ userInfo.AvatarURL }}" alt="{{ userInfo.UserName }}的头像" class="user-avatar" />
    
  2. Username (Username)Username verifiedUserNameField retrieval, this is the most direct manifestation of user identity.

    Example:

    <span>{{ userInfo.UserName }}</span>
    
  3. User homepage link (Link)If your website provides an independent personal homepage or profile page for each user,LinkThe field provides the URL of the page.

    Example:

    <a href="{{ userInfo.Link }}" class="user-profile-link">{{ userInfo.UserName }}的主页</a>
    

Case study: Display author information on the article detail page.

Assuming you are on the article detail page of AnQiCMS and want to display the author's avatar, username, and last login time below the article content. The data for the article detail page can be accessed byarchiveDetailTag acquisition, which includes the author'sUserId.

”`twig {# 1. First, get the author ID of the current article #} {%- archiveDetail articleAuthorId with name=“UserId” %}

{%- if articleAuthorId %} {# 2. Use the author ID, fetch all author details in one go using the userDetail tag, and store them in the userInfo variable #} {% userDetail userInfo id=articleAuthorId %}

<h3 class="section-title">关于作者</h3>
<div class="author-details">
  {# 显示作者头像 #}
  {% if userInfo.AvatarURL %}
    <img src="{{ userInfo.AvatarURL }}" alt="{{ userInfo.UserName }}的头像" class="author-avatar" />
  {% else %}
    {# 如果没有头像,可以显示一个默认占位符图片 #}
    <img src="/public/static/images/default-avatar.png" alt="默认头像" class="author-avatar" />
  {% endif %}

  <div class="author-text-info">
    {# 显示作者用户名,并链接到其个人主页(如果 userInfo.Link 存在) #}
    <p class="author-name">
      作者:

Related articles

How to correctly display the link and title of the previous/next document in AnQiCMS template?

How to elegantly display the link and title of the previous/next document in the AnQiCMS template?When browsing articles on a website, users often want to easily navigate to related content, whether it's going back to the previous one for a deeper understanding or continuing to read the next new content.This "Previous/Next" navigation feature not only significantly improves user experience and extends the time users spend on the website, but also benefits the internal link structure and SEO optimization of the website.

2025-11-07

How to enable WebP image format in AnQiCMS to improve image loading speed and display efficiency?

Enable WebP image format in AnQiCMS: Optimize loading speed and display efficiency In today's digital age, website loading speed is one of the key factors in user experience and search engine ranking.Images are an important part of a website's content, and their loading efficiency has a crucial impact on the overall performance of the page.If the image size is too large or the format efficiency is low, it will not only slow down the website loading speed, but also consume the valuable traffic of users, and even affect the performance of the website in search engines.

2025-11-07

How does AnQiCMS ensure that the front-end displayed content is UTF-8 encoded and avoids garbled characters?

In website operation, content encoding is a crucial link, which directly affects whether users can read the text on the page normally.If encoding is not unified, the appearance of乱码 not only will seriously affect the user experience, but may also lead to information transmission errors.For AnQiCMS, ensuring that the content displayed on the front end is UTF-8 encoded to avoid garbled text issues is a core consideration in its system design and content management.First, Anqi CMS has provided a solid foundation for UTF-8 encoding in its underlying technical architecture.

2025-11-07

How to use filters (such as `truncatechars`) in AnQiCMS templates to truncate long text content and display it friendly?

In website operation, how to efficiently display content while ensuring the page is neat and beautiful is a challenge that every operator needs to face.Especially when the title, introduction, or comments of an article are too long, if they are not processed, it is easy to break the page layout and affect the user experience.Luckyly, AnQiCMS provides a powerful and flexible template filter function, among which the `truncatechars` filter and other truncation filters can help us solve this problem elegantly.Optimize Content Display: Why Do We Need to Truncate Text?

2025-11-07

How to set the website logo and filing number of AnQiCMS so that it can be displayed correctly on the front-end page?

During the process of building a website, the brand identity (Logo) and the necessary compliance information (record number) are indispensable elements.They can not only enhance the professionalism and credibility of the website, but also provide clear guidance to users.AnQiCMS is an efficient content management system that provides an intuitive and convenient way to set these information and ensures that they are displayed correctly on the website front-end page. Next, we will learn how to set the website logo and filing number in AnQiCMS in detail.

2025-11-07

How to automatically convert URLs to clickable links in AnQiCMS templates using the `urlize` filter?

In website content management, we often need to display external links in articles, comments, or introductions.Manually adding the `<a>` tag to each URL is not only cumbersome but also prone to errors, especially when dealing with large amounts of user-generated content or importing data from other sources.Fortunately, AnQiCMS provides a very practical template filter——`urlize`, which can automatically identify URLs and email addresses in text and convert them into clickable links, greatly simplifying the work of content managers.

2025-11-07

How to configure Docker reverse proxy to display multiple independent websites for AnQiCMS multi-site feature?

AnQiCMS (AnQiCMS) is favored by many website operators for its efficient and flexible content management capabilities.Especially its powerful multi-site management feature, combined with Docker containerization technology and reverse proxy, allows you to easily set up and manage multiple independent websites on the same server, greatly enhancing operational efficiency and reducing maintenance costs. ### Discover the charm of multiple sites Imagine that you may have several different brands, aimed at different audiences, or serving different regions, but their content update and management processes are roughly the same.

2025-11-07

How to manage custom fields in the content model of AnQiCMS and call them to display on the front-end page?

AnQi CMS is an efficient and customizable enterprise-level content management system, one of its core highlights is the 'flexible content model'.This feature greatly expands the management boundaries of website content, allowing us to create various and unique content structures according to actual business needs.Among these, custom fields (or custom parameters) play a crucial role, allowing our website content to go beyond traditional article titles, body, and summaries, and carry more personalized information, ultimately displaying flexibly on the front-end page.

2025-11-07