How to dynamically retrieve and display the content of custom model fields for an article in a template?

Calendar 👁️ 74

In AnQi CMS, the flexibility of website content is one of the key advantages for our content operation and website layout.By customizing the content model field, you can add unique properties for different types of content (such as articles, products) that better reflect your business needs.But how can we dynamically present these carefully defined custom field contents in the front-end template of the website?This article will discuss this topic in detail, helping you fully utilize the powerful functions of AnQiCMS.

Flexible customization, efficient management: Anqi CMS content model fields

The Anqi CMS provides the "flexible content model" feature, which means you can create unique fields for articles, products, and other content according to your actual business scenarios.For example, if you run an e-commerce website, in addition to names and descriptions, products may also require exclusive fields such as 'brand', 'model', 'material', 'stock quantity', etc.These custom fields not only make the backend management more structured, but also lay the foundation for the diverse content presentation on the frontend.

You can find the 'Content Model' feature under 'Content Management' in the Anq CMS backend, where you can define new fields for different models.For example, you can add a 'author bio' text field to the 'article model', or a 'color options' multi-select field to the 'product model', or even a 'product album' group image field.Define these fields and when you publish or edit an article/product, you will see these custom input items.

Retrieve the core tag for custom field content in the template

When the content of the custom field is entered in the background, the next step is how to display them in the front-end template.AnQi CMS provides powerful template tags, making this process very intuitive.Mainly used isarchiveDetailTags andarchiveParams.

1. Accurately retrieve a single custom field:archiveDetailTag

If you already know the specific name of the custom field you want to display,archiveDetailThe tag is your preference. This tag can directly retrieve the content of the custom fields you define, just like obtaining built-in fields such as article title and content.

Assuming you have customized a namedauthor_bioThe field of the author's introduction, then in the article detail page template, you can obtain and display its content in the following way:

<div class="author-info">
    <h3>作者简介:</h3>
    {% archiveDetail authorBio with name="author_bio" %}
    <p>{{ authorBio|safe }}</p>
</div>

We used here{% archiveDetail authorBio with name="author_bio" %}toauthor_bioAssign the content of the field toauthorBiothe variable, then through{{ authorBio|safe }}Display it.|safeThe filter is very important here, especially when your custom field content may contain HTML tags (such as content edited in a rich text editor) as it ensures that the content is parsed correctly and not displayed as plain text. If the custom field stores Markdown-formatted content, you can use|renderThe filter renders it into HTML.

2. Flexibly iterate through all custom fields:archiveParamsTag

Sometimes, you may need to iterate over all the custom fields of an article in a template, or be unsure of the specific field names, in such casesarchiveParamsTags come into play. It can retrieve all custom parameters of the current article or a specified article.

archiveParamsTags have two main usage methods:

a) Traversed as an ordered array:By default,archiveParamsIt will return an ordered array, each item of which includesName(field display name) andValue(field value).

<div class="custom-fields-list">
    <h3>更多文章信息:</h3>
    {% archiveParams params %}
    {% for item in params %}
        <p><strong>{{ item.Name }}:</strong> {{ item.Value }}</p>
    {% endfor %}
    {% endarchiveParams %}
</div>

This method is very suitable for universally displaying all custom fields without concerning about the specific names of the fields.

b) As an unordered Map accessed directly:If you want to access the value of a custom field by its 'access field' name as if it were an object property, you canarchiveParamsthe tag withsorted=falseParameter.

Assuming your article model has definedisbn(ISBN number) andpublisher(publisher) two custom fields, their 'call fields' areisbnandpublisher:

<div class="book-info">
    <h3>图书信息:</h3>
    {% archiveParams bookDetails with sorted=false %}
    <p><strong>ISBN:</strong> {{ bookDetails.isbn.Value }}</p>
    <p><strong>出版社:</strong> {{ bookDetails.publisher.Value }}</p>
    {% endarchiveParams %}
</div>

This method requires you to clearly know the name of the custom field's 'calling field', but it can make the code more concise in certain layouts.

Application based on actual scenarios

After understanding the core tags, let's take a look at some common practical application scenarios:

  • Display the product gallery:If you have defined a namedproduct_imagesThe group field is used to store multiple images of the product, and you can display them like this on the product detail page:

    <div class="product-gallery">
        {% archiveDetail productImages with name="product_images" %}
        {% for imgUrl in productImages %}
            <img src="{{ imgUrl }}" alt="产品图片">
        {% endfor %}
        {% endarchiveDetail %}
    </div>
    
  • Display the specifications or features of the product:Assuming you have custom fields such as 'size', 'weight', 'color', etc., you can combinearchiveParamsto display a list:

    <ul class="product-specs">
        {% archiveParams specs %}
        {% for item in specs %}
            <li><strong>{{ item.Name }}:</strong> {{ item.Value }}</li>
        {% endfor %}
        {% endarchiveParams %}
    </ul>
    
  • To display special hints or descriptions:For example, an article has a field namedspecial_note(Special reminder) single-line text field

Related articles

How to retrieve and display a list of articles under a specific category (or multiple categories) in AnQiCMS?

In AnQiCMS, flexibly displaying website content is the key to improving user experience and website functionality.No matter if you are running a corporate website, a personal blog, or a marketing site, you often encounter the need to display a list of the latest articles, popular products, or multiple related category content under a specific category on a particular page.AnQiCMS's powerful template tag system, especially the `archiveList` tag, can help you easily achieve this goal.

2025-11-07

How to configure and display related article lists on the article detail page, and specify relevance rules (such as keywords or manual settings)?

How to make visitors easily find more related content after reading an excellent article on your website, continue to explore your website, which can not only significantly improve user experience, but also is a key strategy to optimize website SEO and reduce bounce rate.AnQiCMS (AnQiCMS) offers powerful and flexible features, allowing you to easily configure and display related article lists on the article detail page and specify relevance rules according to actual needs.Next, we will together learn how to achieve this goal in Anqi CMS.

2025-11-07

How to get and display the title and link of the previous and next articles on the article detail page?

It is crucial to provide a smooth user experience in the operation of our website.When a visitor finishes reading a rich article, they often want to follow the trail and continue browsing other content in the same category or related topics.In this case, it is particularly important to display the titles and links of the previous and next articles on the article detail page.

2025-11-07

How to implement the pagination display function of AnQiCMS article list, as well as how to customize the pagination style and logic?

In content operation, the pagination display of the article list is a basic and crucial function, which not only affects the user's browsing experience, but also is closely related to the performance and SEO performance of the website.AnQiCMS as an efficient and customizable content management system provides us with a flexible way to implement and beautify the pagination function of the article list.

2025-11-07

How to retrieve and display the list of Tag tags and their corresponding links for an article?

In a website with increasingly rich content, how to effectively organize and present information is crucial.The Tag label (usually referred to as keywords or tags) not only helps users find related content quickly, but is also an indispensable part of Search Engine Optimization (SEO).AnQi CMS understands this point, providing users with a powerful and flexible tagging function, allowing you to easily add tags to articles and display these tags and their corresponding links in multiple forms on the website front end.

2025-11-07

How to retrieve and display the system global settings of AnQiCMS template, such as website name, Logo, filing number, and copyright information?

When building a website, some core information such as the website name, website logo, filing number, and copyright information are indispensable elements.These global settings not only help visitors quickly identify your brand, but also reflect the professionalism and compliance of the website.AnQiCMS provides a straightforward and flexible way to manage and present these system-level information in templates.Where can I set these global information?First, these important website information need to be unified configured in the AnQiCMS background.

2025-11-07

How to obtain and display the contact information of the website in AnQiCMS, such as phone, email, address, and social media links?

It is crucial to clearly display contact information when building and operating a website to establish user trust and provide customer support.AnQiCMS as an efficient content management system provides an intuitive way to manage and display this key information, whether it is traditional phone numbers, email addresses, or modern social media links, it can be easily realized. ### One, Back-end Configuration: Centralize Your Contact Information AnQiCMS has designed a centralized area for you to manage all website contact information in one place.

2025-11-07

How does AnQiCMS use the `navList` tag to build and display multi-level website navigation menus?

When building a website with comprehensive functions, a clear and intuitive navigation menu is the core of user experience.AnQiCMS provides a powerful template tag system, where the `navList` tag is specifically used to build and display a multi-level navigation menu for the website.It not only can display the navigation structure you configure in the background, but also help you convert technical details into user-friendly interactive experiences.### The Foundation of Building Navigation: Backend Settings Before using the `navList` tag, you first need to set up in AnQiCMS

2025-11-07