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