In website operation, we often need to finely control the display of page content, especially for global information that needs to be repeated at various places on the website, such as the website name, footer copyright, etc.AnQiCMS provides powerful template tags and filters to help us flexibly handle these requirements.Today, let's talk about a very practical scenario: AnQi CMS'saddCan the filter be used to concatenate fromsystemThe website name and footer copyright information obtained by the tag?
The answer is affirmative, and this method is both intuitive and efficient. Next, we will delve into how to implement this operation and understand its underlying principles and practical applications.
UnderstandsystemLabel: Source of Global Information for the Website
In AnQi CMS,systemThe tag is a key tool for obtaining the global settings of a website. Through it, we can easily access various core data of the background configuration, such as the website name (SiteName)、website logo (SiteLogo) Registration Number (SiteIcp) Footer Copyright Information (SiteCopyright) as well as the basic URL of the website (BaseUrl) and so on. These information are usually returned as strings and are the basis for constructing dynamic page content.
For example, to obtain the website name and copyright information, we usually do it like thissystemTags:
{% system websiteName with name="SiteName" %}
{% system siteCopyright with name="SiteCopyright" %}
Here, websiteNameandsiteCopyrightThe website name and footer copyright content are stored separately.
RevelationaddFilter: The tool for string concatenation
The template engine of AnQi CMS supports various filters,addFilters are one of the very practical ones. According to the documentation,addThe filter can not only be used for addition operations on numbers, but also be competent for string concatenation tasks.This means, we can concatenate multiple string fragments to form a complete string.
addThe basic usage method of filters is{{ obj|add:obj2 }}. Among them,objis the original data,obj2The data to be added or concatenated. Whether it is a number or a string,addThe filter will try to process accordingly:
{{ 5|add:2 }}It will output7.{{ "安企"|add:"CMS" }}It will output安企CMS.
It is this flexible string concatenation capability that provides the possibility for us to combine the website name and copyright information.
Practice exercise: Concatenating the website name and copyright information
SincesystemThe tag can get the string,addThe filter can concatenate strings, so using both of them naturally leads to a smooth transition.
Suppose we want to display a formatted copyright statement at the footer of the website, for example: [网站名称] - [版权内容]We can implement this in the template:
{# 首先,获取网站名称和版权信息 #}
{% set currentSiteName = "" %}
{% system currentSiteName with name="SiteName" %}
{% set currentSiteCopyright = "" %}
{% system currentSiteCopyright with name="SiteCopyright" %}
{# 使用 add 过滤器拼接字符串,并添加分隔符 #}
<p>
{{ currentSiteName|add:" - "|add:currentSiteCopyright|safe }}
</p>
In this code, we first use{% set ... %}statements to combinesystemThe tag, stores the obtained website name and copyright content separately intocurrentSiteNameandcurrentSiteCopyrightthese two variables.
Then, in<p>In the tag, we cleverly utilizedaddfilter.currentSiteNameConcatenate with a fixed string separator" - "Concatenate, then concatenate the result again withcurrentSiteCopyrightPerform the second concatenation.
It should be especially noted that the last|safeFilter. Due to the footer copyright information (SiteCopyright) May contain HTML tags (for example©symbols, links, etc.), which may be escaped as plain text by the template engine.|safeThe filter's role is to inform the template engine that this content is safe, and can be directly output as HTML, ensuring that the copyright information can be displayed correctly with its style and links.
In this way, we can flexibly combine various texts, for example, you may want to add it at the beginning©Symbol:
<p>
{{ currentSiteName|add:" © "|add:currentSiteCopyright|safe }}
</p>
When will such splicing be used?
ThisaddFilter combinationsystemThe skill of combining tags, its application scenarios are not limited to the footer:
- Dynamic page title (
<title>tags): Combine the article title and website name to form a more SEO-friendly title, such as{{ archive.Title|add:" - "|add:currentSiteName }}. - SEO description (
<meta name="description">): Combine article summary, custom keywords, and website brand name to generate a more detailed description. - Social media sharing copywriting:Generate a dynamic text with website information and content summary for sharing links.
- Multilingual site displayAccording to the name and copyright information of different language sites, generate combined content for the corresponding language.
- Dynamic prompt information within the pageDisplay 'Welcome to visit' in a corner of the page.
[网站名称]Personalized content.
By mastering this simple yet powerful technique, you will be able to control the text output of Anqi CMS templates more flexibly, enhancing the dynamic and personalized experience of website content.
Frequently Asked Questions (FAQ)
1. In addition toaddFilter, does AQCMS have other methods to concatenate strings?
The template engine of Anqi CMS is based on Django template syntax, which itself supports outputting string variables and literal strings together without the need for additional concatenation characters, for example{{ websiteName }} - {{ siteCopyright }}ButaddThe filter provides a clearer chain-style concatenation method, making the logic clearer for scenarios where multiple fixed strings need to be inserted in the middle or mixed with other types (such as numbers).
2. IfsystemDoes the content obtained by the tag contain HTML tags, will there be any problems if it is concatenated directly?
Yes, ifsystemHow does the tag getSiteCopyrightThe content itself contains HTML tags (for example©/<a>Tags like, without|safeUnder the filter, the template engine will, for security reasons, default to escaping these HTML tags, causing them to be displayed as plain text instead of parseable HTML elements. Therefore, when you are sure that the content is safe and needs to parse HTML, you must use|safeFilter, for example{{ combined_string|safe }}.
3.addCan the filter concatenate numbers and strings at the same time? For example, adding a year to the website name?
Absolutely.addThe filter is very flexible, it can intelligently handle the mixing of numbers and string concatenation. If you want to concatenate the website name with the current year, for example: “AnQi CMS 2024”, you can first get the current year, and then concatenate it like this:
{% now currentYear "2006" %} {# now标签获取当前年份,2006是格式化模板 #}
{% set siteName = "" %}
{% system siteName with name="SiteName" %}
<p>{{ siteName|add:" "|add:currentYear|safe }}</p>
This will output an effect similar to 'Anqi CMS 2024'.addThe filter will ignore the content to be added when the conversion fails, so you don't have to worry about errors caused by type mismatch.