In AnQi CMS, managing and displaying website content, flexibly using template variables is undoubtedly the key to improving efficiency and maintaining website consistency.Whether it is to unify the information of the entire site or to customize unique attributes for specific content, understanding how to define and reuse template variables can make content operation more skillful.
Define variables from the background source
In Anqi CMS, many of the information we see on the website every day is actually managed through backend preset variables.This means you can easily define and modify these contents without touching a single line of code.
First, the website'sGlobal SettingsandContact information settingsThis is the perfect place to define global variables. For example, you can set 'Website Name', 'Website Logo', 'ICP Number', and other information in the 'Global Function Settings'. Once defined, this information can be called from any page on the website, and when you need to modify it, you only need to update it once in the background, and the entire site content will be synchronized updated, which greatly ensures the consistency of website information.It is even more interesting that both modules provide a "custom setting parameter" feature.This means you can add any global variables that you think are useful for your specific needs, such as setting a unified 'Help Page link' or a specific 'Customer Service Hours', these custom parameters can also be conveniently reused in templates.
Secondly, for the rich and diverse content on the website, Anqi CMS providesFlexible content modelThis means you can define unique fields for different types of content (such as articles, products, events, etc.).For example, a "product model" may need additional attributes such as "product model", "price", "inventory", etc., in addition to the title and content.These are the custom fields defined in the "Content Model Usage Help", which become unique variables for all content under the model.When you publish specific articles or products in the background, these fields will be filled and stored as attributes of the content itself, waiting to be called and displayed on the front end.
Furthermore,Categoryandsingle pageIt also supports defining some specific variables. For example, you can set a dedicated "Banner image" for a product category, or add a unique "category content" or "page introduction" for the "About Us" single page.These settings also expand the range of variables we can use, making it easier to personalize the display of the page.
Flexible definition of temporary variables in templates
In addition to setting global or content-related variables in the background, when writing template files, we also have the ability to define some temporary variables as needed to meet more complex logic or simplify the code.
{% set %}The tag allows you to declare and assign a variable directly in the template.The lifetime of this variable is limited to the current template file and its sub-templates, which is very suitable for storing some calculation results or temporary strings.For example, you may need to add two numbers and then store the result in a variable for later use;or extract a portion of a longer string as a new variable. Through{% set newVariable = existingVariable|filter:param %}Such syntax allows you to easily define these temporary variable declarations.
And{% with %}Labels provide a way to define variables with a scope. It is often used with{% include %}Tags are used together to pass specific data to a code snippet when introducing it, without affecting the variables in other parts.For example, your website has a generic header template, but on some specific pages, you want the header title to be different.Now, you can use{% with title="自定义标题" %}{% include "partial/header.html" %}{% endwith %}The way, temporarily override the title variable when introducing the header, thereby realizing the dynamic display of content.
Reuse these variables in content display.
Defined a variable, the next most critical thing is how to effectively reuse them in the content display of the website front-end.The AnQi CMS template engine (similar to Django template engine syntax) provides a variety of intuitive calling methods.
The most direct way to call is to use double curly bracket syntax:{{ variable.property }}. If your template context already contains an object (for example, on an article detail page,archiveAn object usually contains all the information of the current article, then you can directly go through{{ archive.Title }}to display the article title,{{ archive.Description }}To display the article summary. For custom fields defined in the content model, if they are not in the form of an array or complex structure, they can also be accessed directly by{{ archive.您的自定义字段名 }}Get it.
Moreover, AnQi CMS also provides us with many dedicated tags to retrieve specific types of variables:
{% system %}Tag: to retrieve various types of information from the global settings, such as{% system with name="SiteName" %}To display the website name or call the custom one in the backgroundHelpUrletc.{% contact %}Tag: Specifically to retrieve information from the contact information settings, such as{% contact with name="Cellphone" %}To display the contact phone number. Similarly, the parameters you customize in the contact information can also be obtained through this tag.{% tdk %}Tag: Used to obtain page SEO information such as{% tdk with name="Title" %}Can get the page title.{% archiveDetail %}/{% categoryDetail %}/{% pageDetail %}TagThese tags are used to retrieve the details of the current document, category, or single page. Byname="字段名称"The parameter, you can accurately obtain specific data such as document titles, category descriptions, page content, etc. This label is also applicable when you need to retrieve custom fields in the content model, for example{% archiveDetail with name="price" %}.{% archiveParams %}TagIf you need to iterate over all custom parameters under a document instead of calling just one of them,{% archiveParams %}Tags are very useful. It will return all custom parameters as an array object, and you can{% for item in params %}loop through them one by one to display their names and values.
Of course, when reusing these variables, we often combine{% for %}loop and{% if %}conditional tags. For example, when traversing the article list, you can use{% for item in archives %}Display the title and link of each article one by one; when displaying images, you can use{% if item.Thumb %}<img src="{{ item.Thumb }}" />{% endif %}to judge whether the thumbnail exists to avoid displaying blank images.
Finally, do not ignore it at all.Filter (Filters)The powerful function. Filters can perform various transformations and processing on variables, making the display more in line with your needs. For example, using{{ item.CreatedTime|stampToDate:"2006-01-02" }}timestamp can be formatted into a readable date;{{ item.Description|truncatechars:100 }}Can truncate long descriptions and automatically add ellipses;{{ archiveContent|safe }}Then it can ensure that HTML tags in rich text content are parsed correctly and not displayed as plain text.addThe filter can even add numbers or strings together, allowing simple calculations to be performed directly in the template.
By cleverly defining and reusing these template variables, your security CMS website will have higher maintenance efficiency, better content consistency, and can easily achieve more dynamic and personalized display effects.
Frequently Asked Questions (FAQ)
1. Why do I sometimes need to define variables in the background, and sometimes use them directly in the template?{% set %}or{% with %}? What are the differences?
Backend-defined variables (such as global settings, custom fields of content models) are persistently stored in the database, and they can be reused across different pages and modules on the entire site, making it convenient to manage and modify them through the backend interface without touching the code.This applies to the basic information of the website, core content attributes, etc.{% set %}or{% with %}These are temporary variables defined in the template file, which are only effective during the current request template rendering process, not stored, mainly used to simplify complex logic within the template, pass parameters to the imported code snippets, or store calculation results.The choice of method depends on the scope, lifecycle of the variable, and whether it is necessary to manage through the backend interface.
2. Where can I view all available template variables and their properties?
The Anqi CMS documentation provides a detailed section on 'template invocation tags', you can findindex.md-> "Template call tag" can be found such astag-system.md(System tag),tag-contact.md(Contact information tag