The value obtained by the `{% diy %}` tag, can it be further processed in the filter?

Calendar 👁️ 57

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.

Related articles

How to set and get custom website sidebar content using the `{% diy %}` tag?

As an experienced website operations expert, I know that the flexibility of the website content management system is crucial for operational efficiency.AnQiCMS (AnQiCMS) provides us with many powerful content management functions with its concise and efficient architecture.Today, let's delve deeply into one of the very practical and flexible features - how to set and get custom website sidebar content using the `{% diy %}` tag.

2025-11-06

Can the `{% diy %}` tag be used to call custom configurations of specific modules, such as only for the article module?

In the practice of template development for Anqi CMS, the `{% diy %}` tag is used frequently, providing website operators with great flexibility to customize and call various information configured in the background.However, many developers may feel confused about whether it can accurately call a custom configuration for a specific content model such as "only for article modules".Today, as an experienced website operations expert, I will deeply analyze this mechanism based on the AnQi CMS documentation and guide you on how to correctly use template tags in different scenarios.

2025-11-06

How to use the `{% diy %}` tag to get custom parameters and control formatting and styling on the front end?

As an experienced website operations expert, I am well aware that the flexibility of a Content Management System (CMS) is the key to improving operational efficiency and website performance.AnQiCMS (AnQiCMS) stands out in this aspect due to its excellent customizability, especially with the feature of obtaining custom parameters through the `{% diy %}` tag, which brings great convenience and imagination space for the layout and style control of front-end content.Today, let's delve into how to巧妙运用 this feature, making your website content both beautiful and functional.

2025-11-06

How to create and manage custom parameters for the `{% diy %}` tag in AnQiCMS backend?

Anqi CMS, as an enterprise-level content management system known for its efficiency and customization, its powerful template tag feature is undoubtedly a helpful tool for content operators and website developers.In daily website operations, we often encounter the need to quickly adjust some global and site-level configuration information, such as the company's slogan, a link to a specific event, or a temporary announcement text, etc. These pieces of information do not require complex classification and models like article content, but are more flexible than hard-coded in templates.This is provided by Anqicms with `{% diy

2025-11-06

The `{% diy %}` tag supports multiple calls to different custom content on the same page?

The `{% diy %}` tag of AnQiCMS: a powerful tool for flexibly handling web page content As an experienced website operation expert, I fully understand the importance of a flexible and efficient content management system for a corporate website.AnQiCMS (AnQiCMS) with its excellent feature design provides us with many conveniences in the content operation field.

2025-11-06

What is the main function of the 'Document Tag' feature in AnQiCMS?

As an experienced website operations expert, I know that every function of the Content Management System (CMS) can become a key to improving website operations efficiency and effectiveness.In AnQiCMS, such a content management system that focuses on enterprise-level applications and SEO optimization, the 'Document Tag' feature is not just a simple content classification, it is more like a multi-functional Swiss Army knife, providing deep empowerment for content organization, user experience, and search engine optimization.In AnQiCMS, the main function of the 'Document Tag' feature can be deeply understood from the following core dimensions

2025-11-06

How to add Tag tags for articles and products in the AnQiCMS background?

As an experienced website operations expert, I am well aware of the importance of tags (Tag) in content management and search engine optimization.AnQiCMS as an efficient and flexible content management system, its built-in tag function provides great convenience for operators.Today, let's delve into how to easily and efficiently add Tag tags to your articles and products in the AnQiCMS backend, and transform them into a powerful tool to enhance the value of your website.

2025-11-06

How should the 'document label' and 'document classification' be selected and matched in content organization?

In the daily operation of AnQiCMS (AnQiCMS), how to organize content efficiently and systematically is a problem that every operator needs to think deeply about.Among them, 'Document Classification' and 'Document Tag' are two core content organization tools, although their purposes are similar, their design concepts and application scenarios are quite different.Correctly understand and use them, not only can it significantly improve the user experience of the website, but it can also lay a solid foundation for SEO optimization.

2025-11-06