AnQi CMS is an efficient and flexible content management system, and its flexibility in content display is largely due to its support for the Django template engine syntax.For us content operators and website managers, this means we don't need to delve into complex programming languages to organize and present website content as we please, just like building with blocks.
So, what flexibility does the Django template engine syntax bring to our content display?
1. Direct and efficient data calling and display
Firstly, the most direct flexibility is reflected in data calls. The Django template engine uses double braces.{{ 变量 }}To output data, this concise and clear syntax allows us to easily present the background data on the front-end page. Whether it is global information such as website name, contact information (through{% system %}or{% contact %}Tag retrieval), or article title, category description, single-page content, and other specific page data (via{% archiveDetail %}/{% categoryDetail %}/{% pageDetail %}tags), can be directly and accurately called.
For example, to display the article title, just{{ archive.Title }}; To get the website logo image, use{% system with name="SiteLogo" %}Can be done. This design encapsulates complex data query logic in the background, leaving only clear and understandable variable names and tags for the front-end template, which greatly reduces the threshold for content display.
The second, dynamic and flexible content structure and layout control
The powerful aspects of Django template engine also lie in its dynamic control capabilities over content structure and layout.
Conditional judgment and loop traversal (
ifandfortags)The content of the website is not constant. Often, it is necessary to decide whether to display an element or repeat a set of data based on specific conditions.{% if 条件 %}...{% endif %}Tags allow us to easily implement conditional judgments. For example, if a product article does not have a thumbnail, we do not display the image area;If the user is a VIP, display specific paid content.{% for item in list %}...{% endfor %}Tags are very suitable for handling list data, such as traversing article lists, product categories, navigation menus, etc. Combinedforloop.Counter(Current loop count) and built-in variables, we can even add different styles or logic to specific items in the list, achieving needs such as 'special style for the first article'.
Template inheritance and componentization(
extends/includeandmacrotags)To maintain the consistency of the website style and improve development efficiency and maintenance convenience, the reuse of templates is indispensable.{% extends 'base.html' %}Tags allow us to define a basic layout (like a PowerPoint master), containing the common parts of a website such as the header, footer, and sidebar, then other pages only need to inherit this basic layout and rewrite the{% block content %}...{% endblock %})Specific area content. This ensures consistency of the page structure while allowing flexible modification of the local area.{% include "partial/header.html" %}Tags can isolate small code snippets (such as navigation bars, sidebar ad slots) into separate files and then import them where needed.This can reduce duplicate code and facilitate sharing components between multiple pages.{% macro %}Tags take it a step further, allowing us to create reusable code blocks or functions, pass parameters to call them, and are particularly suitable for content fragments with similar structures but different data, further enhancing the modularization of templates.
Seamless integration of custom content model fieldsOne of AnQi CMS' advantages is that it supports flexible content models and custom fields. The Django template engine syntax perfectly supports this. Whether it is through
{% archiveParams %}Label whether to loop through all the additional parameters of the document or directly through{{ archive.自定义字段名 }}The way to accurately call a custom field (such as the "price" field of a product model), allows us to flexibly display various structured information according to the definition of the content model, meeting the personalized needs of different business scenarios.
Three, refined content processing and optimization
In addition to structural control, template engines also provide a rich set of filters (Filters) for fine-grained content processing, allowing data to be transformed and formatted before display:
Data formatting and conversion
- Date and time:
{{ stampToDate(时间戳, "格式") }}Unix timestamps can be formatted into any date-time format we want, such as “October 26, 2023, 15:30”. - Text processing:
truncatecharsCan truncate strings and add an ellipsis (often used in article summaries),safeThe filter can safely output HTML content (such as rich text in the article body), preventing XSS attacks. In addition, there is alsolower/upper(Case conversion),replace(text replacement),splitSplitting strings and more, able to meet various text display needs. - URL and links:
urlizeThe filter can automatically identify URLs in text and convert them into clickable hyperlinks, which is very suitable for processing user input content. - Markdown rendering:If the background content is written in Markdown format,
renderthe filter can render it directly into HTML without manual conversion.
- Date and time:
Dynamic generation of SEO elementsSEO is crucial for websites. The Django template of Anqi CMS can
{% tdk %}generate the page dynamically with tags.<title>/<meta name="keywords">and<meta name="description">. It is more flexible, allowing us to control whether to append the website name in the title and whether to display the title of the parent category. These detailed settings are very helpful for search engine optimization. At the same time,CanonicalUrlThe label ensures the correct setting of the standard link, avoiding duplicate content issues.Multilingual and multi-site adaptationOur CMS supports multi-site and multi-language management, and the template engine also provides the corresponding flexibility.
{% languages %}Tags can conveniently generate language switch menus, and{% tr "关键词" %}Translation tags can be used to dynamically switch between multilingual content in templates, combinedsiteIdwith parameters, and even call data from different sites within the same template.
Four, highly customized page structure
The AnQi CMS also provides the ability to customize template files for specific categories, articles, or single-page applications.This means we can create a unique list page template for the "News Center", design a dedicated display layout for "Product Details", and even specify a completely independent style for a specific single page like "About Us".This file name matching mechanism (such as{模型table}/list-{分类id}.htmlorpage/{单页面id}.htmlThe combination with Django template has given us unprecedented freedom in page structure customization, allowing each page to showcase a unique visual and interactive experience based on its content characteristics and business requirements.
In summary, the Django template engine syntax of Anqi CMS provides a powerful and intuitive way to control the display of website content. It not only simplifies data calls but also, through conditional logic, component reuse, fine-grained processing, and highly customized page structures, gives us unlimited flexibility, allowing website operators to build, optimize, and manage content more efficiently, ultimately improving user experience and the website