How to flexibly call and display the value of the 'Content Model Custom Field' in the AnQiCMS frontend page?

Calendar 👁️ 93

The Anqi CMS excels in content management, especially its flexible content model and custom field functions, which provide great convenience for building personalized websites.The content of a website is often not just the title and text, we may also need various additional information such as the author of the article, the source of publication, product specifications, event location, and floor area.These custom fields are like a 'personal file folder' for content, allowing us to tailor the required attributes for each content type according to business needs.

How can we cleverly call and display the values of these rich custom fields after creating them in the background for the content model on the website front-end page?This is the core of what we are discussing in this article.

Custom field: Make your content management more flexible

First, let's briefly review the custom fields of Anqi CMS.In the background, go to the "Content Management" under the "Content Model", and you will see that the system defaults to provide article and product models.Of course, you can also add a completely new content model according to your business needs.Each content model can be configured with dedicated 'custom fields', for example, you can add 'product model', 'color', 'inventory quantity', and other fields to the 'product model'.

When adding a field, we need to define the "parameter name" (for display on the backend) and the "call field" (for template calls), and select an appropriate "field type" (such as single-line text, number, multi-line text, single choice, multiple choice, dropdown, etc.), and you can also set whether it is required and the default value.This 'call field' determines how we accurately refer to and display these custom information in the front-end template.

Several methods to call custom fields on the front-end page

The Anqi CMS template engine syntax is similar to Django, which makes field calls intuitive and powerful. There are usually the following flexible ways to call custom field values:

Method one: Access directly via variable (suitable for the current content detail page)

When you are creating a detailed article page, product details page, or other templates that directly display complete information about the content, the system will automatically load all the data of the current content (including custom fields) intoarchiveThis variable contains (for the category detail page it iscategoryvariable). At this point, you can directly access the value of the custom field using dot notation.

Suppose you created a 'author' field for the article model, with the 'reference field' set toauthorThen you can display it simply on the article detail page template:

<p>文章作者:{{ archive.author }}</p>

If your custom field is of numeric type, for example, the calling field of "reading time" isreadTime:

<p>预计阅读时长:{{ archive.readTime }} 分钟</p>

This method is very convenient and is particularly suitable for directly displaying on the details page of the current content.

Method two: usearchiveDetailorcategoryDetailTag

When you need to control the field retrieval more accurately, or want to obtain specific content *not on the current page* (such as displaying a custom field of a specified ID article in the sidebar),archiveDetailOr for articles or productscategoryDetailThe tag for classification is very useful.

Both these tags can benameSpecify the field to retrieve. For example, you can display the "author" field of an article with ID 10 in a module on the homepage:

<div>特荐文章作者:{% archiveDetail with name="author" id="10" %}</div>

Or in the category list page, display a custom field of the category with ID 5.bannerText:

<div>分类横幅文字:{% categoryDetail with name="bannerText" id="5" %}</div>

This approach provides greater flexibility, allowing you to call specific fields across content.

Method three: usearchiveParamsLabel loop acquisition

Sometimes, you may want to dynamically display all or part of custom fields on a page without needing to know the specific names of each field, or the number and type of these fields may change. At this time,archiveParamsThe tag comes into play. It can retrieve all custom parameters of a specified document (or the current document) and provide them in the form of an array or Map, making it convenient for us to iterate over them.

This tag defaults to retrieving all custom fields of the current document and organizing them into a reusable array. You can iterate over and display them in the template like this:

<div class="custom-fields">
    {% archiveParams params %}
    {% for item in params %}
        <p>
            <span>{{ item.Name }}:</span>
            <span>{{ item.Value }}</span>
        </p>
    {% endfor %}
    {% endarchiveParams %}
</div>

Hereitem.NameIt will display the parameter name you set in the background (such as 'Article Author'),item.ValueThe value entered by the user. If you create a custom field in the background and set the 'Default Value' to empty, and the user does not fill in the content when publishing, thenitem.Valueit will be an empty string.

If your business scenario requires more fine-grained control, such as displaying only specific named custom fields, or you want to access via field name instead of loop index, you canarchiveParamslabel'ssortedthe parameter tofalseThus,paramsThe variable will become a Map, you can access it directly by field name:

<div class="specific-fields">
    {% archiveParams params with sorted=false %}
        <p>文章来源:{{ params.source.Value }}</p>
        <p>发布日期:{{ params.publishDate.Value }}</p>
    {% endarchiveParams %}
</div>

HeresourceandpublishDateAll are the 'callback fields' of the custom fields you set in the background.

Handling values of different types of custom fields

The types of custom fields are diverse, and their display methods on the front end also need to be appropriately handled:

  • Single-line text and numeric types:This field's value is usually a simple string or number, which can be displayed directly.

    <p>联系电话:{{ archive.phoneNumber }}</p>
    <p>商品价格:¥{{ archive.price }}</p>
    
  • Multiline text (rich text editor):If a custom field uses a rich text editor, its value may contain HTML tags. To render these HTMLs correctly on the front end, rather than displaying the source code directly, you need to use|safefilter.

    <div class="product-features">
        <h3>产品特点</h3>
        {{ archive.features|safe }}
    </div>
    

    If the rich text content is in Markdown format and the Markdown editor is enabled on the backend, you can add extra parameters byrender=truecoordinating|safe)to ensure it is rendered correctly as HTML.

    <div class="markdown-content">
        {{ archive.markdownIntro|render|safe }}
    </div>
    
  • Single choice, multiple choice and dropdown choice:

    • Single choice and dropdown choiceIt usually stores only one value and can be directly called like ordinary text.
      
      <p>产品颜色:{{ archive.color }}</p>
      
    • Multiple selectionsValues are usually stored multiple times, these values may exist in the database in the form of a string connected by commas or other delimiters, or they may be an array directly. If it is a comma-separated string

Related articles

How to correctly display the filing number and copyright information at the bottom of the AnQiCMS website and ensure automatic year update?

Information at the bottom of the website, such as filing numbers and copyright statements, may seem trivial, but it is an important manifestation of the website's professionalism, legal compliance, and brand trust.For operators, correctly displaying this information and ensuring that the year can be automatically updated is not only convenient but also avoids the trouble of manual modification every year.In AnQiCMS, achieving this goal is very direct and flexible.

2025-11-09

How does AnQiCMS handle and display the lazy loading (lazyload) feature of images in article content?

In website operation, the loading speed of image content is crucial for user experience and search engine optimization (SEO).Especially when an article contains a large number of images, if all the images are loaded all at once when the page loads, it may cause the page to respond slowly and even affect the user's patience.This problem solved, the image lazy loading (Lazyload) technology emerged.

2025-11-09

How to use the AnQiCMS recommendation attribute (flag) to filter and display specific content, such as "headline" or "slideshow"?

AnQiCMS provides a flexible recommendation attribute (flag) mechanism that allows website operators to easily filter and highlight specific content, whether it is the "top news" on the website homepage, the "slideshow" in the carousel, or other "recommended" content that needs special attention.This feature greatly enhances the efficiency of content operation and the richness of website content.### Understanding Recommended Attributes: Tag content with "Exclusive Tags" Imagine that you have published many excellent articles and product information, but some are currently the most important, most popular, or need to be displayed in specific locations

2025-11-09

Does AnQiCMS support setting custom templates for pages of different categories to achieve differentiated display?

In website operation, we often encounter such needs: different types of content or pages with different themes need to be presented in a unique way to better attract visitors, convey information, and even enhance the brand image.This involves the customization ability of the template.AnQiCMS (AnQiCMS) indeed provides strong and flexible support in this aspect, allowing users to set custom templates for pages of different categories to achieve differentiated content display.

2025-11-09

How to set up an independent display layout and elements for the single page of AnQiCMS, such as the 'About Us' page?

In AnQiCMS, setting up an independent display layout and elements for single pages like "About Us" can make the website content more distinctive and professional.AnQiCMS with its flexible template engine and modular design makes this process intuitive and efficient.Why do you need a custom layout for a single page?

2025-11-09

How does AnQiCMS ensure that the HTML content output through the `safe` filter is secure and correctly parsed and displayed on the front end?

In website operation, we often need to display user input or HTML content generated by a rich text editor.How to ensure that these dynamic contents are displayed correctly and avoid potential security risks is a challenge that every content management system must face.AnQiCMS as a Go language CMS that focuses on security has a mature mechanism in this aspect, especially the `safe` filter it provides, which plays a key role in ensuring the security and correct parsing of front-end content. ### Default security considerations: Why escape automatically?

2025-11-09

How to correctly display user comments and their review status in the AnQiCMS comment list?

In AnQiCMS, managing and displaying user comments, especially their review status, is a very practical feature in content operation.It not only helps us maintain community order, but also allows flexible control over the presentation of website content, ensuring the quality and compliance of published content.Next, let's take a detailed look at how to correctly display user comments and their review status in your website comment list. ### Core Function: Comment List Invocation To display user comments on the website, we need to rely on the powerful template tag system of AnQiCMS.

2025-11-09

Does AnQiCMS provide a convenient tag to display the website logo and name, and support multi-site calls?

In the process of managing and operating a website, the brand image of the website - Logo and website name - is undoubtedly the core identification.They not only represent the identity of the website, but also directly affect users' perception of the brand.For a system like AnQiCMS that is committed to providing an efficient content management solution, how conveniently to display this key information, and to support flexible calls under a multi-site environment, is an important standard to measure its usability and powerful features.AnQiCMS' design concept in this aspect is centered around 'convenience' and 'flexibility'

2025-11-09