In AnQiCMS template development,wordwrapFilter is an important tool used to optimize text display.Its main function is to automatically wrap text lines that are too long according to the set length, making the content more readable on the front-end page and avoiding the situation where horizontal scroll bars or text overflow layout occur.
Deep understandingwordwrapThe original intention of the filter design
From its design, wordwrapThe filter is specifically designed to handle general text content.It identifies words based on spaces in the text and tries to insert line breaks between words when reaching a specified length.wordwrapProcessed, it can display the行长in a more standardized format, enhancing the visual experience. However, it also has its characteristics: for continuous Chinese text, since Chinese words are usually not separated by spaces,wordwrapThe filter will not force line breaks in the middle of Chinese words.
This means,wordwrapThe core logic is based on the concept of 'word' and 'space' in natural language, aiming to beautify and standardize the layout of plain text.
wordwrapThe essential difference with JSON data
Now, let's return to the core issue:wordwrapDoes the filter support formatting JSON data when outputting in AnQiCMS? The answer is:Not supported.
JSON (JavaScript Object Notation) data is a lightweight data interchange format, its structure is composed of key-value pairs, objects (curly braces){})and array (brackets)[])consists of, and uses a colon:and comma,English as a separator.JSON formatting usually refers to the process of 'beautifying' (pretty-printing) the original, compact JSON string by adding appropriate indentation and line breaks, making its hierarchy clear and easy for human reading.
wordwrapThe filter does not understand the inherent structure of JSON data. If we pass a JSON string (even an unformatted compact string) towordwrapFilter, it will treat it as ordinary text.wordwrapThe JSON string will break lines where there are spaces (for example, between keys and values, or some manually added spaces for readability), but it cannot recognize JSON syntax elements such as object boundaries, array elements, or key-value separators. Worse still, ifwordwrapIf a newline character is inserted in a critical position within a JSON (such as in the key name or in the middle of a value), it will directly destroy the validity of the JSON string, causing it to become unparseable.It cannot also achieve the hierarchical indentation required for JSON formatting.
In short,wordwrapThe filter does not have the ability to parse and understand JSON syntax, nor can it perform the indentation and structuring operations required for JSON beautification.It manages the line length of plain text only.
How to handle JSON data in AnQiCMS?
AlthoughwordwrapNot applicable to JSON, but in the application scenarios of AnQiCMS, we may still encounter situations where we need to process JSON data, such as:
Display the original JSON data from the API in the page:如果您的目的是将原始 JSON 数据(例如用于调试或展示技术细节)呈现在页面上,并且希望它具有可读性,那么通常的**实践是在后端(Go 语言的业务逻辑层)完成 JSON 数据的漂亮打印(pretty-print)操作。Go 语言标准库中的
json.MarshalIndentThe function can easily implement this feature.Processed formatted JSON string before passing to the template.<or>),and we hope the browser displays the formatted text directly instead of parsing it, by placing it inside<pre><code>tags, and combiningsafefilters to avoid HTML escaping, for example:<pre><code>{{ formattedJsonString|safe }}</code></pre>Embed JSON-LD structured data:AnQiCMS itself supports JSON-LD structured data. As shown in the documentation, you can use it directly in the template.
{% jsonLd %}...{% endjsonLd %}Label to define and output JSON data that conforms to the JSON-LD specification. The system will automatically handle it on the page<head>The correct insertion of parts. In this case, you provide a JSON structure, not thewordwrapcommon text to be processed.Debugging Go struct data:If you just want to see the internal representation of the Go struct data passed to the template, rather than its JSON format, you can use
dumpFilter orstringformat:"%#v"These filters will output detailed information about the structure in the Go language, but please note that this is not a standard JSON format.
Summary
wordwrapThe filter is a practical utility in AnQiCMS for managing line length of plain text content.It is not suitable for the formatting of JSON data because its design philosophy and working mechanism are completely different from the requirements of the JSON structure.jsonLdTo embed structured data. Understanding the purpose of each tool can make website operation and development twice as efficient.
Common Questions (FAQ)
1. I want to display a segment of original JSON data from an external API in the template, and I hope it can automatically indent and wrap.wordwrapCan't I do that, then what should I do?You should handle the JSON data returned by the API in the backend of AnQiCMS (usually the code logic layer in Go language). Before passing the data to the template, use the Go language tojson.MarshalIndentThe functions format it into a string with indentation and line breaks.Then, pass this formatted string as a variable to the template.<pre><code>Label inside, and use{{ yourFormattedJsonString|safe }}This way to output.
2. Does AnQiCMS template system have built-in filters that can likewordwrapIs it possible to directly perform a generic 'pretty-print' (pretty-print) on any data type (including JSON strings)?According to the document description of AnQiCMS,wordwrapThe filter is designed specifically for plain text, it cannot understand or format JSON structures. Althoughdumpfilters andstringformat:"%#v"Can be used to view the internal representation of Go structures, but they are not universal "pretty print" tools for JSON format.Currently, AnQiCMS's template engine Pongo2 does not provide a built-in, general-purpose JSON pretty-print filter.This type of requirement is usually recommended to be handled on the backend, or consider adding custom filters through secondary development.
3. If I just want to embed JSON-LD structured data in a template for SEO,wordwrapDoes the filter have an effect?
wordwrap过滤器对 JSON-LD 结构化数据没有任何影响,也不应与 JSON-LD 结合使用。AnQiCMS 提供了专门的 English{% jsonLd %}...{% endjsonLd %}Tags to handle such needs.You just need to write JSON code that conforms to the JSON-LD specification within this tag.wordwrapIt is completely unrelated.