As an experienced website operations expert, I have accumulated rich experience in the practical application of AnQiCMS, especially deeply understanding the importance of content flexibility to the vitality of a website. Today, let's delve into a question that many AnQiCMS users have been curious about:{% diy %}What is the custom value obtained from the label, can it be further processed in the template filter?
In short, the answer is affirmative, and this ability greatly expands the imagination space for AnQiCMS template customization and content operation. Custom values through{% diy %}After the label is extracted, it is not only displayed as is. They can be completely like other variables, under the powerful influence of template filters, to perform various formatting, truncation, conversion, or calculation operations, thus achieving more refined and intelligent content presentation.
{% diy %}Tag: Bridge for Custom Backend Content
First, let's take a look back at{% diy %}The role of tags. In AnQiCMS,{% diy %}It stands for 'Do It Yourself' abbreviation and is a very practical label that allows us to flexibly create and manage some non-structured but frequently used custom information from places like 'Global Feature Settings' or 'Contact Information Settings' in the background. For example, a website's customer service WeChat ID, company slogan, annual copyright year, or even a temporary notification text can be created and managed through it.{% diy %}labels to{% diy with name="字段名称" %}The form is presented easily on the website frontend.
When we go through{% diy with name="Author" %}When obtaining a custom parameter value named 'Author', this value is like a common string or numeric variable in a template, waiting to be further 'refined'.
Template filter: A powerful assistant for content processing
AnQiCMS's template engine supports a rich set of filter (Filters) mechanisms, which are powerful tools for processing variable output. The usage of filters is typically to add a pipe symbol after a variable|Followed by the filter name and optional parameters, for example{{ obj|filter_name:param }}These filters can perform various operations on data types such as strings, numbers, and arrays, such as
- String processingExtract characters, convert case, delete specified characters, replace content, etc.
- Number processingAddition, subtraction, multiplication, division, formatting floating-point numbers, etc.
- HTML/URL processing:Escape HTML, recognize URLs and generate links, URL encoding, etc.
- Logical judgment:Check length, check containment, etc.
It is these powerful filters that give{% diy %}The value obtained from the tag has endless possibilities.
{% diy %}The value and the filter join forces.
Now, let's take a specific look.{% diy %}How to shine in the filter with the obtained custom value. Suppose we have customized the following parameters in the background "Global Function Settings":
- Parameter name:
CompanySloganParameter value:AnQiCMS:让你的网站更安全,运营更高效! - Parameter name:
FoundedYearParameter value:2021 - Parameter name:
SupportEmailParameter value:[email protected]
1. Handle the company slogan (CompanySlogan)
We may wish to display the full slogan on the homepage, but only a simplified version in the footer or other locations, while maintaining a specific format.
Full display:
<div>我们的口号:{% diy with name="CompanySlogan" %}</div>Output:
我们的口号:AnQiCMS:让你的网站更安全,运营更高效!Extract and simplify to uppercase:
{%- diy sloganValue with name="CompanySlogan" %} <p>精简口号:{{ sloganValue|truncatechars:15|upper }}</p>Here, we first assign
{% diy %}tosloganValuevariable, and then applied in a chain styletruncatechars(Extract characters, parameter is 15) andupperTwo filters. Output:精简口号:ANQICMS:让你...
2. Handle the company's founding yearFoundedYear)
Assume we want to dynamically display 'Copyright © 2021-Current Year' in the copyright information.
- Calculate the copyright year:
Here,{%- diy startYear with name="FoundedYear" %} <p>版权所有 © {{ startYear }}-{% now "2006" %}</p>FoundedYearis a number,{% now "2006" %}Get the current year. Although there is no direct access tostartYearPerform arithmetic operations using a filter, but its numeric type allows it to be smoothly concatenated with text, demonstrating the combination of custom values with other dynamic content. If a numerical calculation is required, such as{{ startYear|add:1 }}Also feasible. Output (assuming the current year is 2024):版权所有 © 2021-2024
3. Handling supported email addresses (SupportEmail)
IfSupportEmailCustom value is an email address, and we hope it can be automatically converted into a clickable link.
- Automatically convert to a link:
{%- diy emailValue with name="SupportEmail" %} <p>联系我们:{{ emailValue|urlize|safe }}</p>urlizeThe filter automatically identifies URLs or email addresses in text and wraps them.<a>In tags, whilesafeThe filter ensures that HTML code is not escaped. Output:联系我们:<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>
It is not difficult to see from these examples that,{% diy %}Label gets the custom value, perfectly integrates into the AnQiCMS template filter ecosystem.This brings great convenience to website operators and template developers, without the need to modify backend code, just by adjusting the front-end template, it can achieve rich display and flexible control of content.
the extension of practical application scenarios
The strength of this combination lies in its wide range of application scenarios:
- Dynamic navigation text: Utilizing
{% diy %}Define the display text of a navigation item, thenupperorlowerunify the filter format. - SEO keyword optimization: Through
{% diy %}get general keywords, and combinereplaceFilter performs variant replacement to adapt to the SEO needs of different pages. - Announcement information management: Backend settings a line
{% diy %}Announcement, frontend usestruncatecharsControl display length, click to view details. - Product Parameters DisplayIf some general parameters are defined in
{% diy %}, you can use filters to add units, format numbers, etc.
Summary
AnQiCMS{% diy %}Labels and template filters are not isolated features, they work together to provide great flexibility and efficiency for website content management and frontend display.As a website operator, mastering this deep integration application method means you can make refined adjustments and optimizations to the website content at a lower cost and faster speed, keeping your website fresh and competitive.
Common Questions (FAQ)
Q1:{% diy %}Whether the obtained value can be a complex data type, such as a list or an object, and then filter it?
A1:In most cases,{% diy %}Tags are mainly used to obtain the custom single text (string) or numeric value from the background. Although theoretically, you can store JSON strings in{% diy %}Value, but AnQiCMS's built-in template filters have limited ability to directly parse complex JSON structures. If you need to handle lists or objects, it is more recommended to usearchiveList/categoryListTags specifically used for retrieving structured data, which return data that is itself iterable or directly accessible as properties. For{% diy %}The value, it is best to treat it as a basic data type and then process it with a filter.
Q2: Can I use multiple filters with the value obtained?{% diy %}Can I use multiple filters with the value obtained?
A2:It is absolutely possible. AnQiCMS's template engine supports chained filter calls. This means you can use the output of one filter as the input for the next filter. For example,{{ diyValue|trim|upper|truncatechars:10 }}This writing is completely valid. This chained call makes it very convenient to perform multi-step, refined processing on custom content.
Q3: If I{% diy %}The obtained value is empty, how will the filter handle it?
A3:Most filters will handle empty values (nilor empty strings) according to their design logic. For example, truncatecharsNo output will be produced for an empty string;upperorlowerAn empty string is still output. If you are worried about empty values causing display issues, you can coordinate withdefaultordefault_if_nonea filter to set default values, for example{{ diyValue|default:"暂无信息"|upper }}So, evendiyValueIf it is empty, it will display 'NO INFORMATION' in uppercase, ensuring the friendliness of the page.