As an experienced website operations expert, I have accumulated rich experience in the practical application of AnQiCMS, especially knowing 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 %}The custom value obtained by the tag, can it be further processed in the template filter?

In short, the answer is yes, and this ability greatly broadens 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 it is, but it can also be formatted, truncated, converted, or calculated in various ways like other variables under the powerful influence of template filters, thus achieving a more refined and intelligent content presentation.

{% diy %}Tag: Bridge for background custom content

First, let's review{% diy %}The role of tags. In AnQiCMS,{% diy %}(Abbreviation of 'Do It Yourself') is a very practical tag that allows us to flexibly create and manage some unstructured but frequently used custom information from places like 'Global Feature Settings' or 'Contact Information Settings'. For example, a website's customer service WeChat ID, company slogan, annual copyright year, or even a temporary notice text can be created and managed through{% diy %}tags to{% diy with name="字段名称" %}The form appears easily on the website front-end.

When we go through{% diy with name="Author" %}When a custom parameter value named "Author" is obtained, 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 the variable|,followed by the filter name and optional parameters, for example{{ obj|filter_name:param }}These filters can perform various operations on string, number, array and other data types, 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 for inclusion, etc.

It is these powerful filters that empower{% diy %}The value obtained by the label has unlimited possibilities.

{% diy %}The value combines strongly with the filter

Now, let's take a specific look.{% diy %}How to make the custom value shine in the filter. Suppose we have customized several parameters in the background "Global Function Settings":

  1. Parameter name:CompanySlogan, Parameter value:AnQiCMS:让你的网站更安全,运营更高效!
  2. Parameter name:FoundedYear, Parameter value:2021
  3. Parameter name:SupportEmail, Parameter value:[email protected]

1. Handle the company slogan (CompanySlogan)

We may hope to display the complete slogan on the homepage, but only display a simplified version in the footer or other locations, while maintaining a specific format.

  • Complete display:

    <div>我们的口号:{% diy with name="CompanySlogan" %}</div>
    

    output:我们的口号:AnQiCMS:让你的网站更安全,运营更高效!

  • Extract the simplified version and convert it to uppercase:

    {%- diy sloganValue with name="CompanySlogan" %}
    <p>精简口号:{{ sloganValue|truncatechars:15|upper }}</p>
    

    Here, we first declare{% diy %}the value tosloganValueVariables, then applied in chaintruncatechars(Truncate characters, parameter is 15) andupper(Convert to uppercase) two filters. Output:精简口号:ANQICMS:让你...

2. Process the company's establishment year (FoundedYear)

Suppose we want to dynamically display 'Copyright © 2021-current year' in the copyright information.

  • Calculate the copyright year:
    
    {%- diy startYear with name="FoundedYear" %}
    <p>版权所有 © {{ startYear }}-{% now "2006" %}</p>
    
    here,FoundedYearis a number,{% now "2006" %}Get the current year. Although it does not directly statestartYearUsing a filter to perform arithmetic operations, but its numeric type allows it to be easily concatenated with text, demonstrating the combination of custom values with dynamic content. For example, if you need to perform numerical calculations, such as{{ startYear|add:1 }}It is also feasible. Output (assuming the current year is 2024):版权所有 © 2021-2024

3. Email processing supported (SupportEmail)

IfSupportEmailThe custom value is an email address, we hope it can be automatically converted into a clickable link.

  • Automatically convert to link:
    
    {%- diy emailValue with name="SupportEmail" %}
    <p>联系我们:{{ emailValue|urlize|safe }}</p>
    
    urlizeThe filter automatically identifies URLs or email addresses in the text and wraps them in<a>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,{% diy %}The custom value obtained by the tag perfectly integrates into the AnQiCMS template filter ecosystem.This brings great convenience to website operators and template developers, without the need to modify the backend code, just by adjusting the front-end template, it can achieve rich content display and flexible control.

the extension of practical application scenarios

The strength of this combination lies in its wide range of application scenarios:

  • Dynamic navigation textUtilize{% diy %}Define the display text of a navigation item and thenupperorlowerunify the format of the filter.
  • SEO keyword optimization: Pass{% diy %}Get the general keywords and thenreplacereplace the variants with the filter to adapt to the SEO needs of different pages.
  • Announcement Information Management: Set up one in the background{% diy %}Announcement, frontend utilizestruncatecharsControl the display length, click to view details.
  • Product parameter displayIf certain general parameters are defined in{% diy %}you can use filters to add units, format numbers, etc.

Summary

AnQiCMS'{% diy %}The tag and template filter are not isolated functions, they work together to provide great flexibility and efficiency for website content management and front-end display.As a website operator, mastering this integrated application method means that you can make fine adjustments and optimizations to the website content at a lower cost and faster speed, keeping your website fresh and competitive.


Frequently Asked Questions (FAQ)

Q1:{% diy %}Can the value be a complex data type, such as a list or an object, and then be filtered?

A1:Generally speaking,{% diy %}The label is mainly used to obtain a single custom text (string) or numeric value from the background. Although theoretically, you can store JSON strings in{% diy %}But the built-in template filter of AnQiCMS has limited ability to directly parse complex JSON structures. If you need to process lists or objects, it is recommended to usearchiveList/categoryListTags specifically used to obtain 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{% diy %}multiple filters consecutively on the value obtained?

A2:It is completely possible. The AnQiCMS template engine supports chained filter calls. This means you can take the output of one filter as the input for the next filter. For example,{{ diyValue|trim|upper|truncatechars:10 }}This style of writing is completely valid. This chained call makes it very convenient to perform multi-step, refined processing of custom content.

Q3: If I{% diy %}The value is empty, how will the filter handle it?

A3:Most filters will handle empty values (nilor empty strings) based on their design logic. For example,truncatecharsThere will be no output for an empty string;upperorlowerAn empty string will still be output. If you are worried about empty values causing display issues, you can usedefaultordefault_if_nonea filter to set default values, for example{{ diyValue|default:"暂无信息"|upper }}And evendiyValueIf empty, it will also display "NO INFORMATION" in uppercase to ensure the friendliness of the page.