In website operation, we often need to finely control the display of page content, especially for those global information that needs to appear repeatedly in various parts of the website, such as the website name, footer copyright, etc.AutoCMS (AutoCMS) provides powerful template tags and filters, helping us flexibly handle these needs.addFilter can be used to concatenate fromsystemthe website name and footer copyright information obtained by tags?
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.
Understandsystem标签:Website global information source
In the Anqi CMS,systemLabels are the key tools for obtaining global settings of the website. Through it, we can easily access various core data of background configuration, such as website name (SiteName)、website logo(SiteLogo)、备案号(SiteIcp)、Footer copyright information(SiteCopyright)and the basic URL of the website(BaseUrl)etc. These information are usually returned as strings and are the foundation for building dynamic page content.
For example, to obtain the website name and copyright information, we usually use it like thissystemTags:
{% system websiteName with name="SiteName" %}
{% system siteCopyright with name="SiteCopyright" %}
Here,websiteNameandsiteCopyrightThey store the website name and footer copyright content separately.
UnveilingaddFilter: a powerful tool for string concatenation
The template engine of AnQi CMS supports various filters.addFilters are one of the very practical ones.addThe filter can not only be used for arithmetic operations on numbers, but also be competent for string concatenation tasks.This means that we can concatenate multiple string fragments to form a complete string.
addThe basic usage method of filters is{{ obj|add:obj2 }}.objis the original data,obj2Is it to add or concatenate data. Whether it is a number or a string,addThe filter will attempt to process accordingly:
{{ 5|add:2 }}it will output7.{{ "安企"|add:"CMS" }}it will output安企CMS.
It is this flexible string concatenation ability that provides the possibility for us to combine the website name and copyright information.
Practical Exercise: Concatenating Website Name and Copyright Information
SincesystemTags can obtain strings,addThe filter can concatenate strings, so using both naturally leads to a smooth process.
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 ... %}Sentence combinationsystemThe tag stores the obtained website name and copyright content separately into:currentSiteNameandcurrentSiteCopyrightthese two variables.
Then, in<p>We巧妙地运用了 inside the tag,addFilter.currentSiteNameFirstly, separate with a fixed delimiter string." - "Concatenate, then, concatenate the result again with.currentSiteCopyrightPerform the second concatenation.
It should be especially noted that the final.|safeFilter due to the footer copyright information (SiteCopyright)In the background configuration, it may contain HTML tags (such as©symbols, links, etc.), which may be escaped as plain text by the template engine.|safeThe role of the filter is to inform the template engine that this content is safe and can be directly output as HTML, thereby ensuring that the copyright information can be displayed correctly in terms of style and links.
In this way, we can flexibly combine various texts, such as adding something at the beginning if you want©symbols:
<p>
{{ currentSiteName|add:" © "|add:currentSiteCopyright|safe }}
</p>
When would we use such concatenation?
Thisaddfilter combined withsystemLabel concatenation techniques, their application scenarios are not limited to the footer:
- Dynamic page title (
<title>Label): 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:To generate a dynamic copy containing website information and content summary for sharing links.
- Multilingual site displayEnglish according to the name and copyright information of different language sites, generate corresponding language combination content.
- Dynamic prompt information within the page: Display 'Welcome to visit' at a corner of the page
[网站名称]English content.
By mastering this simple yet powerful technique, you will be able to control the text output in the security CMS template more flexibly, enhancing the dynamism and personalized experience of website content.
Common Questions (FAQ)
1. BesidesaddFilter, does the Anqi CMS have other methods to concatenate strings?
The template engine of Anqi CMS is based on Django template syntax, which inherently supports combining string variables and literal strings for output without the need for additional concatenation symbols, for example,{{ websiteName }} - {{ siteCopyright }}.addThe filter provides a clearer way of chaining concatenation, making the logic clearer when multiple fixed strings need to be inserted in the middle or mixed with other types (such as numbers).
2. IfsystemLabel content includes HTML tags, will there be any problems if directly concatenated?
Yes, ifsystemThe tags obtained likeSiteCopyrightThe content itself contains HTML tags (for example©/<a>Label, etc.), without|safeIn the case of filters, the template engine will default to escaping these HTML tags for security reasons, 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, be sure to 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 a website name?
Absolutely.addThe filter is very flexible, it can intelligently handle the mixing and matching of numbers and strings. 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 something like 'AnqiCMS 2024'.addThe filter will ignore the content to be added if the conversion fails, so you don't have to worry about errors caused by type mismatch.