安企CMS as an efficient and flexible content management system, its flexibility in content display is largely due to its support for Django template engine syntax.For those of us who are content operators and website managers, this means that we don't need to deeply learn complex programming languages, and we can organize and present website content as we wish, just like building blocks.
Then, what flexibility does the Django template engine syntax bring to our content display?
1. Intuitive and efficient data calling and display
Firstly, the most direct flexibility is reflected in data calls. Django template engine uses double curly braces{{ 变量 }}Output the data, this concise and clear syntax allows us to easily display the background data on the front-end page. Whether it is global information such as website name, contact information (via{% system %}or{% contact %}Label acquisition), or article titles, category descriptions, and single-page content of specific pages (via{% archiveDetail %}/{% categoryDetail %}/{% pageDetail %}such 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" %}Could be done. This design encapsulates complex data query logic in the background, leaving only clear and understandable variable names and labels for the front-end template, which greatly reduces the threshold for content display.
二、English flexible content structure and layout control
The powerful aspects of the Django template engine are also reflected in its dynamic control capabilities over content structure and layout.
Conditional judgment and loop traversal (
ifandforLabel)The content of the website is not immutable. Often, it is necessary to decide whether to display a certain 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 will not display the image area; if the user is a VIP, we will display specific paid content.{% for item in list %}...{% endfor %}Tags are very suitable for processing list data, such as traversing article lists, product categories, navigation menus, etc. Combinedforloop.CounterThe current loop count) built-in variables, we can even add different styles or logic to specific items in the list, to achieve needs such as 'The first article has special style'.
Template inheritance and componentization(
extends/includeandmacroLabel)To maintain consistency in the website style and improve development efficiency and maintenance convenience, template reuse is indispensable.{% extends 'base.html' %}Labels allow us to define a basic layout (similar to a PowerPoint master), including common parts of the website such as headers, footers, and sidebars, and then other pages can simply inherit this basic layout and overwrite it ({% block content %}...{% endblock %}Content specific to an area. This ensures the consistency of the page structure while allowing flexible modification of the local area.{% include "partial/header.html" %}Tags can extract 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 %}The tag goes further, allowing us to create reusable code blocks or functions, and pass parameters for invocation. It is particularly suitable for content fragments that have similar structures but different data, further enhancing the modularization of the template.
The seamless integration of custom content model fieldsOne of the major advantages of AnQi CMS is that it supports flexible content models and customizable fields. The Django template engine syntax perfectly supports this. Whether through
{% archiveParams %}Label loops through all the additional parameters of the document or goes 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
除了结构控制,模板引擎还在内容的精细化处理上提供了丰富的过滤器(Filters),让数据在展示前可以进行各种转换和格式化:
Data formatting and conversion
- Date and time:
{{ stampToDate(时间戳, "格式") }}Unix timestamp can be formatted into any date and time format we want, for example “October 26, 2023 15:30”. - Text processing:
truncatecharsCan truncate a string and add 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,lower/upper(case conversion,)replace(text replacement,)split(Split string) Etc., which can 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 handling 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 the website. The Django template of Anqi CMS allows
{% tdk %}tags to dynamically generate the page.<title>/<meta name="keywords">and<meta name="description">.It is more flexible, allowing us to control whether to attach the website name to the title and whether to display the title of the parent category. These detailed settings are very helpful for search engine optimization.CanonicalUrlThe label also ensures the correct setting of standard links, avoiding issues with duplicate content.Multilingual and multi-site compatibilityAuto CMS supports multi-site and multi-language management, and the template engine also provides corresponding flexibility.
{% languages %}Tags can conveniently generate language switching menus, and{% tr "关键词" %}Translate the label can enable the dynamic switch of multi-language content in the template, combinedsiteIdParameters, even you can call data from different sites in the same set of templates.
English, highly customized page structure
Auto 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 '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'.{模型table}/list-{分类id}.htmlorpage/{单页面id}.htmlThe combination with Django templates gives us unprecedented freedom in customizing page structure, allowing each page to display a unique visual and interactive experience based on its content characteristics and business requirements.
In summary, the Django template engine syntax provided by AnQi CMS offers a powerful and intuitive way to control the display of website content. It not only simplifies data retrieval but also provides unlimited flexibility through conditional logic, component reuse, fine-grained processing, and highly customizable page structures, allowing website operators to build, optimize, and manage content more efficiently, ultimately enhancing user experience and website performance.