In AnQiCMS template development, text and strings are the foundation for building website content.Whether it is displaying article titles, website names, or handling user input, flexibly and efficiently obtaining and manipulating strings is a skill that template developers must master.AnQiCMS powerful template engine based on Go language, providing rich tags and filters, making string acquisition and processing intuitive and practical.

Core mechanism: Understanding the string retrieval method in AnQiCMS template

In AnQiCMS template, the most direct way to obtain a string is through variable references. Just like in ordinary programming languages,{{ 变量名 }}The form of outputting the variable value. When the variable is a struct or object, its properties can be accessed through a dot (). For example.) to access its properties, for example{{ archive.Title }}The title of the current document will be output.

To more accurately obtain specific types of strings, AnQiCMS provides a variety of template tags:

  • String to obtain content details:For example, on the article detail page, you may need to retrieve the article title, content, keywords, or custom fields.{% archiveDetail %}Tags provide such capabilities. ThroughnameThe parameter specifies the field name to be retrieved, such as{% archiveDetail with name="Title" %}You can get the article title,{% archiveDetail with name="Description" %}Get the article description. For document content, you can use{% archiveDetail with name="Content" %}. Similarly,{% categoryDetail %}Used to get the category detail string,{% pageDetail %}Used for single-page,{% tagDetail %}Used for tag details.
  • String to retrieve global site information:Website name, copyright information, contact information, etc. are usually global configurations.{% system %}Labels can retrieve strings from the system settings, for example{% system with name="SiteName" %}Get the website name.{% contact %}Labels are specifically used to retrieve contact information, for example{% contact with name="Cellphone" %}Obtain contact information. If you have customized parameters in the background, you can also obtain these tags, justnameset the parameter to the parameter name you defined.
  • In a list or loop, get the stringWhen you need to display a list of articles, categories, navigation, etc.{% archiveList %}/{% categoryList %}/{% navList %}These tags are usually associated with{% for %}Loop combination usage. Each list item (such asitem) can access its string properties directly through the dot notation, such as{{ item.Title }}/{{ item.Description }}.
  • Define a temporary string variable:Sometimes you may need to define temporary string variables in the template for reuse or complex operations.{% set new_var = "这是我的自定义字符串" %}and{% with title="文章标题" %}These two methods can both help you create and use string variables in templates.

The Swiss Army knife for string processing: built-in filters

Obtaining a string often requires further processing, such as formatting, truncating, cleaning, or escaping. AnQiCMS template engine includes a large number of practical filters, which|Symbols and variables can be connected, allowing for chained usage, which greatly enhances the flexibility of string processing.

  • Formatting and conversion:
    • Case conversion:|upperConvert to uppercase,|lowerConvert to lowercase,|capfirstCapitalize the first letter,|titleConvert the first letter of each word to uppercase, which is very useful in text display processing.
    • Universal formatting:|stringformat:"%s"Can format various types of values into strings and supports Go-style placeholders for fine control.
    • Type conversion:|integerand|floatCan try to convert a string to an integer or a floating-point number, which is very useful when dealing with strings containing numbers.
  • Cutting and splicing:
    • Cutting a specified part:|slice:"start:end"Can extract a specified portion of a string or array, for example{{ "abcdefg"|slice:"1:4" }}Will get "bcd".
    • Truncate display:|truncatechars:长度Truncate a string by character length and add an ellipsis (...)|truncatewords:长度Then truncate based on the number of words. If the string contains HTML, it can be used|truncatechars_htmland|truncatewords_html, and they will try to maintain the integrity of the HTML structure when truncated.
    • String concatenation and splitting:|join:"分隔符"The array elements can be concatenated into a string.|split:"分隔符"Then, the string can be split into an array with a delimiter.|make_listand|fieldsIt can also quickly split a string into an array of characters or words.
  • Cleanup and replacement:
    • Delete characters:|trimCan remove spaces at the beginning and end of the string, or specify characters.|trimLeftand|trimRightIt handles the left and right sides separately.|cut:"字符"Can remove all occurrences of a specific character in the string.
    • Replace content:|replace:"旧词,新词"It is very convenient to replace the old content with new content in a string, which is very useful when modifying text in bulk or dealing with specific keywords.
  • Security and Encoding:
    • HTML Safe Output:AnQiCMS template defaults to escaping output HTML tags to prevent XSS attacks. If you are sure that a string content is safe HTML code and want it to be normally parsed by the browser, you can use|safeFilter. For example, the content of the article usually contains HTML, at which time it is necessary{{ archive.Content|safe }}. Conversely,|escape(or its alias)|e) will force the HTML to be escaped.
    • URL encoding:|urlencodeand|iriencodeUsed to encode URL parameters to ensure the validity and security of the URL.
    • Converts newline characters to HTML.:|linebreaksand|linebreaksbrCan convert newline characters in text to<p>or<br>Tags, suitable for formatting plain text content as web paragraphs.
  • Other practical features:
    • Default value:|default:"默认文本"Provide a fallback string when the variable is empty.
    • Statistical information:|lengthGet the length of a string or an array,|wordcountCount the number of words in a string,|count:"关键词"Count the frequency of keywords.
    • Debugging tool: When you are unsure about the structure or value of a variable,|dumpFilter can print detailed information of variables (including type and value), which is very useful during template debugging.

Practical suggestions and techniques

  1. Define the goal clearlyBefore getting the string, determine what information you need (article title or website name?) and then choose the most appropriate tag.
  2. Make good use of the dot operator.For objects that have been obtained, prefer to useitem.PropertyNameto access string properties, which is usually more concise and efficient than repeatedly calling tags.
  3. chained use of filtersMultiple filters can be chained together like a pipeline, with the output of the previous filter serving as the input to the next filter. For example,{{ item.Title|lower|truncatechars:20 }}Convert to lowercase and then truncate.
  4. Use with caution|safe:|safeCan directly output HTML, but there may be security risks if the content source is unreliable. Use it only when you completely trust the content source.
  5. Debugging TipsIf the string output does not meet expectations, try adding|dumpthe filter to view the original structure and values to help you locate the problem. For example{{ someVariable|dump }}.

AnQiCMS in templates has a powerful and flexible mechanism for obtaining and processing strings. By mastering the usage of various tags and filters, you can easily build beautiful and feature-rich website content.


Common Questions (FAQ)

Q1: How to get the current page title in the page title and automatically append the website name? A1:You can usetdkTags and combine `siteName