What template engine syntax does AnQi CMS support to control content display?

Calendar 👁️ 73

As users of AnQi CMS, we all hope to control the presentation of website content efficiently and flexibly.AnQi CMS does a very good job in this aspect, it provides a set of intuitive and powerful template engine syntax, allowing us to easily achieve a variety of content display needs.

Anqi CMS template engine basics: Django style, easy to learn and use

The Anqi CMS template engine uses a marking method similar to Django syntax, which allows friends with experience in other template engines to quickly get started.Its design philosophy is to strive for simplicity and efficiency, even if you are a first-time user, you can master its core usage in a short time.

In AnQi CMS template files, there are two main syntax structures:

  1. Variable output:{{ 变量名 }}This is the most common usage, used to display dynamic data on the page. For example, if you want to display the title of an article on the web page, you just need to write in the template.{{ archive.Title }}. The double curly braces contain the data variables to be displayed. The system will automatically replace them with the actual content. This method is direct and clear, making the presentation of data extremely simple.

  2. Logical control and labeling:{% 标签名 参数 %}and{% 结束标签 %}When we need to implement more complex logic such as conditional judgments, loop traversals, and data acquisition in templates, we will use the tag syntax combined with single curly braces and percent signs. These tags usually appear in pairs and have clear start and end marks, for example{% if 条件 %}...{% endif %}Used for conditional judgment,{% for item in 列表 %}...{% endfor %}Used to loop through list data. This structure clearly defines the logical scope, effectively avoiding confusion.

Powerful built-in tags: retrieve various content and features

The AnQi CMS is built-in with a series of rich tags, which are the core tools for dynamic content display.They can help us retrieve system settings, article lists, category information, and even user comments from the database, and display them according to the rules we set.

For example, when you need to display the latest article list on the homepage,{% archiveList archives with type="list" limit="10" %}tags can help you dynamically fetch and display this content. If you want to build a website navigation menu,{% navList navs %}Labels can conveniently pull the navigation data configured on the back-end. In the article detail page,{% archiveDetail with name="Content" %}then you can easily pull up the main content of the article.

It is worth mentioning that these tags often support rich parameters, such ascategoryId(Category ID),moduleId(Model ID),order(Sorting method),limit(Display quantity) and so on, allowing us to accurately control the source and presentation rules of the data. In addition,{% with 变量名="值" %}Tags also allow you to temporarily define variables in the template, which is very useful when passing data toincludethese auxiliary tags, helping to maintain code cleanliness and modularity.

For displaying dates and times, Anqi CMS also provides convenient tools.{{ stampToDate(时间戳, "格式") }}This function allows you to beautify the timestamp data stored in the database, in any date and time format you need (for example, “2006-01-02 15:04”), without manual conversion.

Flexible filter: beautify and process data

In addition to direct output and logical control, the Anqi CMS template engine also provides a 'filter' function, allowing you to fine-tune and beautify the data before it is finally displayed. The filter passes|The symbol acting on the variable after can be used in a chain, very flexible.

The following are some commonly used filters and their purposes:

  • |safe: When you need to display content containing HTML tags (such as article text) without escaping these tags,|safeThe filter is your preference. It tells the browser that this part of the content is safe and can be directly parsed as HTML.
  • |truncatechars:数字or|truncatewords:数字: These two filters are commonly used to truncate long text and add an ellipsis at the end.The former truncates by character count, the latter by word count, and is very suitable for displaying brief content in lists or summaries.
  • |default:"默认值": When a variable may be empty or not exist, you can use|defaultA filter provides a fallback value to prevent the page from displaying blank or error messages.
  • |join:"分隔符"and|split:"分隔符":|joinUsed to concatenate array elements with a specified delimiter,|splitCan split a string by delimiter into an array, which is very useful when processing list data such as tags, keywords, etc.
  • |date:"格式": Similar tostampToDatesimilar,|dateThe filter can be used totime.TimeThe date object of the type is formatted into a specified string, following the time formatting rules of the Go language.

Auxiliary label: Build modular and reusable templates.

To improve the maintainability and reusability of the template, Anqi CMS introduces several auxiliary tags to encourage modular development:

  • {% include "模板文件" %}: Allow you to embed the content of a template file (such as a header, footer, or sidebar) into another template, avoiding repeated code writing.
  • {% extends "基础模板" %}and{% block 名称 %}: This is a powerful template inheritance mechanism. You can create a "base template" that includes common page structures (such as headers, footers, sidebars), and then other pages can inherit through it.{% extends %}Inherit it. Through{% block %}Define the areas that can be overridden, so that child templates can overwrite these areas to achieve local content customization while maintaining overall consistency.
  • {% macro 名称(参数) %}: Macro (Macro) is similar to a function, allowing you to define reusable code snippets.Macros can accept parameters and be used in templates like a function call, greatly enhancing code reusability.
  • {% lorem %}: In the early stages of template development, if real data is lacking,{% lorem %}tags can quickly generate placeholder text to help you adjust the page layout and style.
  • {%-and-%}These special markers are used to control the whitespace characters during template rendering.By adding hyphens on both sides of the tag, you can eliminate the extra line breaks and spaces around the tag itself, making the generated HTML code more compact.

Summary

The template engine syntax of AnQi CMS is clever, balancing flexibility and ease of use.Whether it is a simple content display, complex logic control, or efficient modular development, it provides a comprehensive and intuitive solution.Familiarize yourself with these syntaxes and tags, which will greatly enhance your efficiency in website content operation and page customization on Anqi CMS, allowing your website content to be presented to users in the most expected form.


Frequently Asked Questions (FAQ)

1. I used in the template.{{ 变量 }}or{% 标签 %}But it doesn't display any content, or it displays an error message, why is that?

There are usually several possible reasons for this situation:

  • Spelling error in variable name or tag name:The template engine of AnQi CMS is case-sensitive, please carefully check whether the variable name or tag name is consistent with the document or backend definition. For example,{{ Archive.Title }}and{{ archive.title }}it may point to different data.
  • Data was not passed to the template:Some variables or list data need to be explicitly passed to the template from the backend logic in order to use them.If you are customizing the template, make sure that the relevant data has been correctly passed from the backend controller.
  • The variable value is empty:Even if the variable name is correct, if the variable has no value in the current context, it may still display blank during template rendering. You can try using|default:"备用内容"A filter is used to provide a default value, which is convenient for debugging and user experience.
  • The label usage is incorrect:Especially logical control labels (such asifandforThey usually need to appear in pairs and the parameter format must be correct. For example,forAfter the loop, there must be{% endfor %}.

2. I want to display the article content in the template, but HTML tags (like<b>/<p>) are displayed as text directly instead of being parsed by the browser, what should I do?

This is the result of HTML escaping all output content for security reasons by the template engine, aimed at preventing cross-site scripting attacks (XSS). If you are sure that the content to be displayed is safe and you want the browser to parse it as HTML, you can use|safeFilter. For example, to{{ archive.Content }}is modified to{{ archive.Content|safe }}You may. Please ensure that the content source is reliable to avoid introducing security risks.

3. How to use different template layouts on different pages, for example, some pages need sidebars while some do not?

AnQi CMS supports template inheritance and custom template files.

  • Template inheritance:You can create a name calledbase.htmlThe basic template, defining the common structure of the website (such as header, footer), and using{% block 内容区域 %}{% endblock %}mark the variable areas. Then, your other page templates (such asindex.html/detail.htmlPassed{% extends "base.html" %}Inherit it and only overwrite (override) thoseblockarea to define its unique content, thereby achieving different layouts.
  • Custom template file:For specific content models (such as articles, categories, single pages), Anqi CMS allows you to specify different template files in the backend. For example, to set the template for the "About Us" single page,page/about.htmlAs its exclusive template

Related articles

How does AnQi CMS ensure the compliance and security of displayed content?

In website operation, the compliance and security of content is a focus for every webmaster and operator.It not only concerns the reputation and trust of the website, but may also touch upon the legal and regulatory line.As a system dedicated to providing users with an efficient and secure content management experience, AnQiCMS (AnQiCMS) integrated these core demands into its security mechanism from the outset, ensuring the compliance and security of the content we display in multiple dimensions.<h3>Build a Secure Foundation: From System Bottom to Content Interaction</h3> The security of AnQi CMS primarily originates from its solid technical foundation

2025-11-07

How do static caching and SEO optimization enhance the loading speed and visibility of website content?

In today's digital age, whether a website can stand out among numerous competitors, attract and retain users, loading speed and search engine visibility (SEO) are two crucial factors.No one wants to wait for a website that does not respond promptly, and if the content of your website is difficult to be found by search engines, then the best content is of no value.Luckyly, AnQiCMS (AnQiCMS) with its unique architecture and rich features, can effectively help websites achieve double improvement in speed and visibility.

2025-11-07

How does the flexible permission control mechanism ensure precise management of content display by different administrators?

In the field of content management, especially for websites that require multi-team collaboration and refined operations, a flexible and powerful permission control mechanism is indispensable.AnQiCMS (AnQiCMS) provides a comprehensive solution in this aspect, ensuring that different administrators can accurately manage the display of website content, thereby ensuring smooth, efficient, and secure operations.The permission control of AnQi CMS is not as simple as "yes" or "no", but goes deep into the levels of content type, operation behavior, and even system functions, forming a three-dimensional management system

2025-11-07

How does modular design support the secondary development and expansion of content display functions?

In AnQiCMS (AnQiCMS) design philosophy, modularization is one of its core strengths, aiming to provide users with efficient, customizable, and easily scalable content management solutions.When we talk about the secondary development and expansion of content display functions, it is this modular design that plays a key role, allowing users to create highly personalized website experiences according to their own needs without touching the core code.

2025-11-07

How to configure Anqi CMS to support adaptive, code adaptation, or PC+Mobile independent site display mode?

In the current internet environment, it has become a norm for users to browse websites on various devices. How to ensure that your website presents a**good effect on different devices is one of the keys to the success of website operation.AnQiCMS (AnQiCMS) is well-versed in this, providing flexible and diverse display mode configurations, allowing you to easily switch between adaptive websites, code adaptation, or PC+Mobile independent sites according to your actual needs.This article will detail how to configure these display modes in Anqi CMS, ensuring that your content shines on any screen.###

2025-11-07

How to organize directory structure of AnQi CMS template files to manage content display?

For users of AnQiCMS, understanding the organization of template files is the key to efficiently managing website content display.A clear and orderly template directory structure, not only allows you to easily customize the appearance of the website, but also greatly improves the flexibility and efficiency of content operation.The template files of Anqi CMS use `.html` as the suffix and are stored in the `/template` folder under the root directory of the website.And all the style sheets, JavaScript scripts, images, and other static resources used in all templates have their own home

2025-11-07

How to define and use variables in AnQiCMS templates to display dynamic content?

When building a website, we all hope that the content can change flexibly according to different pages and different data, rather than unchanging static text.AnQiCMS leverages its high-performance architecture based on the Go language and a flexible Django-like template engine, providing us with powerful dynamic content display capabilities.And at the core of all this, it cannot be separated from the definition and use of 'variables'. Mastering how to define and use variables in the AnQiCMS template is like mastering the key to activating the dynamic vitality of the website.This is not just a technical operation, but also the realization of content marketing and SEO

2025-11-07

How to flexibly control the display logic of conditional judgment and loop control tags?

Build dynamic and interactive websites in AnQi CMS, it is inseparable from precise control of the content display logic.This involves conditional judgment (`if`) and loop control (`for`) tags playing a core role.They are like a powerful 'combination punch', making the template no longer a static layout, but one that can intelligently respond and flexibly present content based on data changes.The template syntax of AnQi CMS borrows from the Django template engine, its concise and efficient marking method allows even beginners in template development to quickly get started.

2025-11-07