How do you access the internal properties of a Go language struct (struct) object by using the dot (.) character?

Calendar 78

Use the dot operator (.) operator: The art of accessing Go struct properties in Anqi CMS templates

In modern website operations, an efficient and flexible content management system is the foundation of success.AnQiCMS (AnQiCMS), as an enterprise-level content management platform developed based on the Go language, has won the favor of many users with its high performance, security, and strong customization capabilities.When discussing the customization of AnQiCMS, especially the development of templates, understanding how data flows between the Go backend and the frontend templates, as well as how to elegantly access these data, is a skill that every operator and developer must master.

Today, we will delve deeply into a core issue: when data exists in the Anqi CMS backend as a Go language struct object, how do we access it in the frontend template through the seemingly simple dot (.How to accurately access the various properties inside it? This is not just a technical detail, but also the key to unlocking the infinite possibilities of AnQiCMS templates.

SecureCMS and Go struct: The Secret of Data Streams

Firstly, we need to understand why we encounter the concept of "Go struct".The core architecture of AnQi CMS is built based on Go language, which means all business logic, data processing, and interaction with the database are inseparable from the strong typing features of Go language.structA struct is the basic way to organize data, it packages a group of related fields (attributes) into a single entity.

When the backend logic of AnQi CMS has processed the data and passed these data to the frontend Django-style template engine, the data is often mapped to the template context in the form of Go structs or their collections.The template engine does not run Go code directly, but it has powerful reflection capabilities, allowing us to 'inspect' the internal structure of Go structs, thus enabling us to access the fields of these structs using familiar syntax in the template.

Point(.)Character: access eye in the template

In Anqi CMS template, accessing the internal properties of a Go struct object is intuitive and concise, similar to the field access method of the Go language itself, using a point (.) Characters connect object and property names. For example, if we have a variable representing an article informationarchive, to get its title, we would write: {{ archive.Title }}.

HerearchiveThis is the Go struct object passed from the backend, andTitleThis is a field within this structure. This syntax is not only applicable to a single struct object but also to each element in a struct slice (or list):

{% for item in archives %}
    <a href="{{ item.Link }}">{{ item.Title }}</a>
{% endfor %}

Here, archivesIt is a list containing multiple article structures,forThe loop will take each article structure one by one and assign it toitema variable, and then we can use it toitem.Linkanditem.TitleVisit the link and title of the current article.

In-depth Practice: Accessing Go Struct Attributes in Anqi CMS Template

AnQi CMS provides a rich set of template tags to retrieve various data, most of which are data types that we can access through a point (.Accessed by the operator of the Go structure. Let's look at some specific examples to see how this mechanism works:

  1. Article details (archiveDetail): When you use{% archiveDetail ... %}When a single article detail is obtained by the tag, it returns an article structure. You can easily access its fields:

    <h1>{{ archive.Title }}</h1>
    <p>发布时间:{{ archive.CreatedTime | stampToDate("2006-01-02") }}</p>
    <div>{{ archive.Content | safe }}</div>
    

    here,archiverepresents the article data of the current page,.Title/.CreatedTime/.Contentare all of its direct properties.

  2. Category details (categoryDetail): Similar to, get the category details:

    <h2>{{ category.Title }}</h2>
    <p>{{ category.Description }}</p>
    <img src="{{ category.Logo }}" alt="{{ category.Title }}">
    

    categoryThe object provides you with the title, description, and logo image address of the category, etc.

  3. List data (archiveList,categoryListetc.)When the label returns a list (i.e., a slice in Go language), you need to combinefora loop to iterate over each element, and then use a dot (.) to access its properties:

    {% archiveList latestArchives with limit="5" %}
        <ul>
        {% for article in latestArchives %}
            <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
        {% endfor %}
        </ul>
    {% endarchiveList %}
    

    Here, articleIn each loop is an article structure, we can access its properties througharticle.Linkandarticle.Title.

  4. System configuration (system), Contact information (contact)These global configuration information is also provided in a structured form:

    <footer>
        <p>&copy; {% now "2006" %} {{ system.SiteName }} - {{ system.SiteCopyright | safe }}</p>
        <p>联系电话: {{ contact.Cellphone }}</p>
    </footer>
    

    systemandcontactVariables provide attributes such as website name, copyright information, and contact phone number.

It is not just a property: the call of the struct method

The AnQi CMS template engine even allows you to directly call methods exposed (exported) from Go structs. This brings greater flexibility to data processing. For example, intag-tags.mdMentioned earlier, if you define a method named in a Go structGetThumb()that returns the thumbnail URL of the article, you can call it directly in the template:

<img src="{{ item.GetThumb() }}" alt="{{ item.Title }}" />

Please note that the method call also follows the export rules of the Go language (the first letter of the method name is capitalized), and usually does not accept parameters, or only accepts specific context parameters that the template engine can automatically handle.

Deep dive and debugging: Make good use of tools

During template development, you may sometimes be unsure about which accessible properties a Go struct actually contains, or what the names of these properties are. In such cases, some auxiliary means provided by AnQiCMS can come in handy:

  • View the official documentationThis is the most direct and effective method. The AnQiCMS tag documentation (such astag-/anqiapi-archive/142.html) will list in detail the data structure returned by each tag and the available fields.
  • UsedumpFilterIn the debugging phase,{{ variable | dump }}This filter helps you print the complete structure, type, and current value of a Go struct to the page, allowing you to clearly see all accessible properties inside.This is the tool to understand complex data structures.

Summary

Pass through point(.The character access to the internal properties of the Go structure is a fundamental and core operation in the development of secure CMS templates.It not only embodies the intuitiveness of Go language data organization methods, but also showcases the intelligence and strength of Anqi CMS template engine.From simple text output to complex nested data display, to direct invocation of struct methods, mastering this mechanism will greatly enhance your efficiency and creativity in content operation and template customization on AnQiCMS.I hope this article can help you better master Anqi CMS and unleash the unlimited potential of your website.

Frequently Asked Questions (FAQ)

  1. Why the attribute name I access isTitleinstead oftitle?Go language has an important convention: only the structure field names with the first letter capitalized(such asTitle/LinkOnly when it is considered "exported" can it be accessed outside the package.The AnQi CMS template engine follows this rule when parsing data.PascalCase(Pascal case) attribute names.)

  2. Can I directly call the method of a Go struct in the template? What should I pay attention to?Yes, Anqi CMS template engine supports calling exported methods of Go structs. For example, ifarchivea struct has a namedGetThumb()method, you can use it directly in the template.{{ archive.GetThumb() }}Call. It should be noted that these methods must be exported from the Go structure (capitalized first letter), and usually not

Related articles

How to correctly display dynamic data variables passed from the backend or controller to the frontend in Anqi CMS template?

As an experienced website operations expert, I have been dealing with AnQiCMS (AnQiCMS) for many years in my daily work, and I am well aware of its efficient and flexible content management features.When using Anq CMS to build and maintain websites, a core and frequently encountered requirement is how to accurately display dynamic data passed from the backend or controller in the front-end template.Today, let's delve deeply into this topic and help everyone better master the template function of AnQiCMS

2025-11-07

How to reduce the workload when updating the website theme or changing the layout using the `extends` tag?

## AnQi CMS: Use the `extends` tag to easily manage theme and layout updates, say goodbye to repetitive work Theme iteration and layout adjustment are common needs in the daily operation of a website.However, for many website managers and content operators, each such change often means a huge amount of work and potential risk of error: modifying a header may require manual editing of dozens, even hundreds, of files;Adjust a sidebar, and it's another round of massive 'copy-paste' engineering.This way of working is not only inefficient

2025-11-07

Why is it recommended to wrap all possible variable data inside the `block` tag in the template inherited by `extends`?

In the world of Anqi CMS templates, the `extends` tag is undoubtedly the core tool for building an efficient and maintainable website skeleton.It allows us to define a basic layout (like a well-designed blueprint), then let all pages be built on this foundation for content filling.However, using `extends` alone is not enough to fully utilize its potential.

2025-11-07

What is the main difference between the `extends` tag and the `include` tag in constructing template structures and managing page relationships?

## The Way of Building AnQi CMS Templates: Core Differences and Application Practices of `extends` and `include` Tags The flexible use of template engines in today's efficient iterative website development is the key to improving efficiency and ensuring project maintainability.As an experienced website operations expert, I am well aware that AnQiCMS, with its powerful performance in Go language and Django-style template syntax, provides an excellent solution for content management.The `extends` and `include` tags in its template system are for building efficient

2025-11-07

How to directly call built-in methods (such as getting thumbnails) of a Go language struct object in a template?

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible CMS system for enterprise content management.AnQiCMS (AnQiCMS) leverages its powerful performance based on the Go language and the ease of use of the Django template engine, bringing us an unprecedented degree of freedom.In daily content operations, we not only hope that the data can be displayed accurately, but also hope that these data can come alive and be intelligently processed according to different needs.Today, let's delve into a seemingly advanced but actually very practical skill

2025-11-07

If a variable's value is an array or slice, how can you use a `for` loop in a template to iterate over and output its contents?

As an experienced website operations expert, I know that efficiently displaying dynamic data is very important in daily content management.AnQiCMS (AnQiCMS) leverages the powerful performance and flexible template system of the Go language to provide us with an excellent solution.Today, let's delve deeply into a practical and common scenario in AnQi CMS template development: how to use `for` loops to traverse arrays or slices and elegantly present their content on web pages.

2025-11-07

What do `{{item.Title}}` and `{{archive.Title}}` represent in different template contexts?

As an experienced website operations expert, I know that understanding the meaning and usage scenarios of template variables is crucial for efficient website operation in a content management system.AnQiCMS (AnQiCMS) provides great convenience for content display with its flexible and powerful template engine.However, beginners or users not familiar with its template mechanism often become confused about the two seemingly similar variables `{{item.Title}}` and `{{archive.Title}}`.}]

2025-11-07

How to display the current date and time in the Anqi CMS template and output them in a custom format?

As an expert with many years of experience in website operations, I am well aware of the importance of content timeliness and display professionalism to the value of the website.In an AnQiCMS content management system that is flexible and efficient, accurately controlling the display of dates and times on the page and formatting the output according to actual needs is an indispensable part of improving user experience and optimizing information delivery.Today, let's delve into how to display and format dates and times effortlessly in the AnQiCMS template.

2025-11-07