What are the common variable naming and tag syntax rules (such as `{{variable}}` and `{% tag %}`) when making AnQiCMS templates?

Calendar 👁️ 85

To create templates in AnQiCMS and master its variable naming and tag syntax rules is the core.The system is developed based on Go language, the template engine adopts the syntax style of Django and Blade, which makes content output and logic control very intuitive.You will mainly encounter two basic syntax structures when developing templates, each with different responsibilities.

Core syntax structure: variable output and logical control

First are the double braces{{ 变量 }}It has a very direct role, which is to 'print' the variable values passed from the system or backend to the template on the page.You can imagine it as a placeholder, which will eventually be replaced with actual data.For example, to display the name of the website, it might be written as{{ system.SiteName }}.

Then the single braces and percent sign{% 标签 %}This set of tags is used to handle various logic in templates, such as conditional judgment, loop traversal, and importing other template files.It does not directly output content, but controls the display or structure of the content.All logical control tags must have a corresponding end tag to{% end标签 %}The form appears, forming a closed code block. For example, a conditional judgment would be{% if 条件 %} ... {% endif %}.

Variable naming and access

AnQiCMS variable naming usually follows the CamelCase naming convention, which means that the first letter of each word in the variable name is capitalized, for exampleArchive.Title/Category.Link.

When you need to access a property within a variable, you will use a dot.to perform hierarchical access. For example,archiveis an object representing an article, its title isarchive.Title, and its link isarchive.LinkThis concise access method allows you to clearly obtain the required data.

Output variable:{{...}}actual application

After understanding the basic syntax, let's take a look at{{...}}the specific use in the actual template:

  • Directly output the variable valueThis is the most common usage, for example:
    
    <h1>{{ archive.Title }}</h1> {# 输出文章标题 #}
    <p>联系电话:{{ contact.Cellphone }}</p> {# 输出联系电话 #}
    
  • Combine filters (Filters) to process dataWe sometimes need to format or convert the output data. AnQiCMS provides a variety of filters, using the pipe symbol|to apply.
    • HTML content safety outputWhen a variable contains HTML content (such as article content), in order to ensure that the browser parses it correctly rather than displaying the original HTML code, and to prevent potential XSS attacks, it is usually necessary to use|safefilter.
      
      <div>{{ archive.Content|safe }}</div> {# 安全输出文章HTML内容 #}
      
    • Date and time formatting: The timestamp stored in the database needs to be converted to a readable date format,stampToDateThe function combined with the Go language's date formatting string is very practical.
      
      <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span> {# 将时间戳格式化为“年-月-日” #}
      
    • Text truncation: For long text, you can use|truncatecharsor|truncatewordsto truncate.
      
      <p>{{ archive.Description|truncatechars:100 }}</p> {# 截断描述,保留100个字符 #}
      

Logical Control:{%...%}The powerful function of the tag

{%...%}The tag gives the template the ability to dynamically display content:

  • Conditional judgment(if/elif/else): Display different content blocks based on different conditions.
    
    {% if archive.IsFeatured %}
        <span class="badge">推荐</span>
    {% elif archive.Views > 1000 %}
        <span class="badge">热门</span>
    {% else %}
        <span>普通内容</span>
    {% endif %}
    
  • Loop traversal(for/empty)Iterating over array or list data, often used to display article lists, navigation menus, etc.
    
    {% for item in archives %}
        <li><a href="{{ item.Link }}">{{ forloop.Counter }}. {{ item.Title }}</a></li>
    {% empty %}
        <li>暂无文章</li> {# 当archives列表为空时显示 #}
    {% endfor %}
    
    In the loop,forloop.CounterReturns the current loop count (starting from 1),forloop.RevcounterThen returns the remaining count from the end.
  • Define a variable(with/set): Create a temporary variable in the template, which is convenient for code reuse and organization.withThe variable defined by the{% with %}and{% endwith %}is valid between, andsetThe variable is defined with a wider scope in the current template file. “`twig {% with greeting=“Hello” %} <p

Related articles

How does Anqi CMS template support adaptive, code adaptation, and independent display mode on PC and mobile?

How to ensure that the website can provide a high-quality user experience on different devices is a challenge that cannot be ignored in today's content marketing and website operations.AnQiCMS (AnQiCMS) knows this, therefore it provides flexible and diverse adaptation modes in template design and content display, allowing users to easily deal with various terminal access scenarios according to their own needs.

2025-11-08

How to set up independent display template files for specific articles or categories?

In the daily operation of websites, we often encounter such situations: some articles are in-depth reports, some are promotional pages for events, and some are corporate profiles or product details.They each have unique display requirements, and if they all follow the website's general template, it may seem monotonous and fail to fully exert the appeal of the content.AnQi CMS is an efficient and flexible content management system that fully considers this personalized demand, allowing us to set independent display template files for specific articles, categories, even single pages, making the content display more tailored and impactful.

2025-11-08

On the document detail page, how to call and display the custom model field content of the article?

The flexible content model of AnQiCMS (AnQiCMS) is a very powerful tool that allows us to add dedicated fields for different types of content (such as articles, products, events, etc.) based on the actual needs of the website.This means that our content is no longer limited to fixed titles, summaries, and content, but can carry more diverse and structured information.How do we call and display the content of these custom model fields on the document detail page? Next, let's explore the various flexible ways provided by Anqi CMS.###

2025-11-08

How to implement personalized front-end display for different types of content (articles, products, single pages) in AnQi CMS?

In AnQi CMS, achieving personalized front-end display of different types of content (such as articles, products, and single pages) is one of its core advantages, and it is also a key factor for content operators to achieve brand differentiation and improve user experience.This is mainly due to the flexible content model design and powerful template customization capabilities of Anqi CMS. ### Flexible Content Model: The Foundation of Personalized Display The core of Anqi CMS lies in its "flexible content model".This means you are not limited to fixed "article" or "product" types, but can customize various content models according to your actual business needs. For example

2025-11-08

How to efficiently organize and reference common page header, footer, and other code snippets in the front-end page template?

Manage front-end page code in AnQi CMS, especially the header, footer, and other code snippets that need to be repeated on multiple pages, which is a key to improving development efficiency and website maintainability.AnQi CMS provides a powerful and flexible template engine, allowing us to organize and reference these common codes in an elegant way. ### The Importance of Template Reusability Imagine if your website has dozens, even hundreds of pages, each of which independently contains the complete header, navigation, and footer code. When the website's logo needs to be replaced, or the footer copyright information needs to be updated

2025-11-08

How to customize the display layout of content on the frontend page in Anqi CMS?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides us with great freedom with its powerful template functions and content model, allowing us to finely control the layout of content on the front-end page according to the actual needs of the website.To achieve this goal, we need to deeply understand the template engine, content model, and built-in tags of Anqicms.

2025-11-08

How to implement independent templates for different pages (such as articles, products, single pages) in Anqi CMS?

In website operation, we often need to adopt completely different layouts and design styles for different types of content, such as a deep article, a product display page, or an 'About Us' single page.This differentiated display not only enhances user experience, but also helps content better convey information and achieve marketing goals.AnQiCMS (AnQi CMS) provides a very flexible and powerful solution in this regard, allowing us to easily implement independent templates for different pages.

2025-11-08

How to configure Anqi CMS to support responsive design or independent mobile content display?

In the era of co-existing multiple devices, websites that can adapt well to various screen sizes, whether PC, tablet, or mobile phone, have become a basic requirement.AnQiCMS (AnQiCMS) understands this and provides a variety of flexible configuration options to help you easily implement responsive design or independent mobile content display, ensuring that your content brings users a high-quality browsing experience on any device.The Anqi CMS provides three main website modes to support multi-terminal display: adaptive, code adaptation, and PC+mobile independent sites

2025-11-08