How to call the embedded method of Go structure in AnQiCMS template to get and display data?

Calendar 81

AnQiCMS leverages its efficient architecture based on the Go language and flexible template system, providing strong support for website content display.For users who want to implement smarter and more dynamic content display in templates, understanding how to call the built-in methods of Go structures in AnQiCMS templates is particularly important.This can make your template more tidy, and it can encapsulate complex logic processing on the back end, allowing the front end to focus more on visual presentation.

Unveiling the Go method call in AnQiCMS template

In AnQiCMS, the template is based on a grammar similar to the Django template engine, it displays data through double curly braces{{变量}}and data through single curly braces followed by a percentage sign{% 标签 %}Process logic. When data is retrieved from the backend, it is usually passed to the template in the form of a Go struct.

Usually, we would directly access the fields of the structure to display data, for example, in a document list loop, you might see{{item.Title}}to display the document title, or{{item.Description}}To display the document introduction. HereTitleandDescriptionareitemthe public fields of this document structure.

However, a Go struct can also define methods in addition to fields.These methods encapsulate specific logic, such as formatting data, performing calculations, or obtaining a derived value.In the AnQiCMS template, you can call its built-in public methods directly, just like calling a field of a Go structure.()Even if this method does not require any parameters.

For example, ifArchive(Document) The structure is defined in the Go backend namedGetThumb()The method, which may be used to return a thumbnail URL that has been specially processed (such as cropping, adding watermarks, or obtaining default values), can be called in the template like this:{{item.GetThumb()}}.

Why call Go methods in templates?

Encapsulating logic in Go backend methods and calling them from templates brings many benefits:

  1. Encapsulation and template neatness:Complex business logic, such as image URL generation rules, intelligent content summarization, or determining whether to display an element based on specific conditions, can all be encapsulated in Go methods.This makes the template code clearer, easier to read and maintain, and avoids cluttering the template with complex conditional judgments and data processing logic.
  2. Data preprocessing and formatting:Backend methods can preprocess and format the original data. For example,GetThumb()The method may not only return the image path, but may also automatically adjust the image size based on the context, or provide a default placeholder image if the image does not exist.
  3. Improve maintainability and consistency:When the data processing logic changes, you only need to modify the corresponding method on the Go backend, without having to modify all related template files, which greatly improves the maintainability of the system.At the same time, all templates call the same method, which also ensures the consistency of data processing.
  4. Scalability:If you are doing a secondary development on AnQiCMS, you can add your own methods to existing or custom Go structs to easily implement more customized functions in the template.

Practice: Calling Go struct methods in templates

Next, we will demonstrate how to call the built-in methods of Go structures and related data processing methods in the AnQiCMS template through several common scenarios.

Scene one: Get the processed thumbnail path

Assuming we are displaying a document list and want to display a thumbnail that has been processed by the backend in AnQiCMSArchiveThere may be a structure in itGetThumb()Methods are used for this purpose.

{# 假设这是一个文章列表页,通过 archiveList 标签获取文档列表 #}
{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div class="article-card">
            <a href="{{item.Link}}" class="article-link">
                {# 调用 Go 结构体 Archive 的 GetThumb() 方法获取缩略图 URL #}
                <img src="{{item.GetThumb()}}" alt="{{item.Title}}" class="article-thumbnail" />
                <h3 class="article-title">{{item.Title}}</h3>
                <p class="article-description">{{item.Description}}</p>
            </a>
        </div>
    {% empty %}
        <p>目前没有可用的文章。</p>
    {% endfor %}
{% endarchiveList %}

In this example,{{item.GetThumb()}}It isitemrepresenting aArchivestructure instance) called onGetThumb()method. This method may be responsible for judgment on the Go backend.itemIs there a thumbnail, if so, return its URL, if not, return a default placeholder image URL, or perform other image processing operations.

Scenario two: Combine template functions for time formatting

Sometimes, a struct field stores raw data (such as timestamps), and we need to format it in the template.AnQiCMS provides built-in template functions to handle such requirements.

In the document structureCreatedTimeThe field is typically a Unix timestamp.Although this is not a direct call to the struct method, it demonstrates how to combine the data provided by the Go backend with functions at the template level.

`twig

Related articles

How to integrate image captcha functionality into the comment or message form to improve security?

When a website's comment section or message board is flooded with spam, the captcha is undoubtedly a simple and effective defense measure.It can effectively identify whether it is a human user or an automated program, thereby preventing malicious screen scraping or spam comments.AnQi CMS understands the importance of website security, therefore it has integrated the graphic captcha function into the system, allowing us to easily add this layer of protection to the form and enhance the overall security of the website.

2025-11-08

How to build a comment form in a template and dynamically display custom fields from the backend?

In website operation, the feedback form is an important bridge for interaction with users, collecting feedback, and establishing contact.AnQiCMS (AnQiCMS) provides a powerful and flexible message feature, which not only allows users to quickly build basic forms but also supports custom fields through the backend to make forms dynamically adapt to various business needs.This article will delve into how to build such a comment form in Anqi CMS templates and dynamically display these custom fields from the backend.

2025-11-08

How to display a comment list on the website front end and support pagination, replies, and review status distinction?

The website comment section is an important battlefield for user communication and content feedback. A well-designed and functional comment system can greatly enhance the activity and user stickiness of the website.AnQiCMS provides us with a flexible and powerful comment management feature, whether you want to display comments, implement reply interaction, or differentiate according to the review status, you can easily achieve this through simple template tags.This article will introduce in detail how to efficiently display comment lists on the website front-end, and implement features such as pagination, reply, and review status distinction.### One, Basic Display of Comment List

2025-11-08

How to safely display content containing HTML tags or JavaScript code in a template and prevent XSS attacks?

Building website templates in AnQiCMS (AnQiCMS), ensuring the safe display of content is a core issue, especially when the content may contain HTML tags or JavaScript code, how to effectively prevent cross-site scripting (XSS) attacks is particularly important.AnQi CMS is a Go language content management system that focuses on security and considered these security challenges from the outset, providing mechanisms to help users manage and control the rendering of content.

2025-11-08

How to display the current year or a specified format of time in a template, such as "2023-10-26 15:30:00"?

In website operation, it is often necessary to dynamically display the current year or a specific date and time format at different positions on the page, whether it is used for the copyright statement in the footer, the publication update time of articles, or the time points of various events.AnQiCMS (AnQiCMS) provides flexible and powerful template tags, allowing you to easily meet these needs.

2025-11-08

How to retrieve and display custom contact information from the background in a template (such as phone, email, WeChat)?

In website operation, clearly displaying contact information is crucial for improving user experience and promoting business communication.Whether it is customer consultation, seeking support, or business cooperation, visitors hope to find and contact you quickly.AnQiCMS provides a convenient and efficient mechanism for you to centrally manage these contact information in the background, and dynamically display them in the website template without frequently modifying the code, which greatly improves the efficiency of website maintenance.### Contact Management in AnQiCMS On AnQiCMS backend

2025-11-08

How to customize Json-LD structured data to enhance search engines' understanding and display of page content?

In today's highly competitive online environment, making website content stand out, being accurately understood by search engines, and presented in an attractive manner is a goal that every website operator diligently seeks.Among them, the structured data of Json-LD plays a vital role.

2025-11-08

How to display user group details, user avatar, username, and other personal information in the template?

In website operations, providing users with personalized experiences, enhancing community interaction, and clearly showcasing the rights and interests of different user groups is the key to improving user stickiness and website value.AnQiCMS as a powerful content management system, provides flexible template tags, allowing us to easily display users' personal information (such as username, avatar) and details of their user group on the front-end page.

2025-11-08