How to define template variables and reuse them in content display?

Calendar 👁️ 69

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

Related articles

How to format a timestamp into a readable date and time string for display?

In Anqi CMS, formatting timestamps into readable dates: A practical guide When managing website content, we often encounter the need to display article publishing time, update time, and user registration time information.These data are usually stored in the database in the form of a string of numbers - that is, a 'timestamp'.Although computers can easily understand these numbers, they may not seem so friendly to users visiting websites.For example, seeing a number like '1678886400' is not as intuitive as '2023 March 15 10:00:00'

2025-11-08

How to use a loop (for label) to traverse and display a content list?

In Anqi CMS, whether it is displaying article lists, product catalogs, navigation menus, or friend links, we will frequently deal with content lists.To dynamically display this content on the website front-end, you can't do without powerful looping functions.Today, let's delve deeper into how to use the loop (`for` tag) in Anqi CMS templates to traverse and display your content list. ### The core syntax of the `for` tag: Bring content to life The Anqi CMS template syntax is similar to Django and Blade, very intuitive.When you need to handle a set of data

2025-11-08

How to dynamically display different content blocks based on conditions (if tag)?

In Anqi CMS, the dynamic display of website content is the key to improving user experience and operational efficiency.You often encounter such needs: you want a certain content block to appear only under specific conditions, or to display different information for different situations.At this point, the powerful conditional judgment feature of Anqi CMS - the `if` tag, can be put to good use.The Anqi CMS template system adopts a syntax similar to the Django template engine, allowing you to implement logical judgments in templates in a straightforward manner.By flexibly using the `if` tag, you can easily control the display and hide of web elements

2025-11-08

How to display the background custom banner (Banner) image list in the template?

The banner image on the website is an important element to attract visitors' attention and display core information.In Anqi CMS, you can flexibly customize these banner images in the background and elegantly present them in various template positions on the website.Below, we will discuss in detail how to display the custom banner image list in the template, making your website more attractive. ### Configuration and Management of Background Banner Images In the Anqi CMS backend, the management of banner images is mainly divided into two cases: global homepage banners and specific content banners. 1.

2025-11-08

How does Anqi CMS manage and display uploaded images and video resources?

When using Anqi CMS to manage website content, images and video resources are undoubtedly the key to enriching the page and improving the user experience.AnQi CMS provides a set of intuitive and powerful functions to help us efficiently upload, manage, and display these multimedia contents. ### Easy upload and centralized management of resources When creating articles, products, or single-page content, images and videos can be directly inserted into the content editing area.This process is very smooth, just like using a document editor in everyday life.In addition to this immediate insertion method, Anqi CMS also has a dedicated "image resource management" area

2025-11-08

How do static rules affect the display form and SEO-friendliness of website URLs?

## Static rule of AnQi CMS: How to create a URL structure that users and search engines both love In the operation of a website, the URL is not only the path for users to access the page, but also an important clue for search engines to understand and evaluate the content of the website.A clear, concise, and meaningful URL that greatly enhances user experience and website SEO performance.AnQi CMS knows this, and therefore provides a flexible and diverse static rule management function to help us easily create an ideal URL structure.What is pseudo-static and why is it so important

2025-11-08

How does the automatic compression of images and the handling of large images in content settings affect page display performance?

In website operation, images are indispensable elements that enrich content and attract users.However, if image processing is not handled properly, the large file size often becomes the culprit that slows down page loading speed, thereby affecting user experience and search engine optimization (SEO) performance.AnQiCMS provides a series of powerful image processing features in its content settings, aimed at helping us easily optimize images and significantly improve the display performance of the website.When a visitor opens a page, the browser needs to download all the resources on the page, including text, styles, scripts, and images.If the image file is too large

2025-11-08

How to display the current year or other system time information in the template?

In website operation, keeping your content up-to-date in real-time, especially information like the year or date of system time, can not only enhance the professionalism of the website but also provide users with more accurate references.AnQiCMS as a flexible and efficient content management system provides a variety of convenient ways for you to easily implement this requirement. Next, let's learn how to flexibly display the current year or other system time information in the AnQiCMS template.### Display the current system time——`{% now %}`

2025-11-08