Creating templates in AnQiCMS and mastering its variable naming and tag syntax rules is the core.System is developed based on Go language, the template engine borrows the syntax style of Django and Blade, which makes content output and logic control very intuitive.You will mainly come across two basic syntax structures when developing templates, each of which has different responsibilities.
Core syntax structure: Variable output and logical control
The first is double curly braces{{ 变量 }}It has a very direct function, which is to 'print' the variable values passed from the system or background to the template onto the page.You can imagine it as a placeholder that will be replaced by the actual data.{{ system.SiteName }}.
Then is single curly braces and percentage sign{% 标签 %}This set of tags is used to handle various logic in templates, such as conditional judgments, loop traversals, and including other template files.It does not directly output content, but controls the display style or structure.{% end标签 %}The form of appearing, forming a closed code block. For example, a conditional judgment would be{% if 条件 %} ... {% endif %}.
Variable naming and access
AnQiCMS中的变量命名通常遵循驼峰命名法则(CamelCase),这意味着变量名中的每个单词首字母会大写,例如Archive.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:{{...}}Practical application of:
After understanding the basic syntax, let's take a look at{{...}}Specific usage in the actual template:
- Directly output the variable valueThis is the most common usage, for example:
<h1>{{ archive.Title }}</h1> {# 输出文章标题 #} <p>联系电话:{{ contact.Cellphone }}</p> {# 输出联系电话 #} - Process data with filters (Filters):Sometimes we need to format or convert the output data. AnQiCMS provides a rich set of filters, via the pipe symbol.
|to apply.- HTML content security outputWhen a variable contains HTML content (such as article text), in order to ensure that the browser correctly parses it rather than displaying the raw HTML code, and to prevent potential XSS attacks, it is usually necessary to use
|safeFilter.<div>{{ archive.Content|safe }}</div> {# 安全输出文章HTML内容 #} - Date and time formattingThe timestamp stored in the database needs to be converted to a readable date format,
stampToDateThe function combined with 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个字符 #}
- HTML content security outputWhen a variable contains HTML content (such as article text), in order to ensure that the browser correctly parses it rather than displaying the raw HTML code, and to prevent potential XSS attacks, it is usually necessary to use
Logic control:{%...%}The powerful functions of tags
{%...%}Tags give the template the ability to dynamically display content:
- Conditional judgment (
if/elif/else):According to different conditions, display different content blocks.{% if archive.IsFeatured %} <span class="badge">推荐</span> {% elif archive.Views > 1000 %} <span class="badge">热门</span> {% else %} <span>普通内容</span> {% endif %} - Loop traversal (
for/empty):Iterate over the data in an array or list, commonly used to display article lists, navigation menus, and so on.
In the loop,{% for item in archives %} <li><a href="{{ item.Link }}">{{ forloop.Counter }}. {{ item.Title }}</a></li> {% empty %} <li>暂无文章</li> {# 当archives列表为空时显示 #} {% endfor %}forloop.Counterauto English returns the current loop count (starting from 1),forloop.Revcounterthen returns the remaining count from the end. - Define variables (
with/set): Create temporary variables in the template, which is convenient for code reuse and organization.withLabels define variables that are{% with %}and{% endwith %}valid between,setThe defined variable has a wider scope in the current template file. “`twig {% with greeting=“Hello” %} <p