Manage and display website content in the Anqi CMS, flexibly using template variables is undoubtedly the key to improving efficiency and maintaining website consistency.Whether it is to unify the adjustment of the entire station information 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 the Aanqi CMS, many of the information we see daily on the website is actually managed through pre-set variables.This means you can easily define and modify these contents without touching a single line of code.
Firstly, the website'sGlobal SettingsandContact information settingsThis is the perfect place to define global variables.比如,You can set 'Website Name', 'Website Logo', 'Record Number' and other information in the 'Global Feature Settings'. Once defined, this information can be called on any page of the website, and when you need to modify it, you only need to update it once in the background, and the content of the entire website will be synchronized updated, which greatly ensures the consistency of the website information.It is more interesting that both modules provide the "Custom Settings Parameters" feature.This means you can add any global variables you think are useful according to your specific needs, such as setting a unified 'Help Page Link' for your website or a specific 'Customer Service Hours', these custom parameters can also be easily reused in templates.
Secondly, for the rich and diverse content on the website, Anqi CMS providesFlexible Content Model.This 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 number', 'price', 'stock', and so on, in addition to the title and content.These custom fields defined in the 'Content Model Usage Help' become unique variables for all content under the model.When you publish specific articles or products in the background, these fields are filled and stored as attributes of the content itself, waiting to be called and displayed on the frontend.
In addition,Categoryandsingle pageAlso 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 summary' for the single page 'About Us'.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 the template
In addition to setting global or content-related variables in advance, 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 label allows you to declare and assign a variable directly in the template.This variable's lifecycle is limited to the current template file and its sub-templates, which is very suitable for storing some computation 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 part of a longer string as a new variable.{% set newVariable = existingVariable|filter:param %}This style allows you to easily define these temporary variable definitions.
while{% with %}Labels provide a way to define variables with scope. It is often used with{% include %}Label配合使用,以便在引入某个代码片段时,向该片段传递特定的数据,而不影响其他部分的变量。For example, your website has a universal header template, but on some specific pages, you want the header title to be different.{% with title="自定义标题" %}{% include "partial/header.html" %}{% endwith %}The way to 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 on the website front end.The template engine of Anqi CMS (similar to Django template engine syntax) provides various intuitive calling methods.
The most direct way to call is to use double curly braces syntax:{{ variable.property }}. If an object is already included in your template context (for example, on an article detail page,archiveThe object usually contains all the information of the current article), then you can directly display it{{ archive.Title }}to show the article title,{{ archive.Description }}To display the article summary. For custom fields defined in the content model that are not in the form of an array or complex structure, they can also be accessed directly through{{ archive.您的自定义字段名 }}to get.
Moreover, the CMS also provides many special tags for us to obtain specific types of variables:
{% system %}tags: used to obtain various information from the global settings, such as{% system with name="SiteName" %}Show website name, or call the customized one you set in the backgroundHelpUrletc.{% contact %}tags: Specifically get the information in the contact information settings, such as{% contact with name="Cellphone" %}Show the contact phone number. Similarly, the parameters you customize in the contact information can also be obtained through this tag.{% tdk %}tags: Used to obtain the SEO information of the page,{% tdk with name="Title" %}Can get the title of the page.{% archiveDetail %}/{% categoryDetail %}/{% pageDetail %}tagsThese tags are used to retrieve detailed information about the current document, category, or single page.name="字段名称"The parameter allows you to accurately retrieve specific data such as document title, category description, page content, etc. This label also applies when you need to retrieve custom fields in the content model, for example,{% archiveDetail with name="price" %}.{% archiveParams %}tagsIf you need to iterate over all custom parameters under a document instead of calling just one of them,{% archiveParams %}Labels are very useful. It will return all custom parameters as an array object, and you can iterate over them to display their names and values.{% 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 %}condition judgment tags. For example, when traversing the article list, you can{% for item in archives %}show each article's title and link 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.
at last, never ignoreFiltersThe 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" }}you can format timestamps into readable dates;{{ item.Description|truncatechars:100 }}Can truncate long descriptions and automatically add ellipses;{{ archiveContent|safe }}Ensures that HTML tags in rich text content are parsed correctly and not displayed as plain text.addThe filter can even add numbers or strings, 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.
Common 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?
Background-defined variables (such as global settings, content model custom fields) are persistently stored in the database, and they can be reused by different pages and modules across the entire site, and can be easily managed and modified through the backend interface without touching the code.This applies to the basic information of the website, core content attributes, and so on.{% set %}or{% with %}
2. Where can I view all available template variables and their properties?
The document of Anqi CMS provides a detailed section on “template call tags”, you can find inindex.md-u003e “Template call tag” under find suchtag-system.md(System tag)、tag-contact.md(Contact information tag}