In AnQiCMS (AnQiCMS) content operations, we often encounter the need to dynamically process strings displayed on the front end of the website.In order to enhance the user reading experience, optimize search engine inclusion, or maintain consistency in content display, it is a very practical skill to flexibly concatenate or replace strings.AnQi CMS, with its efficient template engine based on the Go language, provides us with powerful and convenient tools, making these operations easily achievable at the template level.

String concatenation: Make information flow smoothly

When we need to combine multiple variables or fixed text to form a complete sentence, string concatenation becomes particularly important.This is very common in scenarios where dynamic titles, breadcrumb navigation, product descriptions, or any information that needs to be combined for display are required.

In AnQiCMS templates, the most intuitive way to concatenate is to combine variables with text content directly.The template will intelligently connect them together. For example, if you want to display an article's title and its category:

{{ archive.Title }} - {{ archive.Category.Title }}

This will generate content similar to 'AnQiCMS Template Tips - Template Development'. Similarly, you can also concatenate fixed text:

{{ item.Title }}到{{ combine.Title }}的旅游线路

When we need to perform more complex concatenation, especially involving the mixing of numbers and strings, or when clearer type handling is required,addThe filter comes into play.addThe filter can intelligently add two values together, whether it is adding numbers or concatenating strings.If one of the values cannot be recognized as a number for calculation, it will try to concatenate strings.

For example, display the price of a product followed by the currency unit:

{{ item.Price|add:" 元" }}

If your data itself is a list (array), and you want to concatenate all its elements into a single string,joinThe filter is your helpful assistant. For example, an article may have multiple tags, and you want them to be displayed in the form of a comma-separated list:

{{ item.Tags|join:", " }}

This way, you can["SEO", "优化", "内容"]displayed asSEO, 优化, 内容.

String replacement and formatting: precise control over displayed content

Content replacement, as the name implies, is to replace a specific part of the string with the new content we want.This is very useful for unifying brand terms, correcting error information, or hiding or displaying sensitive words in specific scenarios.

AnQiCMS template providedreplaceThe filter is the main tool for implementing string replacement. You need to specify the old keyword and the new keyword, separated by a comma.

For example, replace "AnQiCMS" in the article description with "AnQiCMS":

{{ item.Description|replace:"安企CMS,AnQiCMS" }}

If you only want to remove the unnecessary parts of a string, such as extra spaces or certain characters, thencutandtrimFilters can really shine here.cutThe filter will remove all matched specified characters from the string.

For example, remove all spaces from a string:

{{ "Hello World"|cut:" " }}  {# 输出:HelloWorld #}

AndtrimThe filter focuses on removing whitespace characters from the beginning and end of a string. If you need more fine-grained control, you can also usetrimLeft(remove the beginning) andtrimRight(remove the end).

For example, remove leading and trailing spaces from a string:

{{ "  Hello World  "|trim }}  {# 输出:Hello World #}

In addition to direct replacement operations, AnQiCMS also provides various formatting filters that change the display of strings, thereby achieving a certain sense of 'replacement':

  • Extract text: truncatecharsandtruncatewordsThe filter can help you truncate long text and automatically add an ellipsis at the end.The former is truncated by character count, the latter by word count. If the content contains HTML tags, it can also be usedtruncatechars_htmlortruncatewords_htmlExtract safely, avoid breaking HTML structure.

    {{ item.Content|truncatechars:100 }}  {# 截取前100个字符 #}
    
  • Case conversion: upper/lower/capfirst(Capitalize the first letter) andtitle(Each word with its first letter capitalized can easily adjust the case of the text to meet page design or SEO standards.)

    {{ item.Title|upper }}  {# 全部大写 #}
    
  • Time formatting: stampToDateThe filter can format timestamps into the date or time format we need, which is very useful in scenarios such as displaying publication dates, update times, etc.

    {{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}
    
  • URL processing: urlizeandurlizetruncThe filter can automatically identify URLs in text and convert them into clickable hyperlinks, and you can also choose to truncate the link text.urlencodeThis is used for escaping URL parameters.

  • HTML tag processing: striptagsRemove all HTML tags,removetagsIt can remove the specified HTML tags. These are very useful when displaying plain text summaries, or cleaning user input content.

It is especially worth noting that if the content you concatenate or replace contains HTML tags, and you want the browser to correctly parse these tags rather than displaying them as plain text, then you must remember to usesafeFilter. This is the default behavior of AnQiCMS template engine for security reasons (to prevent XSS attacks).

{{ articleContent|safe }} {# 确保HTML内容被正确解析 #}

Why are these operations important?

Skillfully using string concatenation and replacement techniques not only makes the presentation of web content more rich and dynamic, but also brings many benefits:

  • Improve user experience:Generate more attractive titles and descriptions to make the page content more readable and relevant.
  • Optimize SEO:Dynamically adjust the page based on the page context.<title>/<meta description>Generate a URL that is more compliant with search engine crawling rules, thereby improving the ranking of the website in search results.
  • Improve operational efficiency:Reduce repetitive background editing work, define the content display rules once through a template, and no manual modification of the front-end display is needed when the content is updated.

Mastering these template-level string manipulation skills will undoubtedly greatly enhance our efficiency and flexibility in operating website content with AnQiCMS.


Frequently Asked Questions (FAQ)

1. What is the difference between the "Full Site Content Replacement" function on the AnQiCMS backend and string replacement in templates?

The background "full site content replacement" function (such as "document keyword replacement") is located indatabase levelModify the actual content of the website in bulk, once replaced, the original content is permanently changed. The string concatenation and replacement operations in the template are only performed on the contentdisplayed on the front end at that timePerform dynamic processing without modifying the original content in the database.Both of these methods have their own focus, background replacement is suitable for large-scale content correction, and template operation is used for flexible adjustment of the presentation layer.

2. Why is the original code displayed on the front end instead of the rendered style after I concatenate or replace some content containing HTML tags in the template?

This is the default mechanism adopted by AnQiCMS template engine for website security (to prevent cross-site scripting XSS attacks), which will automatically escape the output HTML content, and<to&lt;Wait. If you are sure