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