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:
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.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 as
ifandforThey 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 called
base.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